我正在尝试按照此示例生成带有动态文本的图像。
我想更改字体的大小,我甚至输入了 100 而不是 4,但它看起来仍然和以前一样。
我不太擅长PHP。任何形式的帮助将不胜感激。
下面是一个它看起来有多小的示例:(
这是我的示例代码 -
$font = 'arial.ttf'; //FONT SIZE
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng($imageO);
$x = imagesx($im) / 2; //PLACEMENT CENTERISH – X
$y = imagesy($im) / 2; //PLACEMENT CENTERISH – Y
// $backgroundColor = imagecolorallocate ($im, 255, 255, 255);
$transparency = 25;
imagesavealpha($im, true);
$background = imagecolorallocatealpha($im, background_r, background_g, background_b, $transparency);
$textColor = imagecolorallocate ($im, 0,0,0);
imagestring ($im, $font, $x, $y, $string, $textColor);
imagepng($im,$imageN[$k]);
$w = imagesx($im);
$h = imagesy($im);
谢谢
稍后添加
好的,现在这就是我所做的,但结果,标注框中没有可见的文本。
$font = 'arial.ttf'; //YOUR FONT SIZE
$im = imagecreatefrompng($imageO);
$string = "My Text";
$imageN ="NewImage.png";
$transparency = 25;
imagesavealpha($im, true);
$background = imagecolorallocatealpha($im, background_r, background_g, background_b, $transparency);
$textColor = imagecolorallocate ($im, 0,0,0);
//imagestring ($im, 5, $x, $y, $string, $textColor);
imagettftext($im, 36, 0, 10, 20, $textColor, $font, $string);
imagepng($im,$imageN);
你不能把 100 - http://php.net/manual/en/function.imagestring.php
只有 1-5(默认)
更新
为了能够完全控制您可能想要使用的字体大小 http://php.net/manual/en/function.imagettftext.php
示例(来自同一站点):
<?php
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
有人喜欢跳出框框思考吗?是的,我也是。
因此,如果您必须使用 PHP 的 imagestring
而不是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);
你选择的任何字体写更大的文本,我建议你使用imagettftext。
http://php.net/manual/en/function.imagettftext.php
很多建议,但没有解决方案...
这是一个可行的解决方案。诀窍是为此使用 imagettftext()。它允许您选择任何大小的任何 truetype 字体。
在现有映像上使用以下代码:
<?php
$txt = 'This is the text overlay';
header('Content-type: image/png');
$image = ImageCreateFromJPEG('existingimage.jpg'); // path to the existing image
$color = imagecolorallocate($im, 0, 0, 0); // black
$font = 'fontname.ttf'; // path to font
imagettftext($image, 14, 0, 395, 85, $color, $font, $txt );
imagepng($image); // png gets better font results than jpg
imagedestroy($image);
?>
这对我有用!
更改 sting 的字体样式可以使用 imagettftext() 函数或 imageloadfont() 来完成。使用两者,您可以更改字体样式,但使用 imageloadfont(),您只是加载定义良好的二进制字体文件,在这种情况下,您无法控制字体的确切大小,无法自定义它。您不能加载真类型字体文件,它们应该是GDF文件。使用 imagettftext() 函数,您可以完全控制字体样式、大小、字体系列等。 在这种情况下,您应该通过在计算机/服务器上分配文件来直接使用真类型字体文件。以下是具有这两个函数的 2 个示例:使用 imageloadfont() 的验证码功能示例:
设置图像宽度和高度 $width = 150; $height = 30;
//Create the image resource
$image = ImageCreate($width, $height);
//We are making three colors, white, black and gray
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 46, 29, 29);
$grey = ImageColorAllocate($image, 204, 204, 204);
//Make the background black
ImageFill($image, 0, 0, $black);
$font = imageloadfont('04b.gdf');
$text='Testing';
//Add randomly generated string in white to the image
ImageString($image, $font, 20, 3, $text, $white);
ImageRectangle($image,0,0,$width-1,$height-1,$grey);
imageline($image, 0, $height/3, $width, $height/3, $grey);
imageline($image, 0, ($height/3)*2, $width, ($height/3)*2, $grey);
imageline($image, $width/2, 0, $width/2, $height, $grey);
//Tell the browser what kind of file is come in
header("Content-Type: image/jpeg");
//Output the newly created image in jpeg format
ImageJpeg($image);
下面是 imagettftext() 函数的示例:
//Set the image width and height
$width = 150;
$height = 30;
//Create the image resource
$image = ImageCreate($width, $height);
//We are making three colors, white, black and gray
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 46, 29, 29);
$grey = ImageColorAllocate($image, 204, 204, 204);
//Make the background black
ImageFill($image, 0, 0, $black);
$font = 'arial.ttf';
$text='Testing';
imagettftext($image, 20, 5, 10, 20, $white, $font, $text);
ImageRectangle($image,0,0,$width-1,$height-1,$grey);
imageline($image, 0, $height/3, $width, $height/3, $grey);
imageline($image, 0, ($height/3)*2, $width, ($height/3)*2, $grey);
imageline($image, $width/2, 0, $width/2, $height, $grey);
//Tell the browser what kind of file is come in
header("Content-Type: image/jpeg");
//Output the newly created image in jpeg format
ImageJpeg($image);