Image Validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Image Validation

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 @
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
Post Reply