Jump to content

Understanding imagecreatefromgif


doubledee

Recommended Posts

Can someone explain to me in plain English what is the purpose of imagecreatefromgif??

 

I read the Manual but am not really understanding what purpose it servers in the larger image rendering process.

 

Also, how should I Error-Handle this function - if at all?!

 

Thanks,

 

 

Debbie

 

 

Link to comment
Share on other sites

pretty self-explanatory

image create from gif  - makes tmp image from a gif file

image create from png - makes tmp image form png file

etc

you than use the tmp image to save as what ever

 

From what I read, that function helps "load" the image into memory for further manipulation...

 

Agree?

 

So, my larger question is, "How can I error-handle for this code?"

// **************
// Load Image.	*
// **************
switch ($imageType){
	case 'image/gif':
		$origImage=imagecreatefromgif($tempFile);
		break;

	case 'image/jpeg':
		$origImage=imagecreatefromjpeg($tempFile);
		break;

	case 'image/png':
		$origImage=imagecreatefrompng($tempFile);
		break;
}

 

What do I do if imagecreatefrom____ fails and returns FALSE?

 

I assume I should error-handle for that, right?

 

In the snippet above, I already check that an image is a GIF, JPEG or PNG, so $imageType should be okay.

 

But what if the function fails?

 

Make sense?

 

 

Debbie

 

Link to comment
Share on other sites

will it hurt if you catch the potential failure  and deal with it? (rhetorical question)

 

Grrrr...

 

No, it would be a good thing to handle every error.

 

I'm just a little unsure of how to modify my code to efficiently handle errors from any of these functions in GD...  :(

 

My larger question is one of how to adapt my current coding style to this situation?!

 

Would seeing more code help you to help me?

 

 

Debbie

 

Link to comment
Share on other sites

if($origImage===false)
{
    //Could not create the new image
    //Add error messaging and or handling as you see fit
}
else
{
    //Creation of new image was successful
    //Continue with additional processes to run against image identifier
}

 

 

How does this look...

// **************
// Load Image.	*
// **************
switch ($imageType){
	case 'image/gif':
		$origImage=imagecreatefromgif($tempFile);
		break;

	case 'image/jpeg':
		$origImage=imagecreatefromjpeg($tempFile);
		break;

	case 'image/png':
		$origImage=imagecreatefrompng($tempFile);
		break;
}

if ($origImage === FALSE){
	// Load Image Failed.
	$_SESSION['resultsCode'] = 'UPLOAD_LOAD_IMAGE_FAILED_2116';

	// Redirect to Outcome Page.
	header("Location: " . BASE_URL . "members/results.php");

	// End script.
	exit();				
}//End of LOAD IMAGE

 

 

Debbie

 

Link to comment
Share on other sites

How does this look...

 

Looks like some code that is ready for you to test. Determine the success/error conditions you want to test for and run the script with those conditions to verify the results.

 

Although I would probable add a default case to the switch statement to set $origImage = FALSE;. Then if the image type isn't found the error condition will run correctly.

Link to comment
Share on other sites

How does this look...

 

Looks like some code that is ready for you to test. Determine the success/error conditions you want to test for and run the script with those conditions to verify the results.

 

Although I would probable add a default case to the switch statement to set $origImage = FALSE;. Then if the image type isn't found the error condition will run correctly.

 

But how would I make a function like imagecreatefromgif fail??

 

 

Debbie

 

Link to comment
Share on other sites

But how would I make a function like imagecreatefromgif fail??

 

Pass it a file with a gif extension which is NOT a gif file. If you are already validating that the file is an actual image (not just a file with an image extension) then most likely you would never get to this code with an invalid image (which is why it has been stated previously that this code would not add any additional security). And, since I assume you are pulling the $imageType from the file you should be confident that the type is correct. For example, if you were to rename a jpeg image to have a .gif extension, it would pass the validation that it really is an image and when it gets to this section it should "know" that the image is really a gif image and process it accordingly - instead of failing.

 

So, a couple thoughts:

 

1. Just to validate this code as a backup to the code you already have in place to validate that it is an image file, bypass the image validation that comes before this code. Then pass the script an invalid image file with an image extension. This code should produce the error condition.

 

2. Pass the script a valid image file that uses the wrong extension (e.g. a jpeg image with a gif extension). That image should be processed without error. but, that is just a guess because I don't know what prior validations you have.

Link to comment
Share on other sites

Psycho,

 

Thanks for the responses today.

 

I am low and sleep and without food, so pardon my spaciness.

 

Once I get these little error-handling kinks worked out, I think my code is ready for a larger review.  Could I post my entire upload script here?  (Or maybe start a new thread aptly labeled?!)

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

Once I get these little error-handling kinks worked out, I think my code is ready for a larger review.  Could I post my entire upload script here?  (Or maybe start a new thread aptly labeled?!)

 

Why are you asking me? Do whatever you wish (within the rules of the forum). I doubt I will take the time to go through your code line-by-line.

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.