本文主要和大家分享php字符串生成图片实例详解,希望能帮助到大家。
function generateimg($source, $text1, $text2, $text3, $font = './msyhbd.ttf') { $date = '' . date ( 'ymd' ) . '/'; $img = $date . md5 ( $source . $text1 . $text2 . $text3 ) . '.jpg'; if (file_exists ( './' . $img )) { return $img;
}
$main = imagecreatefromjpeg ( $source );
$width = imagesx ( $main ); $height = imagesy ( $main );
$target = imagecreatetruecolor ( $width, $height );
$white = imagecolorallocate ( $target, 255, 255, 255 );
imagefill ( $target, 0, 0, $white );
imagecopyresampled ( $target, $main, 0, 0, 0, 0, $width, $height, $width, $height );
$fontsize = 18;//像素字体
$fontcolor = imagecolorallocate ( $target, 255, 0, 0 );//字的rgb颜色
$fontbox = imagettfbbox($fontsize, 0, $font, $text1);//文字水平居中实质
imagettftext ( $target, $fontsize, 0, ceil(($width - $fontbox[2]) / 2), 190, $fontcolor, $font, $text1 );
$fontbox = imagettfbbox($fontsize, 0, $font, $text2);
imagettftext ( $target, $fontsize, 0, ceil(($width - $fontbox[2]) / 2), 370, $fontcolor, $font, $text2 );
$fontbox = imagettfbbox($fontsize, 0, $font, $text3);
imagettftext ( $target, $fontsize, 0, ceil(($width - $fontbox[2]) / 2), 560, $fontcolor, $font, $text3 );
//imageantialias($target, true);//抗锯齿,有些php版本有问题,谨慎使用
imagefilledpolygon ( $target, array (10 + 0, 0 + 142, 0, 12 + 142, 20 + 0, 12 + 142), 3, $fontcolor );//画三角形
imageline($target, 100, 200, 20, 142, $fontcolor);//画线
imagefilledrectangle ( $target, 50, 100, 250, 150, $fontcolor );//画矩形
//bof of 合成图片
$child1 = imagecreatefromjpeg ( 'http://gtms01.alicdn.com/tps/i1/t1n0pxfehaxxxxk1nm-357-88.jpg' );
imagecopymerge ( $target, $child1, 0, 400, 0, 0, imagesx ( $child1 ), imagesy ( $child1 ), 100 ); //eof of 合成图片
@mkdir ( './' . $date );
imagejpeg ( $target, './' . $img, 95 );
imagedestroy ( $main );
imagedestroy ( $target );
imagedestroy ( $child1 ); return $img;
}generateimg ( 'https://ssl.picture.qingger.com/fi8zmits5c_uvm7wuez5z_0vsdg_', 'aaaaa', 'php文字水平居中', '3个字' );exit ();
相关推荐:
php实现生成图片缩略图函数
node实现文字生成图片代码分享
利用php 内置函数生成图片的方法实例详解
以上就是php字符串生成图片实例详解的详细内容。