Page 1 of 1

Image Validation

Posted: Sun Apr 22, 2007 10:17 pm
by superdezign
So in all this time that I've been missing from the forums, I've been developing a few websites. Gets a little busy when it starts to warm up. Anyway, I've been playing around with the GD library a bit more and this time, I decided not to use any tutorials. NOW it makes sense. PHP manual is the greatest. :)

But I've got a problem. I've been doing a lot of testing and purposely hacking my site to pieces, finding and fixing little bugs, and I've ran into one that bothers me.

When uploading a corrupt JPEG, I get a warning from imagecreatefromjpeg(). According to GD lib, it's an "unrecoverable error." It's only a warning and doesn't affect my scripts in any way, and it'll disappear when I go live and turn off printed error_reporting, but I've just got to know... Can I validate images?

Image validation gives me an error on corrupt files and when refreshing the page with an upload request, I get a "Read error!" warning from getimagesize().

I use $_FILES validation, file_exists($_FILES['foo']['tmp_name']), and getimagesize() which was, hopefully, going to be my last step of validation. It works, but throws a warning. Is there a way to find the error BEFORE throwing it into functions that like to throw warnings?

I've been going through the manual all day and can't seem to find the function I'm after. exif stuff seemed a little promising, but I haven't set it up on my server yet. I figured I'd put my pride aside for a second and ask.

Posted: Sun Apr 22, 2007 10:25 pm
by John Cartwright
I always use getimagsize() right away to determine if the file is a valid image. Not quite sure why you want this as your last step.

Regardless, you can always suppress error using @

Posted: Mon Apr 23, 2007 6:09 am
by superdezign
A fine suggestion. Thank you. :)

I used getimagesize() after file_exists() because it would throw me the error only during the refresh, at which point I assumed that the temporary file was no longer available to be altered. However, file_exists() says it's there, and getimagesize() doesn't.