Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6764409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:35:27+00:00 2026-05-26T14:35:27+00:00

I am using imagecreatefromgif / imagecreatefromjpeg / imagecreatefromjpeg functions with uploaded images and sometimes

  • 0

I am using imagecreatefromgif / imagecreatefromjpeg / imagecreatefromjpeg functions with uploaded images and sometimes get an error (e.g. Warning: imagecreatefromgif(): … is not a valid GIF file) when the image is corrupted. I read lots of posts about this topic and couldn’t find a “working” answer.

I did try some of the following to validate the image (as suggested on other posts) but none of them worked in ALL situations. If the image does not have the header info, the following works, but if the image has the header/mime type info and is corrupted, it doesn’t work.

if( imagecreatefromjpeg($uploaded_image) !== false ) {
    // image is okay.
}

OR

try {
    $test = imagecreatefrompng($uploaded_image);
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

OR

using getimagesize($uploaded_image); // will return false or an error if image is not valid.

None of those situations worked with a corrupted image file that has the header info/mime type info.

I would like to prevent having those errors and if possible detect if the image is not corrupted and can be used without problems with any of the imagecreatefromgif / imagecreatefromjpeg / imagecreatefromjpeg functions. If corrupted, I would like to display an error message instead of running the funciton. I am looking for a solution in PHP.

Thank you for your help and suggestions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:35:28+00:00Added an answer on May 26, 2026 at 2:35 pm

    You can try

    $gd = @imagecreatefromstring(file_get_contents($file_path));    
        if ($gd === false) {
            throw new Exception ('Image is corrupted');
        }
    

    It should work with most image formats known to gd. Also if you need specific error message you can use error_get_last().

    @ will suppress error messages, and imagecreatefromstring tries to open known image formats. If this fails, $gd will have value ‘false’ and no error messages are thrown.

    Edit:

    Please not that in this example @ operator also will suprpess any errors from file_get_contents function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using imagecreatefromjpeg(), imagecreatefrompng() and imagecreatefromgif() to convert some image files and want to
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using the Redis info command, I am able to get all the stats of
Using Location.getBearing(); I seem to get randomly changing bearings. Aka, I can turn the
Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files.
Ok so the problem seems to be in the function not creating the image,
I'm using a neat little php script to resize my images to fit in
I'm using a single image upload/crop/resize script for files up to 10MB. When testing,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.