Hello,

I have a functioning site with image uploading using GD libraries. However, I am trying to upload GIF files and keep them as GIF files without any resizing/formatting. Everytime I upload a GIF, it automatically turns it into a jpg regardless of its size. Is there an easy way for me to make any adjustment to do this?

Below is the full code that is being used to upload and resize the images. I also included the snippet that i think where it needs to be changed. If anyone could help me with this, I would really appreciate it.

SNIPPET

	  // RESIZE IMAGE AND PUT IN USER DIRECTORY
	  switch($this->file_ext) {
	      case "gif":
	      $file = imagecreatetruecolor($width, $height);
	      $new = imagecreatefromgif($this->file_tempname);
	      $kek=imagecolorallocate($file, 255, 255, 255);
	      imagefill($file,0,0,$kek);
	      imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);
	      imagejpeg($file, $photo_dest);
	      ImageDestroy($new);
	      ImageDestroy($file);
	      break;

FULL

<?


//  THIS CLASS CONTAINS UPLOAD-RELATED METHODS.
//  IT IS USED DURING THE UPLOAD OF A FILE.
//  METHODS IN THIS CLASS:
//    new_upload()
//    upload_file()
//    upload_photo()
//    image_resize_on()
//    ConvertBMP2GD()
//    imagecreatefrombmp()


class se_upload {

// INITIALIZE VARIABLES
var $is_error = 0;		// DETERMINES WHETHER THERE IS AN ERROR OR NOT
var $error_message = "";	// CONTAINS RELEVANT ERROR MESSAGE

var $file_name;			// CONTAINS NAME OF UPLOADED FILE
var $file_type;			// CONTAINS UPLOADED FILE MIME TYPE
var $file_size;			// CONTAINS UPLOADED FILE SIZE
var $file_tempname;		// CONTAINS TEMP NAME OF UPLOADED FILE
var $file_error;		// CONTAINS UPLOADED FILE ERROR
var $file_ext;			// CONTAINS UPLOADED FILE EXTENSION
var $file_width;		// CONTAINS UPLOADED IMAGE WIDTH
var $file_height;		// CONTAINS UPLOADED IMAGE HEIGHT

var $is_image;			// DETERMINES WHETHER FILE IS AN IMAGE OR NOT
var $file_maxwidth;		// CONTAINS THE MAXIMUM WIDTH OF AN UPLOADED IMAGE
var $file_maxheight;		// CONTAINS THE MAXIMUM HEIGHT OF AN UPLOADED IMAGE






// THIS METHOD SETS INITIAL VARS SUCH AS FILE NAME
// INPUT: $file REPRESENTING THE NAME OF THE FILE INPUT
//	  $file_maxsize REPRESENTING THE MAXIMUM ALLOWED FILESIZE
//	  $file_exts REPRESENTING AN ARRAY OF LOWERCASE ALLOWABLE EXTENSIONS
//	  $file_types REPRESENTING AN ARRAY OF LOWERCASE ALLOWABLE MIME TYPES
//	  $file_maxwidth (OPTIONAL) REPRESENTING THE MAXIMUM WIDTH OF THE UPLOADED PHOTO
//	  $file_maxheight (OPTIONAL) REPRESENTING THE MAXIMUM HEIGHT OF THE UPLOADED PHOTO
// OUTPUT: 
function new_upload($file, $file_maxsize, $file_exts, $file_types, $file_maxwidth = "", $file_maxheight = "") {
  global $class_upload;

  // GET FILE VARS
  $this->file_name = $_FILES[$file]['name'];
  $this->file_type = strtolower($_FILES[$file]['type']);
  $this->file_size = $_FILES[$file]['size'];
  $this->file_tempname = $_FILES[$file]['tmp_name'];
  $this->file_error = $_FILES[$file]['error'];
  $this->file_ext = strtolower(str_replace(".", "", strrchr($this->file_name, "."))); 

  $file_dimensions = @getimagesize($this->file_tempname);
  $this->file_width = $file_dimensions[0];
  $this->file_height = $file_dimensions[1];
  if($file_maxwidth == "") { $file_maxwidth = $this->file_width; }
  if($file_maxheight == "") { $file_maxheight = $this->file_height; }
  $this->file_maxwidth = $file_maxwidth;
  $this->file_maxheight = $file_maxheight;

  // ENSURE THE FILE IS AN UPLOADED FILE
  if(!is_uploaded_file($this->file_tempname)) { $this->is_error = 1; $this->error_message = $class_upload[1]; }

  // CHECK THAT FILESIZE IS LESS THAN GIVEN FILE MAXSIZE
  if($this->file_size > $file_maxsize) { $this->is_error = 1; $this->error_message = $class_upload[2]; }

  // CHECK EXTENSION OF FILE TO MAKE SURE ITS ALLOWED
  if(!in_array($this->file_ext, $file_exts)) { $this->is_error = 1; $this->error_message = $class_upload[3]; }

  // CHECK MIME TYPE OF FILE TO MAKE SURE ITS ALLOWED
  if(!in_array($this->file_type, $file_types)) { $this->is_error = 1; $this->error_message = $class_upload[3]; }

  // DETERMINE IF FILE IS A PHOTO (AND IF GD CAN BE USED)
  if($file_dimensions !== FALSE & in_array($this->file_ext, Array('gif', 'jpg', 'jpeg', 'png', 'bmp')) !== FALSE) {
    $this->is_image = 1;
    // ENSURE THE UPLOADED FILE IS NOT LARGER THAN MAX WIDTH AND HEIGHT IF GD IS NOT AVAILABLE
    if(!$this->image_resize_on()) {
      $this->is_image = 0;
      if($file_width > $file_maxwidth OR $file_height > $file_maxheight) { $this->is_error = 1; $this->error_message = $class_upload[4]; }
    }
  } else {
    $this->is_image = 0;
  }


} // END new_upload() METHOD









// THIS METHOD UPLOADS A FILE
// INPUT: $file_dest REPRESENTS THE DESTINATION OF THE UPLOADED FILE
// OUTPUT: BOOLEAN INDICATING WHETHER UPLOAD SUCCEEDED OR FAILED
function upload_file($file_dest) { 
  global $class_upload;

  // TRY MOVING UPLOADED FILE, RETURN ERROR UPON FAILURE
      if(!move_uploaded_file($this->file_tempname, $file_dest)) { 
    $this->is_error = 1; $this->error_message = $class_upload[1]; 
    return false;
  } else {
    chmod($file_dest, 0777);
    return true;
  }

} // END upload_file() METHOD









// THIS METHOD UPLOADS A PHOTO
// INPUT: $photo_dest REPRESENTS THE DESTINATION OF THE UPLOADED PHOTO
//	  $file_maxwidth (OPTIONAL) REPRESENTING THE MAXIMUM WIDTH OF THE UPLOADED PHOTO
//	  $file_maxheight (OPTIONAL) REPRESENTING THE MAXIMUM HEIGHT OF THE UPLOADED PHOTO
// OUTPUT: BOOLEAN INDICATING WHETHER UPLOAD SUCCEEDED OR FAILED
function upload_photo($photo_dest, $file_maxwidth = "", $file_maxheight = "") { 

  // SET MAX WIDTH AND HEIGHT
  if($file_maxwidth == "") { $file_maxwidth = $this->file_maxwidth; }
  if($file_maxheight == "") { $file_maxheight = $this->file_maxheight; }

  // CHECK IF DIMENSIONS ARE LARGER THAN ADMIN SPECIFIED SETTINGS
  // AND SET DESIRED WIDTH AND HEIGHT
  if($this->file_width > $file_maxwidth | $this->file_height > $file_maxheight) { 
    if($this->file_height > $file_maxheight) {
      $width = ($this->file_width)*$file_maxheight/($this->file_height);
      $height = $file_maxheight;
    }
    if($this->file_width > $file_maxwidth) {
      $height = ($this->file_height)*$file_maxwidth/($this->file_width);
      $width = $file_maxwidth;
    }
  } else {
    $width = $this->file_width;
    $height = $this->file_height;
  }


  // RESIZE IMAGE AND PUT IN USER DIRECTORY
  switch($this->file_ext) {
      case "gif":
      $file = imagecreatetruecolor($width, $height);
      $new = imagecreatefromgif($this->file_tempname);
      $kek=imagecolorallocate($file, 255, 255, 255);
      imagefill($file,0,0,$kek);
      imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);
      imagejpeg($file, $photo_dest);
      ImageDestroy($new);
      ImageDestroy($file);
      break;
    case "bmp":
      $file = imagecreatetruecolor($width, $height);
      $new = $this->imagecreatefrombmp($this->file_tempname);
      for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }
      imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height); 
      imagejpeg($file, $photo_dest);
      ImageDestroy($new);
      ImageDestroy($file);
      break;
    case "jpeg":
    case "jpg":
      $file = imagecreatetruecolor($width, $height);
      $new = imagecreatefromjpeg($this->file_tempname);
      for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }
      imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);
      imagejpeg($file, $photo_dest);
      ImageDestroy($new);
      ImageDestroy($file);
      break;
    case "png":
      $file = imagecreatetruecolor($width, $height);
      $new = imagecreatefrompng($this->file_tempname);
      for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }
      imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height); 
      imagejpeg($file, $photo_dest);
      ImageDestroy($new);
      ImageDestroy($file);
      break;
  } 

  chmod($photo_dest, 0777);

  return true;

} // END upload_photo() METHOD









// THIS METHOD CHECKS FOR NECESSARY IMAGE RESIZING SUPPORT
// INPUT: 
// OUTPUT: BOOLEAN INDICATING WHETHER GD CAN BE USED TO RESIZE IMAGES 
function image_resize_on() {

  // CHECK IF GD LIBRARY IS INSTALLED
  if( !is_callable('gd_info') ) return FALSE;

  $gd_info = gd_info();
  preg_match('/\d/', $gd_info['GD Version'], $match);
  $gd_ver = $match[0];

  if($gd_ver >= 2 AND $gd_info['GIF Read Support'] == TRUE AND $gd_info['JPG Support'] == TRUE AND $gd_info['PNG Support'] == TRUE) {
    return true;
  } else {
    return false;
  }
} // END image_resize_on() METHOD




}

?>

    This line is the one that creates the file:

    imagejpeg($file, $photo_dest);
    

    If youre original filetype was jpg then that's fine, but for GIF you should change it to [man]imagegif[/man]

      Thank you kindly for your help. I changed it to imagegif and it worked. Now gifs remain gifs when uploaded. However, the output file turns into (image.jpeg.gif) so that is actually a gif image but it has been named as a jpg. Any ideas on why that might be?

        Try reading the documentation for imagegif and look at your code. You can figure this out.

          Man oh man. I have been trying to figure this out for the last two hours. I have tried removing and plugging code and I still cannot figure out why it is adding ".jpg" "to the filename while keeping the file type Gif. It is not a huge deal considering I have the gif images uploading now but I really would like to figure this out.

          If you could help me out, I would really appreciate it.:o

            You need to find where the $photo_dest variable is defined. (I don't see it in the code provided here.)

              Yep, you'll need to go find the code that calls the upload_photo function. It is dictating the filename.

                NogDog wrote:

                You need to find where the $photo_dest variable is defined. (I don't see it in the code provided here.)

                Thank you for your help. Could you please suggest any way to find this information. I guess I don't understand how the code knows where to look if it is not defined in that php file. The code I provided was all if it in that php file.

                  It is the first parameter for the upload_photo() function, so you need to find where that function is actuall called and see what is being used for that parameter, and perhaps have to backtrack to find where it is set. (Isn't debugging fun? 😉 )

                  I'm guessing that it might be in a separate file, which first includes/requires the file you posted here, then makes the upoad_photo() call.

                    Thank you for your help. 😃 My brain hurts x 100. 🙁

                    So I found the code in another file in the same directory. Here is it is. However, I still could not figure out what to tweak. This is the code I was looking for. It shows that it automatically creates a file name and throws a ".jpg" at the end. So the question is, how to I get it to throw other extensions to the end. I dont just want gif or png or jpg but all of them. I tried removing the ".jpg" and this caused it to produce files that had no extension.

                    	  // SET KEY VARIABLES
                    	  $file_maxsize = "4194304";
                    	  $file_exts = explode(",", str_replace(" ", "", strtolower($this->groupowner_level_info[level_group_photo_exts])));
                    	  $file_types = explode(",", str_replace(" ", "", strtolower("image/jpeg, image/jpg, image/jpe, image/pjpeg, image/pjpg, image/x-jpeg, x-jpg, image/gif, image/x-gif, image/png, image/x-png")));
                    	  $file_maxwidth = $this->groupowner_level_info[level_group_photo_width];
                    	  $file_maxheight = $this->groupowner_level_info[level_group_photo_height];
                    	  $photo_newname = "0_".rand(1000, 9999).".jpg";
                    	  $file_dest = $this->group_dir($this->group_info[group_id]).$photo_newname;
                    
                      $new_photo = new se_upload();
                      $new_photo->new_upload($photo_name, $file_maxsize, $file_exts, $file_types, $file_maxwidth, $file_maxheight);
                    

                    So a quick recap, I wanted to upload gifs and they came out as jpegs. I changed the php settings from imagejpg to imagegif. So now I have gif format images with a jpg extension. Browsers display the images just fine but I would like to have the appropiate extension that goes with the file. I guess if I cant figure this out, I will be stuck with gif images that have an .jpg extension.

                      Write a Reply...