Page 1 of 1
which is best?
Posted: Fri Mar 03, 2006 6:00 pm
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..
Posted: Fri Mar 03, 2006 6:14 pm
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
}
Posted: Fri Mar 03, 2006 6:59 pm
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...
}
Posted: Fri Mar 03, 2006 10:14 pm
by feyd
I'll vote "none of the above" Alex.
getimagesize() should be used to determine image type, not extension.
Posted: Fri Mar 03, 2006 10:35 pm
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
Posted: Sat Mar 04, 2006 10:11 am
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
Posted: Sat Mar 04, 2006 10:27 am
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";
}
Posted: Sat Mar 04, 2006 2:32 pm
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, ";}
Posted: Sat Mar 04, 2006 9:02 pm
by John Cartwright
ahem
if($img_details[2] == 2){
You were missing the extra equal marks.
= is assignment operator
== is comparison operator
Posted: Sat Mar 04, 2006 9:15 pm
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
