maccy93 Posted January 11, 2012 Share Posted January 11, 2012 Hi, I have an image handling php script which works fine here if the image is actually a readable format by the php program... <?php //determine image type if($this->ext == 'jpeg' || $this->ext == 'jpg') { $imageObject = imagecreatefromjpeg($image); } elseif($this->ext == 'gif') { $imageObject = imagecreatefromgif($image); } ?> However, I tested the script with a random gif created in photoshop and i got an ugly php error returned saying the image was not the correct type for imagecreatefromgif even tho the image was a .gif file. In response I then tried to make a safety test, but it doesn't seem to work: <?php //determine image type if($this->ext == 'jpeg' || $this->ext == 'jpg') { if(imagecreatefromjpeg($image)) { $imageObject = imagecreatefromjpeg($image); } else{ //now remove the old TEMP file ($image) unlink($image); return 'Sorry there is a file type error with the image you are uploading.';*/ } } elseif($this->ext == 'gif') { if(imagecreatefromjpeg($image)) { $imageObject = imagecreatefromgif($image); } else{ //now remove the old TEMP file ($image) unlink($image); return 'Sorry there is a file type error with the image you are uploading.';*/ } } ?> The $image can be either a jpg jpeg gif or png... Could anyone please tell me how to correctly check if the file is in readable by the php program in this context? Any help would be very much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/254811-check-image-type/ Share on other sites More sharing options...
scootstah Posted January 11, 2012 Share Posted January 11, 2012 Can we see where $image came from? Quote Link to comment https://forums.phpfreaks.com/topic/254811-check-image-type/#findComment-1306556 Share on other sites More sharing options...
maccy93 Posted January 11, 2012 Author Share Posted January 11, 2012 The following is part of a class so it would be quite hard for me to show all the code here. The $image actually comes from another class, it the is path to the image on the server. Before any of this there is a file extention filter to only accept jpg, jpeg, gif and png. The variable $image comes from the upload from the user, prior to this the image goes through some resizing and cropping which works perfectly. $image is the path to the image on the server, the file name in the variable $image is 100% ok. The reason for all of this: I want all images stored in one format, png. Below first checks the file type, if jpeg jpg or gif and if so places into $imageObject If $imageObject is then empty i know the file type is already png and does not need converting, otherwise i need to convert the image to png format. The following throws no errors and the script runs. //determine image type if($this->ext == 'jpeg' || $this->ext == 'jpg') { if(imagecreatefromjpeg($image)) { $imageObject = imagecreatefromjpeg($image); } else{ //now remove the old TEMP file ($image) unlink($image); return 'Sorry there is a file type error with the image you are uploading.'; } } elseif($this->ext == 'gif') { if(imagecreatefromjpeg($image)) { $imageObject = imagecreatefromgif($image); //this is line 138 in the script. } else{ //now remove the old TEMP file ($image) unlink($image); return 'Sorry there is a file type error with the image you are uploading.'; } } //if the imageObject is empty then the image is already a png and needs no converting. if(!empty($imageObject)) { //imagepng($imageObject, $image); imagepng($imageObject, $this->path . '.png'); //now remove the old TEMP file ($image) unlink($image); } //if the imageObject is empty so simply rename the image else{ rename($image,$this->path . '.png'); } return 'success'; However I get a warning when uploading gifs (where line 138 is commented in the above code): <b>Warning</b>: imagecreatefromgif() [<a href='function.imagecreatefromgif'>function.imagecreatefromgif</a>]: '0_1TEMP.gif' is not a valid GIF file in <b>fileUploader.php</b> on line <b>138</b><br /> The warning however does not stop the gif image from being uploaded, resized and coverted to a png... I just get this error in the response text. I have attached a copy of the exmaple gif i am trying to upload... there is nothing spectacular about it. Furthermore... if i upload a jpeg jpg or png I do not get any warnings... ?? Quote Link to comment https://forums.phpfreaks.com/topic/254811-check-image-type/#findComment-1306602 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.