1

I have a folder full of .jpg images. With a PHP script I'm reading the list of images in the folder and I'm displaying them on the browser.

The problem is that some of the images exist, but something it's wrong with them. They are few Kb but they can't be displayed (on the browser I see like a broken image), of course if I download these images in local I can't see them.

I tried to google but all I found was how to recognize a broken link or an empty img tag, how can I check if the image is displayable? I also tried the onError event but it isn't fired.

JJJ
  • 32,902
  • 20
  • 89
  • 102
Stefano Giacone
  • 2,016
  • 3
  • 27
  • 50
  • 1
    I'd suggest doing this in PHP before you even send it to the browser, avoid the hassle (and people with JS turned off or blocked, though few) – LeonardChallis May 14 '13 at 06:45
  • 2
    @shin: The fileSize isn't the problem: I have smaller images that can be displayed. Leonard: even in php I don't know how to check. Btw this page is not for the crowd, so I don't care about different clients – Stefano Giacone May 14 '13 at 06:48
  • 2
    Try [this](http://stackoverflow.com/questions/6568247/is-there-any-way-to-have-php-detect-a-corrupted-image) or [this](http://stackoverflow.com/questions/11928878/detect-image-corrupt-or-truncated-in-firefox) – forseta May 14 '13 at 06:53
  • @ES: it isn't really like this. I don't want to know why the images have problems or what the problems are. But discover corrupted images... There must be a way – Stefano Giacone May 14 '13 at 07:18
  • If you want to do it client-side, you could check image size (img.clientHeight>0 && img.clientHeight>0) on image load event. I think on a broken image such sizes will be zero, so you can hide / remove them from your page. – LittleSweetSeas May 14 '13 at 07:34

1 Answers1

1

IMO, you’d be better off checking this on the server, using PHP. PHP has several functions to load images, such as imagecreatefromjpeg and imagecreatefrompng. Both these functions will return FALSE if there’s an error loading the image. Getting FALSE will tell you that it’s not a valid image.

Martijn
  • 13,225
  • 3
  • 48
  • 58