I am trying to convert & rename an uploaded image to a jpg and rename it to thumb.jpg
function convertImage($originalImage, $outputImage, $quality)
{
// jpg, png, gif or bmp?
$exploded = explode('.',$originalImage);
$ext = $exploded[count($exploded) - 1];
if (preg_match('/jpg|jpeg/i',$ext))
$imageTmp=imagecreatefromjpeg($originalImage);
else if (preg_match('/png/i',$ext))
$imageTmp=imagecreatefrompng($originalImage);
else if (preg_match('/gif/i',$ext))
$imageTmp=imagecreatefromgif($originalImage);
else if (preg_match('/bmp/i',$ext))
$imageTmp=imagecreatefrombmp($originalImage);
else
return 0;
// quality is a value from 0 (worst) to 100 (best)
imagejpeg($imageTmp, $outputImage, $quality);
imagedestroy($imageTmp);
return 1;
}
youâve converted the file into image data.
Think of image data as a intermediate language.
So now you need to use one of the output functions to render the image data down into a file, because your code doesnt know what sort of file you want out.
Instead of using jpeg as default I would say throw an exception as default, along the lines of âUnknown image type. Only jpeg, gif (etc) images are supportedâ.
function convertImage($originalImage, $outputImage, $quality)
{
// jpg, png, gif or bmp?
$exploded = explode('.',$originalImage);
$ext = $exploded[count($exploded) - 1];
if (preg_match('/jpg|jpeg/i',$ext))
$imageTmp=imagecreatefromjpeg($originalImage);
else if (preg_match('/png/i',$ext))
$imageTmp=imagecreatefrompng($originalImage);
else if (preg_match('/gif/i',$ext))
$imageTmp=imagecreatefromgif($originalImage);
else if (preg_match('/bmp/i',$ext))
$imageTmp=imagecreatefrombmp($originalImage);
else
return 0;
// quality is a value from 0 (worst) to 100 (best)
imagejpeg($imageTmp, $outputImage, $quality);
imagedestroy($imageTmp);
return 1;
}
So when I select an image for upload (which should rename the image to thumb.jpg after the conversion) I get
ok, trying to do this so its bulletproof (everything works with no warnings), I think I have a problem with the function
function convertImage($originalImage, $outputImage, $quality) {
switch (exif_imagetype($originalImage)) {
case IMAGETYPE_PNG:
$imageTmp=imagecreatefrompng($originalImage);
break;
case IMAGETYPE_JPEG:
$imageTmp=imagecreatefromjpeg($originalImage);
break;
case IMAGETYPE_GIF:
$imageTmp=imagecreatefromgif($originalImage);
break;
case IMAGETYPE_BMP:
$imageTmp=imagecreatefrombmp($originalImage);
break;
// Defaults to JPG
default:
$imageTmp=imagecreatefromjpeg($originalImage);
break;
}
// quality is a value from 0 (worst) to 100 (best)
imagejpeg($imageTmp, $outputImage, $quality);
imagedestroy($imageTmp);
return $outputImage;
}
the upload works, but when I try and call the function, I get an error
I notice
You must have php_exif extension enabled to use this.
How can I check to see if its enabled?
Remember when I said your filename looked mauled?
Youâve passed the wrong filename to your function. Itâs been moved (and renamed in the process) already by the time you call your function, so pass it the location (and filename) it has been moved TO ($file).
If you want to do that, I would suggest youâd also need to do some checking inside the function. A lot of your warnings there are because youâre passing in a file that doesnât exist, but your function doesnât check that, so it blindly calls various imagecreatefrom*() functions, then a couple more functions, which all throw warnings when there are problems.
The way I would do it would be to check that the files exist before trying to do anything, check the returns from each (I havenât used any of those image functions, but thereâs a trend for returning false in times of trouble) and instead of returning $outputImage regardless, return false if there has been any problem. And check for it in your calling code, too.
You could write the function and presume that the calling code has already checked that the images exist, and make sure all your calling code does just that, but that somehow doesnât seem ârightâ.