[英]How to resize gd image to fit with text written in it?
我想調整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;
}
這將導致:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.