Is there a way for PHP to detect a corrupted image?

Is there a way for PHP to determine if the image file is corrupted and cannot it display correctly?

I tried to check with fopenand check if the url really is, but it will not work!

+5
source share
6 answers

Javascript solution (with jQuery, although you can do without it):

<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>
+3
source

Is there a way for PHP to determine if the image file is corrupted

If broken, you mean damaged, changes to imagecreatefrom {extension} will also not be able to read them:

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

, 404, , - :

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

100%:) , file_exists(), , .

<img src="your_image_source" onerror="this.src='/path/to/backup/file'">
+1

- cpanel " ", 300 .

0

All Articles