phpimagecreatefrompng

how to merge multiple image in single image


I have multiple images of business card with 1073X672 resolution. Now i want to merge all images in one sheet 5366X3378 (5 row 5 column) so it can store 25 cards. But after 25 cards i want to save rest card on another sheet then after next 25 rest on another sheet.

I wrote following code but this code is generating only single sheet which contains starting 25 images.

Array ( [0] => E:\idcarddata/finalcard/1.jpg [1] =>
 E:\idcarddata/finalcard/2.jpg [2] =>
 E:\idcarddata/finalcard/3.jpg [3] =>
 E:\idcarddata/finalcard/4.jpg [4] =>
 E:\idcarddata/finalcard/5.jpg [5] =>
 E:\idcarddata/finalcard/6.jpg [6] =>
 E:\idcarddata/finalcard/7.jpg [7] =>
 E:\idcarddata/finalcard/8.jpg [8] =>
 E:\idcarddata/finalcard/9.jpg [9] =>
 E:\idcarddata/finalcard/10.jpg [10] =>
 E:\idcarddata/finalcard/11.jpg [11] =>
 E:\idcarddata/finalcard/12.jpg [12] =>
 E:\idcarddata/finalcard/13.jpg [13] =>
 E:\idcarddata/finalcard/14.jpg [14] =>
 E:\idcarddata/finalcard/15.jpg [15] =>
 E:\idcarddata/finalcard/16.jpg [16] =>
 E:\idcarddata/finalcard/17.jpg [17] =>
 E:\idcarddata/finalcard/18.jpg [18] =>
 E:\idcarddata/finalcard/19.jpg [19] =>
 E:\idcarddata/finalcard/20.jpg [20] =>
 E:\idcarddata/finalcard/21.jpg [21] =>
 E:\idcarddata/finalcard/22.jpg [22] =>
 E:\idcarddata/finalcard/23.jpg [23] =>
 E:\idcarddata/finalcard/24.jpg [24] =>
 E:\idcarddata/finalcard/25.jpg [25] =>
 E:\idcarddata/finalcard/26.jpg [26] =>
 E:\idcarddata/finalcard/27.jpg [27] =>
 E:\idcarddata/finalcard/28.jpg [28] =>
 E:\idcarddata/finalcard/29.jpg [29] =>
 E:\idcarddata/finalcard/30.jpg )



    $mapImage = imagecreatetruecolor(5366, 3378);
$bgColor = imagecolorallocate($mapImage, 255, 255, 255);
imagefill($mapImage, 0, 0, $bgColor);
$temp=1;
foreach ($srcImagePaths as $index => $srcImagePath)
{
 $x = ($index % 5) * (1073 + 1) + 1;
 $y = floor($index / 5) * (672 + 1) + 1;
 $tileImg = imagecreatefromjpeg($srcImagePath);
 imagecopy($mapImage, $tileImg, $x, $y, 0, 0, 1073, 672);
 imagedestroy($tileImg);

 if($temp%20==0)
 {
    $thumbImage = imagecreatetruecolor(5366, 3378);
    imagecopyresampled($thumbImage, $mapImage, 0, 0, 0, 0, 5366, 3378, 5366, 3378);
    header('Content-type: image/png');
    imagejpeg($thumbImage,dirname($_REQUEST['csvsheet'])."/finalcard/sheet".$temp.".jpg");
 }
$temp=$temp+1; 

OUTPUT enter image description here


Solution

  • Use a counting variable $i which you can reset once you hit the 25th image. Also, you forgot to clear the mapImage before drawing the next sheet.

    $mapImage = imagecreatetruecolor(5366, 3378);
    $bgColor = imagecolorallocate($mapImage, 255, 255, 255);
    imagefill($mapImage, 0, 0, $bgColor);
    $temp=1;
    $i = 0;
    foreach ($srcImagePaths as $index => $srcImagePath)
    {
     $x = ($i % 5) * (1073 + 1) + 1;
     $y = floor($i / 5) * (672 + 1) + 1;
     $tileImg = imagecreatefromjpeg($srcImagePath);
     imagecopy($mapImage, $tileImg, $x, $y, 0, 0, 1073, 672);
     imagedestroy($tileImg);
     $i++;
    
     if($i>=25 || $index==count($srcImagePaths)-1)
     {
        imagejpeg($mapImage,dirname($_REQUEST['csvsheet'])."/finalcard/sheet".$temp.".jpg");
        $temp++; 
        $i = 0;
        imagefilledrectangle($mapImage, 0, 0, 5366, 3378, $bgColor);
     }
    }