473,390 Members | 817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

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 11819
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Dean Speir | last post by:
Hi... I've been referred to this Newsgroup by the W3C Markup Validator FAQ. I've been happily using this Validator <http://validator.w3.org> for the past 18 months with great success, but...
3
by: Michael Kalina | last post by:
Hi! On my website (http://michaelsremarks.blogspot.com/) I use a so called flickr-badge. That's some source-code that makes it possible to show a random image out of my gallery on flickr. Now...
7
by: fingermark | last post by:
I'm writing an upload script and would like to know what is the safest way to validate a file type that is being uploaded to a server? I am accepting just bmp, jpg, png, and gif. Here are is...
0
by: SHC | last post by:
Hi all, I have a VC++ .NET 2003 - Windows XP Pro PC. I created a Win32 console application in my VC++ .NET 2003 and copied validateDOM.cpp, books.xml and books.xsd (see the attached files below)...
3
by: Martin | last post by:
Hi, I am implemeting a form in asp.net. The form is quite large and the validation is reasonably complex, so I have decieded to implement my own validation rather than use any custon...
2
by: CDWaddell | last post by:
I'm trying to write some code to validate if an image is present on the server. If the image is missing then I want to display out noimage.gif file. I have a website that has thousands of...
6
by: Jonny | last post by:
Hi, I'm trying to validate my xml against a xsd but I can't get it to work. Originally, I wanted to validate an xml string but since I didn't get that to work I tried to validate an xml file...
15
hsriat
by: hsriat | last post by:
if (!($_FILES == "image/gif" || $_FILES == "image/jpg" || $_FILES == "image/jpeg" || $_FILES == "image/png")) die ('Invalid File Type'); This displays error when a file other than image file is...
3
by: Jeff | last post by:
I know how to use php/mysql, but the html aspect of what Im trying to do (if its even possible this way) is what Im unsure of. I want to have a button that a user can hit to valid mysql info...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.