FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How to detect a file is that jpeg image or gif image ?





Philip
Is there a way to detecting that a file is a image file or not then jpeg or gif then ?

some how i create a index.php?id=www.frihost.com/image.jpg

that contain

Code:
         $filename=$_GET["id"];
            header('Content-type: image/jpeg');
            list($width_orig, $height_orig) = getimagesize($filename);
            $width = 80;
            $height = 80;
            $width = $width_orig*($height/$height_orig);
            $height = $height_orig*($height/$height_orig);
            if ($width && ($width_orig < $height_orig)) {
               $width = ($height / $height_orig) * $width_orig;
            } else {
               $height = ($width / $width_orig) * $height_orig;
            }
            $image_p = imagecreatetruecolor($width, $height);
            $image = imagecreatefromjpeg($filename);
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
            imagejpeg($image_p, null, 100);


that's for only handling jpeg image, but since my member upload a moving gif image i get a error, how can i handle this error ? thanks... Smile
hsadmin
Philip wrote:
Is there a way to detecting that a file is a image file or not then jpeg or gif then ?

some how i create a index.php?id=www.frihost.com/image.jpg

that contain

Code:
         $filename=$_GET["id"];
            header('Content-type: image/jpeg');
            list($width_orig, $height_orig) = getimagesize($filename);
            $width = 80;
            $height = 80;
            $width = $width_orig*($height/$height_orig);
            $height = $height_orig*($height/$height_orig);
            if ($width && ($width_orig < $height_orig)) {
               $width = ($height / $height_orig) * $width_orig;
            } else {
               $height = ($width / $width_orig) * $height_orig;
            }
            $image_p = imagecreatetruecolor($width, $height);
            $image = imagecreatefromgifani($filename);
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
            imagegifani($image_p, null, 100);


that's for only handling jpeg image, but since my member upload a moving gif image i get a error, how can i handle this error ? thanks... Smile


Hmmmm.. Try that code
Philip
what do you mean by hmm.. try that code ? O.o"
that;s code for handling image/jpeg.. --"

i need that a code can compare gif or jpeg files --"
btw i try using gif version but it seem it lost the animation, any comment ?
Karrde
You could do it quick and dirty like this:

Code:

$filename=$_GET["id"];
$ext = strrchr($filename, ".");
if ($ext == ".jpg") {
    // do jpg code
} elseif ($ext == ".gif") {
    // do gif code
} else {
    // do "this aint an image" code
}


That should work, assuming the file extension is set right.
<h1>
^^ on the same lines
Code:

$filename=$_GET["id"];
$img = getimagesize($filename)
$imgtype = $img['mime'];

if ($imgtype == "image/gif") {
    // do jpg code
} elseif ($imgtype== "image/jpeg") {
    // do gif code
} else {
    // do "this aint an image" code
}
Karrde
Yeah, that second code's better, as it allows for .jpg and .jpeg files properly, and doesn't hiccup on bad extensions.

Dunno about the whole animated gif thing though - I imagine thumbnailing one will break the animation, and theres probably no way around it.
Philip
i get this error

Warning: filesize() [function.filesize]: stat failed for http://www.frihost.com/forums/images/avatars/118141599542cc96a3e7f01.jpg in

on code
Code:

                           $img = getimagesize($_POST["txtlink"]);
                           $imgtype = $img['mime'];
                           $ke=1;
                           if ($imgtype == "image/gif") {
                              $filename=$filename.".gif";
                           }
                           if ($imgtype== "image/jpeg") {
                              $filename=$filename.".jpg";
                           }                                                       
Philip
if there is a filename that not contain a extension, can we know is it gif or jpeg images ?

and is there a function to check a file is a image or not ?

like is_image($_FILES['txtfile'])

thanks
hexkid
Philip wrote:
if there is a filename that not contain a extension, can we know is it gif or jpeg images ?

and is there a function to check a file is a image or not ?

like is_image($_FILES['txtfile'])

thanks


http://www.php.net/manual/en/ref.mime-magic.php
http://www.php.net/manual/en/ref.fileinfo.php

Failing that you can, perhaps, open the file, read the first few bytes, and try to determine its type from there ...
Code:
<?php

#####################
#### NOT TESTED! ####
#####################

function file_type($filename) {
  $f = fopen($filename, 'rb');
  $data = fread($f, 4);
  fclose($f);
  if ($data == "\xff\xd8\xff\xe0") { return 'JPG without EXIF'; }
  if ($data == "\xff\xd8\xff\xe1") { return 'JPG with EXIF'; }
  if ($data == "BM\xde\x01") { return 'BMP'; }
  if ($data == "GIF8") { return 'gif'; }
  // ...
  return '(unknown)';
}
?>
Maryndor
well that last reply is really original and maybe can do the job perfectly,
I wonder is this will work as well too:

Code:

define("GIS_GIF",     1);
define("GIS_JPG",     2);
define("GIS_PNG",     3);

if ($imginfo[2] != GIS_GIF && $imginfo[2] != GIS_JPG && $imginfo[2] != GIS_PNG) {
 die("I will die because do not support other image format");
}
Philip
Code:

#####################
#### NOT TESTED! ####
#####################


what was that code for ?? Embarassed

wew i am still dont get
what's was that mean ?
hexkid
Philip wrote:
hexkid wrote:
Code:

#####################
#### NOT TESTED! ####
#####################


what was that code for ?? Embarassed

wew i am still dont get
what's was that mean ?


It means that I didn't test run that script, but I expect it to work.
It also means that if you need something like that and want to use my script, you should run your tests on it.

EDIT: It further means that I don't know the internal format for the image files and that the "signatures" I wrote were from a small sample of images on my disk.
Philip
Maryndor wrote:
well that last reply is really original and maybe can do the job perfectly,
I wonder is this will work as well too:

Code:

define("GIS_GIF",     1);
define("GIS_JPG",     2);
define("GIS_PNG",     3);

if ($imginfo[2] != GIS_GIF && $imginfo[2] != GIS_JPG && $imginfo[2] != GIS_PNG) {
 die("I will die because do not support other image format");
}


i prefer this one since it;s seem more easily for me Razz
but define GIS_GIF a variable that contain 1and next...

then using if()

how would that be working ?
Maryndor
Oh of course, it's my mistake. Forgot following before the if.

Code:
$imginfo = getimagesize("\\path_to_my\image.jpg");


it's pretty much like <h1>'s code. hope it's working.
PatTheGreat42
If you just want to see if a file is a .jpeg or .gif file, why not just glance at the file extension?
Philip
then a user just can rename a what ever file to gif or jpeg extension, then upload it --'
Related topics
GIF and JPEG.
File upload with PHP, Build an upload database for your site
File sizes... using multiple or single, gif or jpeg?
Google's Secret Revealed
php image exif functions
Free image hosting-Imagezones.com unlimited Storage Space
[PHP] Error link
Upload Script
Can l get a css document to
File Hub / Image Hub :: A Reliable New File Hosting Service
Uploading and resizing an image
Image Script not working.
Resize image in PNG Problem
IE8 not allowing new random image on each page (PHP and CSS)
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.