By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

Validate an image type using php

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.