Click here to Skip to main content
15,891,904 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello all,

anyone could help me to merge images from a folder together in only one large image with php?

This is a similar result i wanted:
This is a similar result.


Thank you in advanced.

What I have tried:

Actually i tried this code to resize and copy many images from a source folder to a destination folder. But now i need to compose only one large image composed by all little images resized and actually inside the destination folder.

PHP
<?php
//Maximize script execution time
//ini_set('max_execution_time', 0);

    //Initial settings, Just specify Source and Destination Image folder.
$ImagesDirectory	= 'imagestest/'; //Source //Image Directory End with Slash
$DestImagesDirectory	= 'imagestest/new/'; //Destination Image Directory End with Slash
$NewImageWidth 		= 500; //New Width of Image
$NewImageHeight 	= 500; // New Height of Image
$Quality 		= 80; //Image Quality

//Open Source Image directory, loop through each Image and resize it.
if($dir = opendir($ImagesDirectory)){
	while(($file = readdir($dir))!== false){

		$imagePath = $ImagesDirectory.$file;
		$destPath = $DestImagesDirectory.$file;
		$checkValidImage = @getimagesize($imagePath);

		if(file_exists($imagePath) && $checkValidImage) //Continue only if 2 given parameters are true
		{
			//Image looks valid, resize.
			if(resizeImage($imagePath,$destPath,$NewImageWidth,$NewImageHeight,$Quality))
			{
				echo $file.' resize Success!<br />';
				/*
				Now Image is resized, may be save information in database?
				*/

			}else{
				echo $file.' resize Failed!<br />';
			}
		}
	}
	closedir($dir);
}

//Function that resizes image.
function resizeImage($SrcImage,$DestImage, $MaxWidth,$MaxHeight,$Quality)
{
   	list($iWidth,$iHeight,$type)	= getimagesize($SrcImage);
    $ImageScale          	= min($MaxWidth/$iWidth, $MaxHeight/$iHeight);
    $NewWidth              	= ceil($ImageScale*$iWidth);
    $NewHeight             	= ceil($ImageScale*$iHeight);
    $NewCanves             	= imagecreatetruecolor($NewWidth, $NewHeight);

	switch(strtolower(image_type_to_mime_type($type)))
	{
		case 'image/jpeg':
			$NewImage = imagecreatefromjpeg($SrcImage);
			break;
		case 'image/png':
			$NewImage = imagecreatefrompng($SrcImage);
			break;
		case 'image/gif':
			$NewImage = imagecreatefromgif($SrcImage);
			break;
		default:
			return false;
	}

	// Resize Image
    if(imagecopyresampled($NewCanves, $NewImage,0, 0, 0, 0, $NewWidth, $NewHeight, $iWidth, $iHeight))
    {
        // copy file
        if(imagejpeg($NewCanves,$DestImage,$Quality))
        {
            imagedestroy($NewCanves);
            return true;
        }
    }
}

?>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900