I'm trying to create 3 Thumbnail Images of different sizes, from the same uploaded Image. Presently, I use the code below to create 1 thumbnail that's 150px wide.
Is there an easy way to do this instead of repeating the same code thrice for each thumbnail I need?
I'm trying to create thumbnails in 3 sizes: 750px wide, 150px wide and 70px wide.
Here's the code I use to do the Thumbnail that's 150Px wide.
Does PHP have a function to do such a thing, or is repeating the code below for each size I need my only option.
if(file_exists($thisImage)) {
$imageName = $thisImage;
$imageInfo = finfo_open(FILEINFO_MIME_TYPE);
$imageType = finfo_file($imageInfo, $imageName);
finfo_close($imageInfo);
if($imageType == 'image/pjeg' || $imageType == 'image/jpeg' || $imageType == 'image/jpg') {
$imgSource = imagecreatefromjpeg($thisImage);
} elseif ($imageType == 'image/png') {
$imgSource = imagecreatefrompng($thisImage);
} elseif ($imageType == 'image/gif') {
$imgSource = imagecreatefromgif($thisImage);
} else {
$imgSource = false;
return false;
}
if($imgSource) {
list($width,$height)=getimagesize($thisImage);
$thumbImageWidth = 150;
$thumbImageHeight = ($height/$width)*$thumbImageWidth;
$tempThumbImage = imagecreatetruecolor($thumbImageWidth,$thumbImageHeight);
if(!imagecopyresampled($tempThumbImage,$imgSource,0,0,0,0,$thumbImageWidth,$thumbImageHeight,$width,$height)) return false;
$thumbImageTarget = $thisPath.$thisName;
if(!imagejpeg($tempThumbImage,$thumbImageTarget,100)) return false;
if(!imagedestroy($imgSource)) return false;
if(!imagedestroy($tempThumbImage)) return false;
if(!unlink($thisImage)) return false;
return true;
}
} else {
return false;
}