Ninjakreborn Posted June 28, 2010 Share Posted June 28, 2010 <?php function resize($width,$height) { // Setup new image $new_image = imagecreatetruecolor($width, $height); if( $this->image_type == IMAGETYPE_JPEG ) { } elseif( $this->image_type == IMAGETYPE_GIF ) { imagealphablending($new_image, false); imagesavealpha($new_image,true); $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127); imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent); imagecolortransparent ( $new_image, $transparent); } elseif( $this->image_type == IMAGETYPE_PNG ) { // These parameters are required for handling PNG files. imagealphablending($new_image, false); imagesavealpha($new_image,true); $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127); imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent); } // Resize image imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } ?> The stuff for the PNG and JPG work perfectly...but the GIF is not working. I tried the same thing that I am doing for PNG files and it's not working. Any advice on how to resize a GIF file and have it show up correctly..the full sized image works fine...but not when it's resized... Quote Link to comment https://forums.phpfreaks.com/topic/206117-resize-gif-file-without-losing-transparency/ Share on other sites More sharing options...
litebearer Posted June 28, 2010 Share Posted June 28, 2010 Might look here... http://mediumexposure.com/smart-image-resizing-while-preserving-transparency-php-and-gd-library/ Quote Link to comment https://forums.phpfreaks.com/topic/206117-resize-gif-file-without-losing-transparency/#findComment-1078484 Share on other sites More sharing options...
Ninjakreborn Posted June 29, 2010 Author Share Posted June 29, 2010 Perfect. That led me to a function that I was able to cut up and use to fix my issue. Thanks, I didn't think I would ever get that working. I really appreciate that. Quote Link to comment https://forums.phpfreaks.com/topic/206117-resize-gif-file-without-losing-transparency/#findComment-1078688 Share on other sites More sharing options...
Ninjakreborn Posted June 29, 2010 Author Share Posted June 29, 2010 Just for your information this is what I ended up putting together. Based off what I had (that was already working for JPG and PNG files, I have taken stuff from the resource you pointed to and it started working. Below is the final function I am using to resize that seems to work across the board for all image types. <?php function resize($width,$height) { // Setup new image $new_image = imagecreatetruecolor($width, $height); if( $this->image_type == IMAGETYPE_JPEG ) { } elseif( $this->image_type == IMAGETYPE_GIF) { $trnprt_indx = imagecolortransparent($this->image); // If we have a specific transparent color if ($trnprt_indx >= 0) { // Get the original image's transparent color's RGB values $trnprt_color = imagecolorsforindex($this->image, $trnprt_indx); // Allocate the same color in the new image resource $trnprt_indx = imagecolorallocate($new_image, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); // Completely fill the background of the new image with allocated color. imagefill($new_image, 0, 0, $trnprt_indx); // Set the background color for new image to transparent imagecolortransparent($new_image, $trnprt_indx); } } elseif( $this->image_type == IMAGETYPE_PNG ) { // These parameters are required for handling PNG files. imagealphablending($new_image, false); imagesavealpha($new_image,true); $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127); imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent); } // Resize image imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/206117-resize-gif-file-without-losing-transparency/#findComment-1078692 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.