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!
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.
}
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>
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)) {
..
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With