PHP将多张小图拼接成一张大图

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php
$imgs = array();
$imgs[0] = 'imgs/1.jpg';
$imgs[1] = 'imgs/2.jpg';
$imgs[2] = 'imgs/3.jpg';
$imgs[3] = 'imgs/4.jpg';
$imgs[4] = 'imgs/5.jpg';
$imgs[5] = 'imgs/6.jpg';
$imgs[6] = 'imgs/7.jpg';
$imgs[7] = 'imgs/8.jpg';
$imgs[8] = 'imgs/9.jpg';
$target  = 'bg.jpg'; //背景图片
  
$target_img = Imagecreatefromjpeg($target);
  
$source = array();
  
foreach ($imgs as $k => $v) {
    $source[$k]['source'] = Imagecreatefromjpeg($v);
    $source[$k]['size'] = getimagesize($v);
}

/*echo "<pre>";
print_r($source);
echo "</pre>";*/

//imagecopy ($target_img,$source[0]['source'],2,2,0,0,$source[0]['size'][0],$source[0]['size'][1]);
//imagecopy ($target_img,$source[1]['source'],250,2,0,0,$source[1]['size'][0],$source[1]['size'][1]);
$num1 = 0;
$num  = 2;
$tmp  = 0;
$tmpy = 0; //图片之间的间距
for ($i = 0; $i < 10; $i++) {
    @imagecopy($target_img, $source[$i]['source'], $tmp, $tmpy, 0, 0, $source[$i]['size'][0], $source[$i]['size'][1]);
      
    @$tmp = $tmp + $source[$i]['size'][0];
   // $tmp = $tmp + 0;
    if ($i == $num) {
        $tmpy = $tmpy + $source[$i]['size'][1];
       // $tmpy = $tmpy + 0;
        $tmp  = 0;
        $num  = $num + 3;
    }
}
Imagejpeg($target_img, 'pin.jpg');
  
?>
<img src="pin.jpg">

原文地址:https://www.cnblogs.com/rxbook/p/6008904.html