Jump to content

[SOLVED] Change image filetype on upload?


Dragen

Recommended Posts

Hi,

I've got a form which uploads an image to the server.

Is it possible to change the image type during the upload?

 

I want the user to be able to upload images that are .gif .jpg or .png, but when uploaded, if they are .gif or .jpg, have been changed to .png.

 

Is this possible?

Link to comment
https://forums.phpfreaks.com/topic/180485-solved-change-image-filetype-on-upload/
Share on other sites

Thanks for that, but I'm getting some strange results.

 

Here's my code:

	/**
 * Creates a base image from the img url and returns it.
 */
private function createBase(){
	$src_img = NULL;
	switch($this->getType()){
		case 'png':
			$src_img = imagecreatefrompng($this->getImage());
		break;
		case 'gif':
			$src_img = imagecreatefromgif($this->getImage());
		break;
		default:
			$src_img = imagecreatefromjpeg($this->getImage());
		break;
	}
	return $src_img;
}

/**
 * Creates the resampled image that will be used
 */
private static function resampleImage($src_img, $dimg){
	$w = imagesx($src_img);
	$h = imagesy($src_img);
	imagecopyresampled($dimg, $src_img, 0, 0, 0, 0, $w, $h, $w, $h);
}

private function replaceImage(){
	if($this->getType() == 'png'){
		return true;
	}else{
		//checks images exists and creates the base source image
		if($this->imageExists() && ($src_img = $this->createBase())){
			$dimg = imagecreatetruecolor(imagesx($src_img), imagesy($src_img));
			imagealphablending($dimg, false);
			$colorTransparent = imagecolorallocatealpha($dimg, 0, 0, 0, 127);
			imagefill($dimg, 0, 0, $colorTransparent);
			imagesavealpha($dimg, true);

			//resamples the image
			$this->resampleImage($src_img, $dimg);

			//getImageNoType gets the image path, without the filetype, so I can add .png to the end
			if(imagepng($dimg, $this->getImageNoType() . '.png')){
				@imagedestroy($dimg);
				@imagedestroy($src_img);
				unlink($this->getImage());
				return true;
			}
		}
		return false;
	}
}

 

At first glance this appears to work, as it successfully creates the image, with a .png extension, but when trying to access it through PHP GD library, using imagecreatefrompng(), it throws up the following error:

Warning: imagecreatefrompng() [function.imagecreatefrompng]: 'C:/Program Files/wamp/www/images/links/2_5.png' is not a valid PNG file in C:\Program Files\wamp\www\incs\thumbs.php on line 60

 

So it appears that it has saved the file, but not encoded it as a png.

 

 

EDIT:

In fact, checking it in firefox, pageinfo, it states the filetype is .jpg

use the imagepng after that

   /**
    * Creates a base image from the img url and returns it.
    */
   private function createBase(){
      $src_img = NULL;
      switch($this->getType()){
         case 'png':
            $src_img = imagecreatefrompng($this->getImage());
         break;
         case 'gif':
            $src_img = imagecreatefromgif($this->getImage());
         break;
         default:
            $src_img = imagecreatefromjpeg($this->getImage());
         break;
      }
      return imagepng($src_img);
   }

 

hope it works  :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.