PHP GD库---之商详合成分享图片

$item_pic = 'img/item.jpg';
$qcode_pic = 'img/qcode.png';
$user_pic = 'img/user.jpeg';

$item_title = "镇泰吹风机家用孕婴负离子护";
$item_title2 = "发蓝光大风力 家用";
$font_file = 'fonts/Microsoft_Yahei.ttf';

$item_price = '¥100.50';

$share_title = "来自邹喆的分享";


//创建画布
$percent = 0.5;
$width = 400;
$height = 700;
$image = imagecreatetruecolor($width, $height);

//创建背景颜色
$image_bg = imagecolorallocate($image, 255, 255, 255);
//创建字体颜色
$image_text_bg = imagecolorallocate($image, 0, 0, 0);

//创建价格颜色
$price_bg = imagecolorallocate($image, 255, 0, 0);

//创建灰色颜色
$qcode_bg = imagecolorallocate($image, 128, 138, 135);

//填充背景颜色
imagefill($image, 0, 0, $image_bg);

//把商品图片合成到画布商品
//先把商品图片换成图片流
list($item_pic_width, $item_pic_height) = getimagesize($item_pic);

$item_pic = imagecreatefromjpeg($item_pic);

$new_item_width = $item_pic_width * $percent;
$new_item_height = $item_pic_height * $percent;


//加入商品
imagecopyresized($image, $item_pic, 0, 120, 0, 0, $new_item_width, $new_item_height, $item_pic_width, $item_pic_height);

//加入商品标题
imagettftext($image, 12, 0, 10, 590, $image_text_bg, $font_file, $item_title);
imagettftext($image, 12, 0, 11, 590, $image_text_bg, $font_file, $item_title);


imagettftext($image, 12, 0, 25, 610, $image_text_bg, $font_file, $item_title2);
imagettftext($image, 12, 0, 26, 610, $image_text_bg, $font_file, $item_title2);

//加入商品价格
imagettftext($image, 20, 0, 30, 650, $price_bg, $font_file, $item_price);
//加入推荐者头像

list($user_pic_width, $user_pic_height) = getimagesize($user_pic);
$user_pic = imagecreatefromjpeg($user_pic);
$new_user_pic_width = $user_pic_width * $percent;
$new_user_pic_height = $user_pic_height * $percent;

imagecopyresampled($image, $user_pic, 80, 50, 0, 0, $new_user_pic_width, $new_user_pic_height, $user_pic_width, $user_pic_height);
//加入推荐者
imagettftext($image, 12, 0, 150, 100, $image_text_bg, $font_file, $share_title);

//加入二维码
list($qcode_pic_width, $qcode_pic_height) = getimagesize($qcode_pic);


$new_qcode_width = $qcode_pic_width;
$new_qcode_height = $qcode_pic_height;

$qcode_pic = imagecreatefrompng($qcode_pic);

imagecopyresized($image, $qcode_pic, 260, 550, 0, 0, $new_qcode_width, $new_qcode_height, $qcode_pic_width, $qcode_pic_height);

//加入二维码文字
imagettftext($image, 10, 0, 277, 679, $qcode_bg, $font_file, '长按识别二维码');
header("Content-type:image/jpeg");

imagejpeg($image);
imagejpeg($image, 'img/0.jpg');

  效果图:

 

posted @ 2018-12-18 17:24  桔子木木  阅读(505)  评论(0编辑  收藏  举报