图片合成,最常见的就是自动生成如海报等需求场景,php 本身有一个函数 imagecopymerge 可以满足我们的需求
1 2 3 4 5 6 7 8 9 10 11
   | imagecopymerge(     GdImage $dst_image,     GdImage $src_image,     int $dst_x,     int $dst_y,     int $src_x,     int $src_y,     int $src_width,     int $src_height,     int $pct ): bool
   | 
 
这个函数实现过程为:
从 x、y 坐标 src_x、src_y 开始,将 src_image 的一部分复制到 dst_image 上,宽度为 src_width,高度为 src_height。定义的部分将被复制到 x、y 坐标 dst_x 和 dst_y 上。
其中一个参数 pct 表示:
两个图像将根据 pct 合并,范围是 0 到 100。当 pct = 0,不采取任何操作,当 100 时,此函数的行为与调色板图像的 imagecopy() 相同,除了忽略 alpha alpha 组件(components),其实现了真彩色图像的 alpha 透明度。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
   | $bigImgPath = 'backgroud.png'; $qCodePath = 'qcode.png'; $bigImg = imagecreatefromstring(file_get_contents($bigImgPath)); $qCodeImg = imagecreatefromstring(file_get_contents($qCodePath)); list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath);
  imagecopymerge($bigImg, $qCodeImg, 200, 300, 0, 0, $qCodeWidth, $qCodeHight, 100); list($bigWidth, $bigHight, $bigType) = getimagesize($bigImgPath); switch ($bigType) {     case 1:          header('Content-Type:image/gif');         imagegif($bigImg);         break;     case 2:          header('Content-Type:image/jpg');         imagejpeg($bigImg);         break;     case 3:          header('Content-Type:image/png');         imagepng($bigImg);         break;     default:                  break; } imagedestroy($bigImg); imagedestroy($qcodeImg);
   | 
 
      
     
    
      
  
  
    
      
      
        
        致力于网站建设与Web开发。喜欢新事物,关注前后端动态,对新的技术有追求, 做一个优秀的web全栈工程师。