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';//背景圖
//創(chuàng)建圖片對象
$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);
//創(chuàng)建一個彩色的底圖
$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'];
//創(chuàng)建真彩畫布
$image_3 = imageCreatetruecolor(400, 400);
//為真彩畫布創(chuàng)建白色背景
$color = imagecolorallocate($image_3, 255, 255, 255);
imagefill($image_3, 0, 0, $color);
//設(shè)置透明
// 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