• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

extend code to support GIF PNG and JPG file types

im looking to extend my code so i can upload, and create thumbnails from .png, and .gif.

the current code just works for .jpg as you can see.  what do i need to do so it supports .gif and .png??

im not willing to pay for scripts so any links to sites where i have to pay to download isnt going to get you points...

<?php
if(!empty($_POST['submit']))
{
      $directory = "";
      $directory = "media/";

      $newImagename = "";
      $newImagename = "image1.jpg";

      $newThumbnailImagename = "";
      $newThumbnailImagename = "thumbnail_image1.jpg";

      $extension_arr = array
      (
            'image/gif',
            'image/jpeg',
            'image/jpg',
            'image/png'
      );

      if (in_array($_FILES['imagefile']['type'],$extension_arr))
      {
            $uploadedfile = $_FILES['imagefile']['tmp_name'];
            copy($uploadedfile,$directory.$newImagename) or die ("Could not copy");
            $src = imagecreatefromjpeg($uploadedfile);
            list($width,$height) = getimagesize($uploadedfile);
            $newwidth = 180;
            $newheight = ($height / $width) * 180;
            $tmp = imagecreatetruecolor($newwidth,$newheight);
            imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
            imagejpeg($tmp,$directory.$newThumbnailImagename,100);
            imagedestroy($src);
            imagedestroy($tmp);
      }
      else
      {
            echo "<br><br>";
            echo "Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")<br>";
      }
}
?>
0
ellandrd
Asked:
ellandrd
  • 11
  • 6
1 Solution
 
RoonaanCommented:
Hi, you could use

getimagesize($uploadedfile, $info);
/*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.*/


switch($info[2]) {
  case '1':
    $imageCreateFunc = 'imagecreatefromgif';
    $imageFunc = 'imagegif';
    break;
  case '2':
    $imageCreateFunc = 'imagecreatefromjpg';
    $imageFunc = 'imagejpeg';
    break;
  default:
    $imageCreateFunc = 'imagecreatefromjpg';
    $imageFunc = 'imagejpeg';
    break;
}  

And use:
$src = $imageCreateFunc($uploadedfile);
$imageFunc($tmp, $directory.$newThumbnailImagename,100);

Hope this helps.

-r-
0
 
RoonaanCommented:
You can also choose to always output as jpeg of course.

-r-
0
 
ellandrdAuthor Commented:
use this: getimagesize($uploadedfile, $info); in replace of what?

what is: $info[2]?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ellandrdAuthor Commented:
i see what you mean - sorry i took me a few mins to understand what you did...
0
 
RoonaanCommented:
Ah sorry, getimagesize call was additional to what you already had. It is a function to retrieve the type of image, such that you can determine which functions to use (imagecreatefromjpeg, imagecreatefrompng, imagecreatefromgif, imagecreatefrombmp, and so on).

-r-
0
 
ellandrdAuthor Commented:
is png:

case '3':
                        $imageCreateFunc = 'imagecreatefrompng';
                        $imageFunc = 'imagepng';
                        break;

its just you never put it in...?
0
 
RoonaanCommented:
Yes, your case '3' is correct. I assumed you would be able to add all image types you wanted to support. ;-)

-r-
0
 
ellandrdAuthor Commented:
>>I assumed you would be able to add all image types you wanted to support

ya i figured it out - at first i wasnt too sure if i would have the function names correct, but as it look i did!

thank you!
0
 
ellandrdAuthor Commented:
ok the switch statement keeps doing the default regardless of the image type?

0
 
RoonaanCommented:
Try echoing out $info;

print_r($info);

-r-
0
 
ellandrdAuthor Commented:
info[] is blank
0
 
ellandrdAuthor Commented:
fixed - cheers
0
 
ellandrdAuthor Commented:
>>You can also choose to always output as jpeg of course.

what do you mean - how?  it would be a nice option to have...
0
 
RoonaanCommented:
just always set $imageFunc = 'imagejpeg'; or drop the variable all together and use imagejpeg() at the bottom.

-r-
0
 
ellandrdAuthor Commented:
ok thanks
0
 
ellandrdAuthor Commented:
having a few problems creating .png's but i'll open another question, as its just not that...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 11
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now