Dragen Posted November 5, 2009 Share Posted November 5, 2009 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? Quote Link to comment Share on other sites More sharing options...
clay1 Posted November 6, 2009 Share Posted November 6, 2009 You could try reading this: http://devzone.zend.com/article/1269 http://php.net/manual/en/function.imagepng.php Quote Link to comment Share on other sites More sharing options...
Dragen Posted November 6, 2009 Author Share Posted November 6, 2009 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 Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 6, 2009 Share Posted November 6, 2009 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 Quote Link to comment Share on other sites More sharing options...
Dragen Posted November 6, 2009 Author Share Posted November 6, 2009 Okay, thanks for the replies! Its working fine now, the problem was on my end. I'd missed out a call to a function so it wasn't doing everything... The code I had below works perfectly. Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.