function draw_share_img($goods_pic, $logo, $title, $price)
{
$background = imagecreatetruecolor(520, 880); //创建100*100大小的画布
$white = imagecolorallocate ($background, 255 , 255 , 255 );
imagefill($background, 0, 0, $white); //将背景设为红色
//拼接商品图片
if(stristr($goods_pic,'.png')){
$resource = imagecreatefrompng($goods_pic);
} else {
$resource = imagecreatefromjpeg($goods_pic);
}
$start_x = 1; $start_y= 1;//画到哪里
$pic_w = 518; $pic_h= 518;
imagecopyresized($background,$resource,$start_x,$start_y,0,0,$pic_w,$pic_h,500,500);
// 最后两个参数为原始图片宽度和高度,倒数两个参数为copy时的图片宽度和高度
//拼接二维码
$imgPath = tempnam(sys_get_temp_dir(), 'logo');
file_put_contents($imgPath, $logo);
$resource = imagecreatefromjpeg($imgPath);
$start_x = 300; $start_y= 620;//画到哪里
$pic_w = 200; $pic_h= 200;
imagecopyresized($background,$resource,$start_x,$start_y,0,0,$pic_w,$pic_h,300,300);
//商品标题
$font_size = 20;
$x = 20;$y = 580;
$str = $title;
if(mb_strlen($str,'utf-8')>22){
$str = mb_substr($str,0,21,'utf-8').'...';
}
$black_color = imagecolorallocate($background, 0, 0, 0);
imagettftext($background , $font_size ,0, $x , $y, $black_color, './fonts/DroidSansFallback.ttf', $str);
$font_size = 18;
$x = 280;$y = 860;
$str = '扫描或长按小程序码';
$gray_color = imagecolorallocate($background, 100, 100, 100);
imagettftext($background , $font_size ,0, $x , $y, $gray_color, './fonts/DroidSansFallback.ttf', $str);
//商品价格符号
$font_size = 20;
$x = 20;$y = 660;
$str = '¥';
$red_color = imagecolorallocate($background, 255, 0, 0);
imagettftext($background , $font_size ,0, $x , $y, $red_color, './fonts/DroidSansFallback.ttf', $str);
//商品价格
$font_size = 30;
$x = 50;$y = 660;
$str = number_format($price, 2);
imagettftext($background , $font_size ,0, $x , $y, $red_color, './fonts/DroidSansFallback.ttf', $str);
header("Content-Type:image/png");
imagepng($background); //生成PNG格式的图片输出给浏览器
exit;
}
有话要说