zl程序教程

您现在的位置是:首页 >  后端

当前栏目

使用PHP的GD库绘制电商小程序的商品分享海报

PHP程序 分享 绘制 电商 商品 GD 海报
2023-09-14 09:15:04 时间

       GD(代表 Graphic Draw)是一个开源代码库,在 PHP 中创建和操作图像是必需的。 除了创建 JPEG、PNG 和 GIF 图像外,它还可以创建图形、图表和缩略图。

        下面的例子是PHP使用GD库(Graphic Draw)进行图形绘制,绘制电商小程序的商品分享海报。

//创建画布
$im = imagecreatetruecolor(640, 1136);

//填充画布背景色
$color = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $color);

//获取字体文件
$font_file = "poster/msyh.ttc"; //simhei.ttf
$font_file_bold = "poster/msyh.ttc";  //simhei.ttf

//设定字体的颜色
$font_color_1 = ImageColorAllocate ($im, 140, 140, 140);
$font_color_2 = ImageColorAllocate ($im, 28, 28, 28);
$font_color_3 = ImageColorAllocate ($im, 129, 129, 129);
$font_color_red = ImageColorAllocate ($im, 217, 45, 32);
$font_color_white = ImageColorAllocate ($im, 255, 255, 255);
$font_color_black = ImageColorAllocate($im,0,0,0);
$font_color_price = ImageColorAllocate($im,216,172,147);
$font_color_oldprice = ImageColorAllocate($im,119,119,119);
$fang_bg_color = ImageColorAllocate ($im, 254, 216, 217);

//商品图片
list($g_w,$g_h) = getimagesize($request->param('pic'));
$goodImg = $this->createImageFromFile($request->param('pic'));
imagecopyresized($im, $goodImg, 0, 0, 0, 0, 640, 640, $g_w, $g_h);

//商品标题
imagettftext($im, 26,0, 40, 690, $font_color_black ,$font_file, $this->mg_cn_substr($request->param('name'),46,0,false));
imagettftext($im, 26,0, 40, 730, $font_color_black ,$font_file, $this->mg_cn_substr($request->param('name'),46,46,true));
//商品售价
imagettftext($im, 36,0, 40, 782, $font_color_price ,$font_file, "¥".$request->param('price'));
if($request->param('oldPrice')!=null
	&& $request->param('oldPrice')!=""
	&& $request->param('oldPrice')!="null"
	&& $request->param('oldPrice')!="NULL"){
	//商品原价
	imagettftext($im, 26,0, 40, 830, $font_color_oldprice ,$font_file, "¥".$request->param('oldPrice'));
	//商品原价的横线
	imageline ( $im , 40 , 818 , strlen("¥".$request->param('oldPrice'))*21 , 818 , $font_color_oldprice);
}

//处理二维码
$qrData['page'] = $request->param('path');
$qrData['scene'] = md5($request->param('id').$request->param('inviteCode').$flag);
$res = "生成二维码";
$data = json_decode($res);
imagecopyresized($im, imagecreatefromstring($res), 40, 900, 0, 0, 200, 200, 430, 430);

//用户头像
list($l_w,$l_h) = getimagesize($request->param('avatar'));
$logoImg = $this->createImageFromFile($request->param('avatar'));
imagecopyresized($im, $logoImg, 270, 900, 0, 0, 70, 70, $l_w, $l_h);

$image_width	= 70;
$image_height	= 70;
$resource	 = imagecreatetruecolor($image_width, $image_height);	// 创建一个矩形的图像
imageantialias($resource, true);
$bgcolor	 = imagecolorallocatealpha($resource, 0, 0, 0, 127);	    // 图像的背景
imagefill($resource, 0, 0, $bgcolor);

// 圆角处理
$radius	 = 35;
// lt(左上角)
$lt_corner	= $this->get_lt_rounder_corner($radius);
imagecopymerge($resource, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100);
// lb(左下角)
$lb_corner	= imagerotate($lt_corner, 90, 0);
imagecopymerge($resource, $lb_corner, 0, $image_height - $radius, 0, 0, $radius, $radius, 100);
// rb(右上角)
$rb_corner	= imagerotate($lt_corner, 180, 0);
imagecopymerge($resource, $rb_corner, $image_width - $radius, $image_height - $radius, 0, 0, $radius, $radius, 100);
// rt(右下角)
$rt_corner	= imagerotate($lt_corner, 270, 0);
imagecopymerge($resource, $rt_corner, $image_width - $radius, 0, 0, 0, $radius, $radius, 100);
//合并
imagecopyresized($im, $resource, 270, 900, 0, 0, 70, 70, $image_width, $image_height);

//用户昵称
imagettftext($im, 20,0, 354, 930, $font_color_black ,$font_file, $request->param('nickname'));
//固定提示语
list($l_w,$l_h) = getimagesize('poster/1.png');
$logoImg = @imagecreatefrompng('poster/1.png');
imagecopyresized($im, $logoImg, 354, 940, 0, 0, 248, 40, $l_w, $l_h);

list($l_w,$l_h) = getimagesize('poster/2.png');
$logoImg = @imagecreatefrompng('poster/2.png');
imagecopyresized($im, $logoImg, 270, 1015, 0, 0, 280, 68, $l_w, $l_h);

list($l_w,$l_h) = getimagesize('poster/3.png');
$logoImg = @imagecreatefrompng('poster/3.png');
imagecopyresized($im, $logoImg, 565, 1015, 0, 0, 36, 68, $l_w, $l_h);

//处理图片返回base64
ob_start();
imagepng ($im);
$image_data = ob_get_contents();
ob_end_clean();
$base64_image = 'data:image/png;base64,' . base64_encode($image_data);

//释放空间
imagedestroy($im);
imagedestroy($goodImg);
print_r($base64_image);