which is best?

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
modplod
Forum Commoner
Posts: 45
Joined: Mon Feb 27, 2006 11:18 am

which is best?

Post by modplod »

I dont kow what will work best for me:

This

Code: Select all

$filenameE=explode(".",$name);
		if ($filenameE[1]="jpg"){$src_img=imagecreatefromjpeg($name);}
or this

Code: Select all

$filenameE=explode(".",$name);
		if (preg_match("/jpeg/",$filenameE[1])){$src_img=imagecreatefromjpeg($name);}
The code has to run the imagecreatefromjpeg function if the file type is jpg. I just wish to know what the best way of doing this is..
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Both of the above are flawed.

They assume files only have one dot in them ( ? someclass.class.php ?), the second one only looks for the string "jpeg" in the filename bit after the dot too.... that's a little pointless.

Try:

Code: Select all

$allowed = array('jpg', 'jpeg'); //Can add to this
$ext = substr($filename, strrpos($filename, '.')+1);
if (in_array($ext, $allowed)) //I never remember if haystack comes first or second 
{
    //OK
}
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

For you a regex/case insensitive search you might:

Code: Select all

$ext = substr($file, strrpos($file, '.')+1); 
					$expression ='#((jpg)|(jpeg)|(gif)|(giff)|(png))$#i';
					if(preg_match($expression, $ext, $return)){
						//Do your thing...
					}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'll vote "none of the above" Alex.

getimagesize() should be used to determine image type, not extension.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

yes as feyd said said...

get image size is the ONLY sure way to find out if it is really a picture. every other way would be a simple workaround like:

virus.ex.jpg...MSN has been blocking such crazy things for a while so remember that
modplod
Forum Commoner
Posts: 45
Joined: Mon Feb 27, 2006 11:18 am

Post by modplod »

feyd wrote:
getimagesize() should be used to determine image type, not extension.
Hi, I tryed for hours last night to us this, but with no more luck than before, I'm really new to this, sorry to ask, but could someone show me how I would use getimagesize() to find the image type and compare it to a list of supported image types?

Thanks
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

From the manual.
Returns an array with 4 elements. Index 0 contains the width of the image in pixels. Index 1 contains the height. Index 2 is a flag indicating the type of the image: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM. These values correspond to the IMAGETYPE constants that were added in PHP 4.3.0. Index 3 is a text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag.
So

Code: Select all

$img_details = getimagesize($filename);
if($img_details[2] == 2){
   echo "I'm a jpg";
}
modplod
Forum Commoner
Posts: 45
Joined: Mon Feb 27, 2006 11:18 am

Post by modplod »

Thanks neophyte, thats seems to do the job :

Code: Select all

$img_details = getimagesize($fileName);
		if($img_details[2] = 2){
			$src_img=imagecreatefromjpeg($fileName);
			$x=$img_details[0];
			$y=$img_details[1];
		}else{ echo "no usable image info found, ";}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

ahem

if($img_details[2] == 2){

You were missing the extra equal marks.

= is assignment operator
== is comparison operator
modplod
Forum Commoner
Posts: 45
Joined: Mon Feb 27, 2006 11:18 am

Post by modplod »

Jcart wrote:ahem

if($img_details[2] == 2){

You were missing the extra equal marks.

= is assignment operator
== is comparison operator

yer I found that shortly after trying to run the code again :)
Post Reply