//案例一:將活動背景圖片和動態二維碼圖片合成一張圖片 //圖片一 $path_1 = 'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png'; //圖片二 $path_2 = 'http://tb1.bdstatic.com/tb/static-client/img/webpage/wap_code.png'; //創建圖片對象 //imagecreatefrompng($filename)--由文件或 URL 創建一個新圖象 $image_1 = imagecreatefrompng($path_1); $image_2 = imagecreatefrompng($path_2); //合成圖片 //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_1, $image_2, 0, 0, 0, 0, imagesx($image_2), imagesy($image_2), 100); // 輸出合成圖片 //imagepng($image[,$filename]) — 以 PNG 格式將圖像輸出到瀏覽器或文件 $merge = 'merge.png'; var_dump(imagepng($image_3,'./merge.png')); //案例二:將活動背景圖片設置透明,然后和動態二維碼圖片合成一張圖片 // 圖片一 $path_1 = 'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png'; // 圖片二 $path_2 = 'http://tb1.bdstatic.com/tb/static-client/img/webpage/wap_code.png'; //創建圖片對象 $image_1 = imagecreatefrompng($path_1); $image_2 = imagecreatefrompng($path_2); //創建真彩畫布 //imagecreatetruecolor(int $width, int $height)--新建一個真彩色圖像 $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); // 輸出合成圖片 var_dump(imagepng($image_3,'./merge1.png'));
