有人喜欢跳出框框思考吗?我也是。
因此,如果您必须使用 PHPimagestring
而不是imagettftext
有一种方法可以创建大于标准 1-5 范围的文本大小,并且它需要您将文本创建为大小 A,然后将您创建文本的图像调整为变大。多大取决于你想要的文本有多大。
所以让我们来看看它......
:1。创建空白 png 只是为了将我们的文本放到上面。我们还需要一个最终的图像来编译东西。这些可以imagecreatetruecolor
用于透明背景。
$ImageText1Small = imagecreate( 148, 16 );
$ImageText1Large = imagecreate( 148, 16 );
$ImageText2Small = imagecreate( 308, 40 );
$ImageText2Large = imagecreate( 308, 40 );
$ImageFinal = imagecreate( 500, 100 );
:2。对我们的文本图像的背景和文本颜色进行排序。黑与白。多么原始。
$backgroundColor1 = imagecolorallocate($ImageText1Small, 255,255,255);
$textColor1 = imagecolorallocate($ImageText1Small, 0,0,0);
$backgroundColor2 = imagecolorallocate($ImageText2Small, 255,255,255);
$textColor2 = imagecolorallocate($ImageText2Small, 0,0,0);
:3。我们需要文字。添加它。
imagestring( $ImageText1Small, 1, 1, 0, 'Stack Overflow', $textColor1 );
imagestring( $ImageText2Small, 5, 1, 0, 'Harry Harry Harry', $textColor2 );
:4。这是聪明的一点。调整较小的文本图像,使其大于最大 5 字体。
imagecopyresampled($ImageText1Large, $ImageText1Small, 0, 0, 0, 0, 148, 16, 74, 8);
imagecopyresampled($ImageText2Large, $ImageText2Small, 0, 0, 0, 0, 308, 40, 154, 20);
:5。在这里我做了一些旋转,但显然这是可选的。
$ImageText1Large = imagerotate ( $ImageText1Large, 20, $backgroundColor1 );
$ImageText2Large = imagerotate ( $ImageText2Large, -5, $backgroundColor2 );
:6。获取我们新旋转图像的尺寸。如果您旋转,这也是可选的。
$ImageText1W = imagesx($ImageText1Large);
$ImageText1H = imagesy($ImageText1Large);
$ImageText2W = imagesx($ImageText2Large);
$ImageText2H = imagesy($ImageText2Large);
:7。将文本图像层粘贴到最终图像上:
imagecopymerge($ImageFinal, $ImageText1Large, 350, 20, 0, 0, $ImageText1W, $ImageText1H, 100);
imagecopymerge($ImageFinal, $ImageText2Large, 20, 20, 0, 0, $ImageText2W, $ImageText2H, 100);
:4。打印出来,或者保存:
header( 'Content-type: image/png' );
imagepng($ImageFinal, 0);
:5。自己清理:
imagecolordeallocate( $ImageText1Small, $textColor1 );
imagecolordeallocate( $ImageText1Small, $backgroundColor1 );
imagecolordeallocate( $ImageText1Large, $textColor2 );
imagecolordeallocate( $ImageText1Large, $backgroundColor2 );
imagedestroy($ImageText1);
imagedestroy($ImageText2);
imagedestroy($ImageFinal);
显然你可以玩弄: * 起始图像大小 * 起始字体 (1-5) * 旋转 * 进一步放大 * 背景颜色 * 透明背景 * 定位 *imagepng
压缩级别
整个脚本,不完美,但功能在这里:
$ImageText1Small = imagecreate( 148, 16 );
$ImageText1Large = imagecreate( 148, 16 );
$ImageText2Small = imagecreate( 308, 40 );
$ImageText2Large = imagecreate( 308, 40 );
$ImageFinal = imagecreate( 500, 100 );
$backgroundColor1 = imagecolorallocate($ImageText1Small, 255,255,255);
$textColor1 = imagecolorallocate($ImageText1Small, 0,0,0);
$backgroundColor2 = imagecolorallocate($ImageText2Small, 255,255,255);
$textColor2 = imagecolorallocate($ImageText2Small, 0,0,0);
imagestring( $ImageText1Small, 1, 1, 0, 'Stack Overflow', $textColor1 );
imagestring( $ImageText2Small, 5, 1, 0, 'Harry Harry Harry', $textColor2 );
imagecopyresampled($ImageText1Large, $ImageText1Small, 0, 0, 0, 0, 148, 16, 74, 8);
imagecopyresampled($ImageText2Large, $ImageText2Small, 0, 0, 0, 0, 308, 40, 154, 20);
$ImageText1Large = imagerotate ( $ImageText1Large, 20, $backgroundColor1 );
$ImageText2Large = imagerotate ( $ImageText2Large, -5, $backgroundColor2 );
$ImageText1W = imagesx($ImageText1Large);
$ImageText1H = imagesy($ImageText1Large);
$ImageText2W = imagesx($ImageText2Large);
$ImageText2H = imagesy($ImageText2Large);
imagecopymerge($ImageFinal, $ImageText1Large, 350, 20, 0, 0, $ImageText1W, $ImageText1H, 100);
imagecopymerge($ImageFinal, $ImageText2Large, 20, 20, 0, 0, $ImageText2W, $ImageText2H, 100);
header( "Content-type: image/png" );
imagepng($ImageFinal);
imagecolordeallocate( $ImageText1, $textColor1 );
imagecolordeallocate( $ImageText2, $textColor2 );
imagedestroy($ImageText1);
imagedestroy($ImageText2);