public function merge($dataArr) {
$retArr["ret"] = array("retCod" => "0", "retMsg" => "merge success!");
$path_1 = $dataArr['mainImg'];//要合成的主图地址,eg:http:www.xx.com/aa.png
$path_2=$_SERVER['DOCUMENT_ROOT'].'/shareImg/cuxiao5.png';//背景图
//创建图片对象
$image_1 = $this->createImg($path_1);
$image_2 = $this->createImg($path_2);
file_put_contents("/server/qqq.txt", "aaa=".$image_2."\r\n",FILE_APPEND);
file_put_contents('/server/'.date('Y-m-d').'.txt',date( 'Y-m-d H:i:s' ).' '.__FILE__.' '. __METHOD__.' '.__LINE__."eee=".$image_2, FILE_APPEND);
file_put_contents('/server/'.date('Y-m-d').'.txt',date( 'Y-m-d H:i:s' ).' '.__FILE__.' '. __METHOD__.' '.__LINE__.' '.$path_1.'\r\nimage:'.$image_1.'\r\n', FILE_APPEND);
//裁剪图片1
$arrPic1 = getimagesize($path_1);
$imgWidth = 400;
$imgHeight = 320;
$sss =sprintf("%d %d %d %d", $imgWidth, $imgHeight, $arrPic1[0], $arrPic1[1]);
file_put_contents('/server/'.date('Y-m-d').'.txt',date( 'Y-m-d H:i:s' ).' '.__FILE__.' '. __METHOD__.' '.__LINE__.$sss, FILE_APPEND);
//创建一个彩色的底图
$image_dst = imagecreatetruecolor($imgWidth, $imgHeight);
$aa = imagecopyresampled($image_dst, $image_1, 0, 0, 0, 0,$imgWidth,$imgHeight,$arrPic1[0], $arrPic1[1]*0.8);
$imgPath = $_SERVER['DOCUMENT_ROOT'];
//创建真彩画布
$image_3 = imageCreatetruecolor(400, 400);
//为真彩画布创建白色背景
$color = imagecolorallocate($image_3, 255, 255, 255);
imagefill($image_3, 0, 0, $color);
//设置透明
// src_h:源图象的高度
imagecopyresampled($image_3, $image_dst, 0, 0, 0, 0, 400, 400, 400, 400);
imagecopy($image_3, $image_2, 0, 0, 0, 0, 400, 400);
// 输出合成图片
$merge = $imgPath . '/shareImg/'.$dataArr['skuId'].'.png';
imagepng($image_3, $merge);
imagedestroy($image_1);
imagedestroy($image_2);
imagedestroy($image_3);
}
function createImg($path) {
$image = pathinfo($path);
switch (strtolower($image['extension'])) {
case 'jpg':
return imagecreatefromjpeg($path);
case 'jpeg':
return imagecreatefromjpeg($path);
case 'png':
return imagecreatefrompng($path);
case 'gif':
return imagecreatefromgif($path);
}
}
背景图cuxiao5.png
cuxiao5.png
$dataArr['mainImg']传过来的要合成的主图地址
$dataArr['mainImg'].png最终合成效果如下
image.png
网友评论