如何调整gd图像的大小以适应写在其中的文本?

时间:2012-10-14 07:38:23

标签: php gd

我想调整GD图像的大小,使其内容适合图像。

这显然是可能的吗?

1 个答案:

答案 0 :(得分:9)

最大的问题是恢复文本的大小。 Imagettftext以及imagettfbox返回一个点数组,用于描述图像中文本的位置,但不会返回允许您在逻辑上将图像拟合到其中的区域。

这就是我的意思:

function demo($text, $size, $angle, $font)
{
    $gdh = imagecreatetruecolor(300, 300);
    $white = imagecolorallocate($gdh, 255, 255, 255);
    imagefill($gdh, 0, 0, $white);

    $black = imagecolorallocate($gdh, 0, 0, 0);
    $rect = imagettftext($gdh, $size, $angle, 50, 250, $black, $font, $text);

    // Display a rectangle using the teturn of imagettftext
    $red = imagecolorallocate($gdh, 255, 0, 0);
    imageline($gdh, $rect[0], $rect[1], $rect[2], $rect[3], $red);
    imageline($gdh, $rect[0], $rect[1], $rect[6], $rect[7], $red);
    imageline($gdh, $rect[2], $rect[3], $rect[4], $rect[5], $red);
    imageline($gdh, $rect[4], $rect[5], $rect[6], $rect[7], $red);

    // Calculate and display the real area we need to fit our image
    $blue = imagecolorallocate($gdh, 0, 0, 255);
    $minX = min(array ($rect[0], $rect[2], $rect[4], $rect[6]));
    $maxX = max(array ($rect[0], $rect[2], $rect[4], $rect[6]));
    $minY = min(array ($rect[1], $rect[3], $rect[5], $rect[7]));
    $maxY = max(array ($rect[1], $rect[3], $rect[5], $rect[7]));
    imageline($gdh, $minX, $minY, $minX, $maxY, $blue);
    imageline($gdh, $maxX, $minY, $maxX, $maxY, $blue);
    imageline($gdh, $minX, $minY, $maxX, $minY, $blue);
    imageline($gdh, $minX, $maxY, $maxX, $maxY, $blue);

    header("Content-type: image/png");
    imagepng($gdh);
    die();
} 

如果我们致电:

demo("Cheers!", 48, 45, "font.ttf");

我们会得到这张图片:

enter image description here

您可以在红色中看到imagettfbox给出的区域,用蓝色显示允许我们定义适合文本的新图像大小的计算区域。

这是一个使文本适合图像的解决方案(当然,用你自己的字体替换font.ttf)。

function fitImageToText($gdh, $text, $size, $angle, $font)
{
    // Recover the box size where we will be able to put our text
    // Took from the doc http://www.php.net/manual/en/function.imagettfbbox.php#105593
    putenv('GDFONTPATH=' . realpath('.'));
    $rect = imagettfbbox($size, $angle, $font, $text);
    $minX = min(array ($rect[0], $rect[2], $rect[4], $rect[6]));
    $maxX = max(array ($rect[0], $rect[2], $rect[4], $rect[6]));
    $minY = min(array ($rect[1], $rect[3], $rect[5], $rect[7]));
    $maxY = max(array ($rect[1], $rect[3], $rect[5], $rect[7]));
    $targetWidth = $maxX - $minX;
    $targetHeight = $maxY - $minY;
    $srcWidth = imagesx($gdh);
    $srcHeight = imagesy($gdh);

    // Creating target image
    $targetGdh = imagecreatetruecolor($targetWidth, $targetHeight);
    imagealphablending($targetGdh, false);
    imagesavealpha($targetGdh, true);
    imagecopyresampled($targetGdh, $gdh, 0, 0, 0, 0, $targetWidth, $targetHeight, $srcWidth, $srcHeight);

    // Writting text inside the new image
    $red = imagecolorallocate($targetGdh, 255, 0, 0);
    imagettftext($targetGdh, $size, $angle, abs($minX), abs($minY), $red, $font, $text);

    return $targetGdh;
}

要打电话:

$gdh = imagecreatefrompng("cheers.png");
$newGdh = fitImageToText($gdh, "Cheers!", 48, 45, "font.ttf");
imagedestroy($gdh);

// Outputs the image
header("Content-type: image/png");
imagepng($newGdh);
imagedestroy($newGdh);
die();

原始图片:

enter image description here

结果图:

enter image description here

注意:源图像的宽高比将适合文本大小而没有任何逻辑。 如果要保留图像的宽高比,可以使用以下内容:

function fitImageToText($gdh, $text, $size, $angle, $font)
{
    // Recover the box size where we will be able to put our text
    // Took from the doc http://www.php.net/manual/en/function.imagettfbbox.php#105593
    putenv('GDFONTPATH=' . realpath('.'));
    $rect = imagettfbbox($size, $angle, $font, $text);
    $minX = min(array ($rect[0], $rect[2], $rect[4], $rect[6]));
    $maxX = max(array ($rect[0], $rect[2], $rect[4], $rect[6]));
    $minY = min(array ($rect[1], $rect[3], $rect[5], $rect[7]));
    $maxY = max(array ($rect[1], $rect[3], $rect[5], $rect[7]));
    $targetWidth = $maxX - $minX;
    $targetHeight = $maxY - $minY;
    $srcWidth = imagesx($gdh);
    $srcHeight = imagesy($gdh);

    // Recovering new source image size respecting its aspect ratio
    $srcRatio = $srcWidth / $srcHeight;
    $targetRatio = $targetWidth / $targetHeight;
    if ($srcWidth <= $targetWidth && $srcHeight <= $targetHeight)
    {
        $imgTargetWidth = $srcWidth;
        $imgTargetHeight = $srcHeight;
    }
    else if ($targetRatio > $srcRatio)
    {
        $imgTargetWidth = (int) ($targetHeight * $srcRatio);
        $imgTargetHeight = $targetHeight;
    }
    else
    {
        $imgTargetWidth = $targetWidth;
        $imgTargetHeight = (int) ($targetHeight / $srcRatio);
    }

    // Creating target image
    $targetGdh = imagecreatetruecolor($targetWidth, $targetHeight);
    imagealphablending($targetGdh, false);
    imagesavealpha($targetGdh, true);
    imagecopyresampled($targetGdh, $gdh, 0, 0, 0, 0, $imgTargetWidth, $imgTargetHeight, $srcWidth, $srcHeight);

    // Writting text inside the new image
    $red = imagecolorallocate($targetGdh, 255, 0, 0);
    imagettftext($targetGdh, $size, $angle, abs($minX), abs($minY), $red, $font, $text);

    return $targetGdh;
}

这将导致:

enter image description here