Jump to content

check image type


maccy93

Recommended Posts

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 :)

 

Link to comment
Share on other sites

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... ??

 

post-129144-13482403162615_thumb.gif

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.