doubledee Posted February 21, 2012 Share Posted February 21, 2012 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 Quote Link to comment Share on other sites More sharing options...
litebearer Posted February 21, 2012 Share Posted February 21, 2012 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 Quote Link to comment Share on other sites More sharing options...
doubledee Posted February 21, 2012 Author Share Posted February 21, 2012 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 Quote Link to comment Share on other sites More sharing options...
litebearer Posted February 21, 2012 Share Posted February 21, 2012 will it hurt if you catch the potential failure and deal with it? (rhetorical question) Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 21, 2012 Share Posted February 21, 2012 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 } Quote Link to comment Share on other sites More sharing options...
doubledee Posted February 21, 2012 Author Share Posted February 21, 2012 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 Quote Link to comment Share on other sites More sharing options...
litebearer Posted February 21, 2012 Share Posted February 21, 2012 IMHO using pencil and paper to sketch a flow-chart is IMMENSELY helpful in writing code and in solving problems. As is writing code re-useable in modules. See Psycho's post above Quote Link to comment Share on other sites More sharing options...
doubledee Posted February 21, 2012 Author Share Posted February 21, 2012 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 21, 2012 Share Posted February 21, 2012 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. Quote Link to comment Share on other sites More sharing options...
doubledee Posted February 21, 2012 Author Share Posted February 21, 2012 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 21, 2012 Share Posted February 21, 2012 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. Quote Link to comment Share on other sites More sharing options...
doubledee Posted February 21, 2012 Author Share Posted February 21, 2012 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 21, 2012 Share Posted February 21, 2012 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. Quote Link to comment 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.