Is there any way to have PHP detect a corrupted image?

10,798

Solution 1

Javascript solution (with involving jQuery, though this should be possible to do without it too):

<script type='text/javascript'>
    $(function(){
        var files = [
            'warning-large.png',
            'warning-large-corrupted.png',
            'http://www.example.com/none.gif',
            'http://sstatic.net/stackoverflow/img/favicon.ico'
        ];
        for ( var n in files ) {
            var img = $('<img/>');
            img.error(function(){
                alert('error:\n' + this.src);
            });
            img.load(function(){
                alert('success:\n' + this.src);
            });
            img.attr('src', files[n]);
        }
    });
</script>

Solution 2

Is there any way to have PHP determine whether an image file is broken

If by broken you mean corrupted, changes are the imagecreatefrom{extension} won't be able to read them either:

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

Solution 3

If you mean broken as in a 404, and not a corrupt image, you can always use something along the lines of:

if (file_exists($imageFileName)) {
  ..
}

Solution 4

This works for me 100% :) I test if image exists by file_exists() and if it exists, you will catch corrupted images with this.

<img src="your_image_source" onerror="this.src='/path/to/backup/file'">
Share:
10,798
Piotr
Author by

Piotr

Updated on July 18, 2022

Comments

  • Piotr
    Piotr almost 2 years

    Is there any way to have PHP determine whether an image file is corrupted and will not be able to display properly?

    I've tried to check with fopen and check whether the URL is valid, but it hasn't worked!

    • AlienWebguy
      AlienWebguy almost 13 years
      Broken as in corrupted or missing?
    • Haim Evgi
      Haim Evgi almost 13 years
    • pavium
      pavium almost 13 years
      PHP doesn't display images, browsers do. Maybe you mean PHP will not be able to send it to the browser...
    • Piotr
      Piotr almost 13 years
      yes i meant corrupted, i should have been clearer. sorry. i need PHP to not send it to the browser if it's corrupted.
  • binaryLV
    binaryLV almost 13 years
    Keep in mind that file_exists() checks both files and directories. I would suggest using is_file().
  • binaryLV
    binaryLV almost 13 years
    Keep in mind that file_exists() checks both files and directories. I would suggest using is_file().
  • binaryLV
    binaryLV almost 13 years
    Sometimes it is good idea, sometimes it is not. It will probably raise a warning in case of error. Warnings may be ignored by using @, though that would also ignore "Fatal error: function not found" errors, which would be difficult to debug. Also, when I used imagecreatefromjpeg() on a PHP file, HTTP request failed with "The connection was reset" message in Firefox and I got "PHP Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error" error in Apache's (not PHP!) log, which would also be very difficult to debug. IMHO, this is bad idea.
  • Piotr
    Piotr almost 13 years
    hi thanks, but this doesn't work... it still sees the image as valid, ie not corrupted, and tries to display it... only it displays it with the 'broken image' icon...
  • Sandeep Manne
    Sandeep Manne almost 13 years
    Your images are not there or its corrupted?
  • Sandeep Manne
    Sandeep Manne almost 13 years
    if it corrupted then try to find mime type of image using mime_content_type php.net/manual/en/function.mime-content-type.php $tempArray = explode("/",mime_content_type(file)); if ($temp[0] == "image") { showImage }
  • Piotr
    Piotr almost 13 years
    okay, so imagecreatefromstring seems to get the job done... now i have a different problem - and that's because the images i'm displaying are already glitched... so i need it to shut up about the image being distorted... if it's displayed alright, then that's fine... so the warnings are in the way now.
  • Piotr
    Piotr almost 13 years
    nevermind... found the solution. thanks very much to all who took the time to help! :)
  • Berry Langerak
    Berry Langerak almost 13 years
    @binaryLV I obviously don't mean real-time checking. You shouldn't do things like this while visitors are watching the page, but it's okay in a separate script. Also, I get the errors in my PHP error logs, so that might have been your configuration. I don't think it is a bad idea, as long as you run the script yourself, instead of using it on every page-view (which will bother the visitors).
  • Piotr
    Piotr almost 13 years
    imagecreatefromstring does this... but it does it too well unfortunately... it checks if image is corrupt... but what i am doing is displaying corrupted/glitched images... the difference being that some images are displayable although corrupted, however others cannot be displayed... i have to be able to make that distinction... so i need to tell apart a corrupted image, which is displayable and openable from one that isn't...
  • Berry Langerak
    Berry Langerak almost 13 years
    @Piotr In that case, I'm afraid I'll have to disappoint you: there is no function in PHP that can mimic a browser, open the file and "look at it" as far as I know.
  • Piotr
    Piotr almost 13 years
    what about javascript... do you know if this can be done in javascript on client-side? i just need some way to do this... even if it's from the browser, because then i will be able to go on fine from thereon... :)
  • Berry Langerak
    Berry Langerak almost 13 years
    @Piotr Javascript might be able to do it. Load the image, and try to get the dimensions of the image. If it's 0, changes are good that the image is in fact corrupted and not displayable.
  • Piotr
    Piotr almost 13 years
    hmmm... i can get the dimensions with PHP too, gonna try that first! :)
  • binaryLV
    binaryLV almost 13 years
    @Sandeep Manne, mime_content_type() is deprecated. Also, those generic "get mime type" functions check only few bytes of a file, something like "if file starts with '1f8b', it might be a gzip file", which is not enough to detect corrupted files.