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
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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.