Home >Backend Development >PHP Tutorial >How to use PHP to stitch multiple pictures into one long picture?

How to use PHP to stitch multiple pictures into one long picture?

WBOY
WBOYOriginal
2016-12-01 01:27:522775browse

There are multiple short pictures that can be spliced ​​into one long picture. How to do this easily?

Reply content:

There are multiple short pictures that can be spliced ​​into one long picture. How to do this easily?

<code><?php
$imgs    = array();
$imgs[0] = 'imgs/1.jpg';
$imgs[1] = 'imgs/2.jpg';
$imgs[2] = 'imgs/3.jpg';
$imgs[3] = 'imgs/4.jpg';
$target  = 'emp.jpg'; //背景图片
 
$target_img = Imagecreatefromjpeg($target);
 
$source = array();
 
foreach ($imgs as $k => $v) {
    $source[$k]['source'] = Imagecreatefromjpeg($v);
     
    $source[$k]['size'] = getimagesize($v);
     
}
 
//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  = 1;
$tmp  = 2;
$tmpy = 2; //图片之间的间距
for ($i = 0; $i < 4; $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 + 5;
    if ($i == $num) {
        $tmpy = $tmpy + $source[$i]['size'][1];
        $tmpy = $tmpy + 5;
        $tmp  = 2;
        $num  = $num + 3;
    }
}
Imagejpeg($target_img, 'pin.jpg');
 
?>
<img src="pin.jpg"></code>

imagecopyresample.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn