PHP---两张图片合成一个图片
版权声明:本文为原创文章,未经允许不得转载。
/** 两张图片合成一个图片*/public function setCardPoster1(){$img_1 = '../runtime/1.png'; // 图片1路径$img_2 = '../runtime/2.png'; // 图片2路径$defaut_w = 320;// 宽$defaut_h = 240;// 宽$image_data_1 = getimagesize($img_1);// 图片1属性$image_data_2 = getimagesize($img_2);// 图片2属性$new_card = $image_data_1[1]*$defaut_w/$image_data_1[0];// 指定生成的图片1高$qrcode_height = $image_data_2[1]*$defaut_w/$image_data_2[0];// 获取到原图之后按比例计算后的高度$new_qrcode = $image_data_2[1]*$defaut_h/$image_data_2[0];// 指定生成的图片2高$defaut_poster = $new_card+$qrcode_height;// 整体高度//创建一个图片$img = imagecreatetruecolor($defaut_w,$defaut_poster);//创建颜色 透明$color = imagecolorallocate($img,255, 255, 255);// 白色//imagecolortransparent($img,$color); //这是把图片背景变成透明imagefill($img,0,0,$color);//生成透明图片ImagePng($img,'../runtime/3.png');ImageDestroy($img);// 图片一$path_1 = '../runtime/3.png';// 生成的透明图片url// 图片二// 把两个图片缩略成统一的宽度$path_2 = image_resize(file_get_contents($img_1), $defaut_w, $new_card);// 创建图片对象$image_1 = imagecreatefrompng($path_1);$image_2 = imagecreatefrompng($path_2);// 合成图片imagecopymerge($image_1, $image_2, 0, 0, 0, 0, imagesx($image_2), imagesy($image_2), 100);// 输出合成图片$res_1 = imagepng($image_1, '../runtime/image1.png');if($res_1 === false){return json_encode(array('code'=>0,'msg'=>'合成第一张图片错误'),JSON_UNESCAPED_UNICODE);}// 图片三$path_3 = '../runtime/image1.png';// 第一次合成后的图片url// 图片四$path_4 = image_resize(file_get_contents($img_2), $defaut_h, $new_qrcode);// 创建图片对象$image_3 = imagecreatefrompng($path_3);$image_4 = imagecreatefromjpeg($path_4);// 合成图片imagecopymerge($image_3, $image_4, ($defaut_w-$defaut_h)/2, $new_card, 0, 0, imagesx($image_4), imagesy($image_4), 100);// 输出合成图片$res = imagepng($image_3, '../runtime/image.png');if($res === false){return json_encode(array('code'=>0,'msg'=>'合成第二张图片错误'),JSON_UNESCAPED_UNICODE);}else{return json_encode(array('code'=>200,'msg'=>'成功'),JSON_UNESCAPED_UNICODE);}}/** 生成缩略图* $image 图片url* $width 缩放宽度* $height 缩放高度* $num 缩放比例,为0不缩放,不为0忽略参数2、3的宽高*/
function image_resize($imagedata,$width,$height,$num=0){// 获取图像信息list($bigWidth, $bigHight, $bigType) = getimagesizefromstring($imagedata);// 缩放比例if ($num > 0) {$width = $bigWidth * $num;$height = $bigHight * $num;}// 创建缩略图画板$block = imagecreatetruecolor($width, $height);// 启用混色模式imagealphablending($block, false);// 保存PNG alpha通道信息imagesavealpha($block, true);// 创建原图画板$bigImg = imagecreatefromstring($imagedata);// 缩放imagecopyresampled($block, $bigImg, 0, 0, 0, 0, $width, $height, $bigWidth, $bigHight);// 生成临时文件名$tmpFilename = tempnam(sys_get_temp_dir(), 'image_');// 保存switch ($bigType) {case 1: imagegif($block, $tmpFilename);break;case 2: imagejpeg($block, $tmpFilename);break;case 3: imagepng($block, $tmpFilename);break;}// 销毁imagedestroy($block);$image = $tmpFilename;return $image;
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
