Hi Everyone…!!!
Before starting make sure that the following line is uncommented in php.ini file
extension=php_gd.dll
Following is the function which creates 3 copies of image of different size i.e., thumbnail (approx 100*100), small (approx 250*250) and medium (approx 400*400).
You just have to pass it the complete path of the original image and it will create three different images for you.
function CreateThumbnail ($imagePath)
{
//Create your custom size and names
$sizes = Array (100, 250, 400);
$names = Array (“thumbnail”, “small”,”medium”);
//if passed image exists or not
if (file_exists ($imagePath))
{
//extract Image name and path
$imageName = substr (strrchr($imagePath, “/”), 1);
$imagePath = substr ($imagePath, 0, strripos ($imagePath,”/”)+1);
//create all my custom images
for ($i=0; $i<count($sizes); $i++)
{
//Get the dimensions of original image
$sourceImage = imagecreatefromjpeg(“$imagePath”.”$imageName”);
$sourceWidth = imagesx($sourceImage);
$sourceHeight = imagesy($sourceImage);
//Dimensions of target image (one by one)
$newWidth = $newHeight = $sizes[$i];
//Create Target image with target dimensions
$targetImage = ImageCreateTrueColor ($newWidth, $newHeight);
//Shrink the image to target image and create it physically
imagecopyresampled($targetImage,$sourceImage,0,0,0,0,$newWidth,
$newHeight,$sourceWidth, $sourceHeight);
imagejpeg($targetImage, “$imagePath”.$names[$i].”_$imageName”);
}
return true;
}
else
return false;
}
A simple call to this function could be
CreateThumbnail (“images/example.jpg”);
It will create three file namely, thumbnail_example.jpg, small_example.jpg, medium_example.jpg
–
Keep Smiling