我想调整 GD 图像的大小,使其内容适合图像。
这显然是可能的吗?
最大的问题是恢复文本的大小。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");
我们会得到这张图片:
您可以看到由 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();
原图:
结果图片:
注意:源图像的纵横比将适合文本大小,没有任何逻辑。如果要保留图像的纵横比,可以使用以下内容:
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;
}
这将导致: