思想和行动 总有一个不能落后于人
虽然写出来了js实现文字和图片加载到一起生成一个新的图片的功能,但是我没有用js实现一个把生成的图片下载到本地的过程,请赌神看了看,赌神说他是用的php写的,我想既然他用php写了,我用js就不太方便获得指导啊,于是乎就顺便看了一下如何用php实现该功能。
百度了一下发现有现成的代码,所以为了方便我也就不浪费时间了,直接copy了人家的代码。
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