469,282 Members | 1,889 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Validate an image type using php

Does anyone know how to validate a particular image type using PHP? If I
have a file and I want to ensure that it is a jpeg, how would go about
it? A sort of is_jpeg or is_gif is what I'm seeking.
TIA
Nov 16 '06 #1
6 11627
Lorenzo Thurman wrote:
Does anyone know how to validate a particular image type using PHP? If I
have a file and I want to ensure that it is a jpeg, how would go about
it? A sort of is_jpeg or is_gif is what I'm seeking.
TIA
For each of the image specs (gif, tiff, jpg, etc)
the first few bytes identify what it is. If you can
binary read the first few bytes with PHP, then you
can get that info... and usually a lot of other
stuff (dimensions, pallet, (c), etc.)

Nov 16 '06 #2
Lorenzo Thurman wrote:
Does anyone know how to validate a particular image type using PHP? If I
have a file and I want to ensure that it is a jpeg, how would go about
it? A sort of is_jpeg or is_gif is what I'm seeking.
http://www.php.net/getimagesize

Not tested

<?php
$image_data = getimagesize('whatever') or die('Unable to check
image.');
if ($image_data[2] == 2) {
echo 'File is an image of type JPG.';
} else {
echo 'File is not an image or, if it\'s an image, it\'s not of type JPG';
}
?>

--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
Nov 16 '06 #3
Sanders Kaufman wrote:
Lorenzo Thurman wrote:
>Does anyone know how to validate a particular image type using PHP? If
I have a file and I want to ensure that it is a jpeg, how would go
about it? A sort of is_jpeg or is_gif is what I'm seeking.
TIA

For each of the image specs (gif, tiff, jpg, etc) the first few bytes
identify what it is. If you can binary read the first few bytes with
PHP, then you can get that info... and usually a lot of other stuff
(dimensions, pallet, (c), etc.)
Thanks for the reply. Do you know what I should check for? I know image
types often start with a unique binary header, but I don't know where to
find that info.
Nov 20 '06 #4
Pedro Graca wrote:
Lorenzo Thurman wrote:
>Does anyone know how to validate a particular image type using PHP? If I
have a file and I want to ensure that it is a jpeg, how would go about
it? A sort of is_jpeg or is_gif is what I'm seeking.

http://www.php.net/getimagesize

Not tested

<?php
$image_data = getimagesize('whatever') or die('Unable to check
image.');
if ($image_data[2] == 2) {
echo 'File is an image of type JPG.';
} else {
echo 'File is not an image or, if it\'s an image, it\'s not of type JPG';
}
?>
Thanks for the reply, but do you know where I can get this info for
other types?
Nov 20 '06 #5
Lorenzo Thurman wrote:
Sanders Kaufman wrote:
>For each of the image specs (gif, tiff, jpg, etc) the first few bytes
identify what it is. If you can binary read the first few bytes with
PHP, then you can get that info... and usually a lot of other stuff
(dimensions, pallet, (c), etc.)
Thanks for the reply. Do you know what I should check for? I know image
types often start with a unique binary header, but I don't know where to
find that info.
Whole books have been written on it, so there's more
than I can say here - but I can tell you how to dig
up the info yourself.

Here's the GIF specification:
"http://www.w3.org/Graphics/GIF/spec-gif89a.txt"

As you can see from the spec's sections 12-17, the
first few bytes contain meta-info about the image -
including the fact that it's a GIF. (You can grab
any old GIF and read it into PHP to see how the spec
plays out in the actual file.)

Once you master retrieving meta data about a GIF,
you can do the same thing with the specs for other
image formats, too - including binaries like
executables, ZIP's, and java or .NET assemblies.
Nov 20 '06 #6
Lorenzo Thurman wrote:
Pedro Graca wrote:
>Lorenzo Thurman wrote:
>>Does anyone know how to validate a particular image type using PHP? If I
have a file and I want to ensure that it is a jpeg, how would go about
it? A sort of is_jpeg or is_gif is what I'm seeking.

http://www.php.net/getimagesize
<snip contents="test example"/>
Thanks for the reply, but do you know where I can get this info for
other types?

<BOLD><FONT SIZE="VERY VERY LARGE"><RED><BLINK><BEEP_BEEP>

http://www.php.net/getimagesize

</BEEP_BEEP></BLINK></RED></FONT></BOLD>

--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
Nov 20 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Dean Speir | last post: by
3 posts views Thread by Michael Kalina | last post: by
7 posts views Thread by fingermark | last post: by
3 posts views Thread by Martin | last post: by
2 posts views Thread by CDWaddell | last post: by
6 posts views Thread by Jonny | last post: by
3 posts views Thread by Jeff | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.