public function getmergeQR($file,$card_id){
//案例一:将活动背景图片和动态二维码图片合成一张图片
//图片一
$path_1 = 'http://**********/uploads/bade8.png';
$context = stream_context_create(array(
'http' => array(
'timeout' => 30 //超时时间,单位为秒
) ));
ini_set("user_agent","Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)");
// ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; GreenBrowser)');
$path_file = file_get_contents($path_1, 0, $context);
// $path_file = geturldata($path_1);
// ob_start();
// readfile($path_1);
// $path_file = ob_get_contents();
// ob_end_clean();
//图片二 图片路径
$path_2 = "http://***{$file}";
//创建图片对象
// $image_1 = imagecreatefrompng($path_1);
$image_1 = imagecreatefromstring($path_file);
$image_2 = imagecreatefromjpeg($path_2);
//创建真彩画布
$image_3 = imageCreatetruecolor(imagesx($image_1),imagesy($image_1));
//为真彩画布创建白色背景
//imagecolorallocate(resource $image, int $red, int $green, int $blue)
$color = imagecolorallocate($image_3, 255, 255, 255);
//imagefill(resource $image ,int $x ,int $y ,int $color)
//在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)
imagefill($image_3, 0, 0, $color);
//设置透明
//imagecolortransparent(resource $image [,int $color])
//将image图像中的透明色设定为 color
imageColorTransparent($image_3, $color);
//复制图片一到真彩画布中(重新取样-获取透明图片)
//imagecopyresampled(resource $dst_image ,resource $src_image ,int $dst_x ,int $dst_y ,int $src_x , int $src_y ,int $dst_w ,int $dst_h ,int $src_w ,int $src_h)
// dst_image:目标图象连接资源
// src_image:源图象连接资源
// dst_x:目标 X 坐标点
// dst_y:目标 Y 坐标点
// src_x:源的 X 坐标点
// src_y:源的 Y 坐标点
// dst_w:目标宽度
// dst_h:目标高度
// src_w:源图象的宽度
// src_h:源图象的高度
imagecopyresampled($image_3, $image_1, 0, 0, 0, 0, imagesx($image_1), imagesy($image_1), imagesx($image_1), imagesy($image_1));
//与图片二合成
//imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )---拷贝并合并图像的一部分
// //将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。两图像将根据 pct 来决定合并程度,其值范围从 0 到 100。当 pct = 0 时,实际上什么也没做,当为 100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。
imagecopymerge($image_3, $image_2, 0, 0, 0, 0, imagesx($image_2), imagesy($image_2), 100);
// 输出合成图片
$baseUrl = DIRECTORY_SEPARATOR .implode(DIRECTORY_SEPARATOR, ['mergewechat',date('Y-m-d',time())]). DIRECTORY_SEPARATOR;
// $baseUrl = DIRECTORY_SEPARATOR .implode(DIRECTORY_SEPARATOR, ['wxacode']). DIRECTORY_SEPARATOR;
$path = $_SERVER['DOCUMENT_ROOT'].'/uploads'.$baseUrl;
if(!is_dir($path)){
mkdir($path, 0777, true);
}
$image_file = $path.$card_id.'.png';
imagepng($image_3,$image_file);
return '/uploads'.$baseUrl.$card_id.'.png';
}
PHP合成图片方法
最新推荐文章于 2024-07-11 17:27:04 发布