sbourdon Posted March 22, 2007 Share Posted March 22, 2007 Hello, I've just installed a modification on my board to resize uploaded pics and the only problem I have is that GIF images lose their transparency... How can I fix this? Here's part of the code where images get resized: // iMinimize_MOD - start function getsize($url) { global $userdata; // Maximum width and height for inpost images $iWidth = 640; $iHeight = 640; // JPEG Quality $quality = 80; $base = basename($url); $base_ext = substr($base, 0, -4); $id = $userdata['username'] . "_" . $base_ext; // broken_link $broken_link = $phpbb_root_path . "images/broken_link.gif"; if(@!$size = getimagesize($url)) { return $broken_link; } $img_size = getimagesize($url); $real_width = $img_size[0]; $real_height = $img_size[1]; // Find the height or width if 0 is the value. if($iHeight == 0) { $percent = $size[1] / $size[0] * 100; $iHeight = round($iWidth / 100 * $percent, 0); } if($iWidth == 0) { $percent = $size[0] / $size[1] * 100; $iWidth = round($iHeight / 100 * $percent, 0); } // Look up the extension of the image and use the right functions. $cut_url = strlen($url) - 3; $ext = substr($url, $cut_url); // .jpg if($ext == "jpg") { $im = imagecreatefromjpeg($url); $ow = imagesx( $im ); $oh = imagesy( $im ); if($real_width > $iWidth OR $real_height > $iHeight) { $wscale = $iWidth / $ow; $hscale = $iHeight / $oh; } else { $wscale = 1; $hscale = 1; } $scale = ( $hscale < $wscale ? $hscale : $wscale ); $nw = round( $ow * $scale, 0 ); $nh = round( $oh * $scale, 0 ); $dstim = imagecreatetruecolor( $nw, $nh ); imagecopyresampled( $dstim, $im, 0, 0, 0, 0, $nw ,$nh ,$ow ,$oh ); imagejpeg( $dstim, $phpbb_root_path . "images/IMG/" . $id . ".jpg", $quality); imagedestroy( $dstim ); $thumb = $id . ".jpg"; // .gif } elseif($ext == "gif") { $im = imagecreatefromgif($url); $ow = imagesx( $im ); $oh = imagesy( $im ); if($real_width > $iWidth OR $real_height > $iHeight) { $wscale = $iWidth / $ow; $hscale = $iHeight / $oh; } else { $wscale = 1; $hscale = 1; } $scale = ( $hscale < $wscale ? $hscale : $wscale ); $nw = round( $ow * $scale, 0 ); $nh = round( $oh * $scale, 0 ); $dstim = imagecreatetruecolor( $nw, $nh ); imagecopyresampled( $dstim, $im, 0, 0, 0, 0, $nw ,$nh ,$ow ,$oh ); imagegif( $dstim, $phpbb_root_path . "images/IMG/" . $id . ".gif"); imagedestroy( $dstim ); $thumb = $id . ".gif"; // .png } elseif($ext == "png") { $im = imagecreatefrompng($url); $ow = imagesx( $im ); $oh = imagesy( $im ); if($real_width > $iWidth OR $real_height > $iHeight) { $wscale = $iWidth / $ow; $hscale = $iHeight / $oh; } else { $wscale = 1; $hscale = 1; } $scale = ( $hscale < $wscale ? $hscale : $wscale ); $nw = round( $ow * $scale, 0 ); $nh = round( $oh * $scale, 0 ); $dstim = imagecreatetruecolor( $nw, $nh ); imagecopyresampled( $dstim, $im, 0, 0, 0, 0, $nw ,$nh ,$ow ,$oh ); imagepng( $dstim, $phpbb_root_path . "images/IMG/" . $id . ".png"); imagedestroy( $dstim ); $thumb = $id . ".png"; // broken_link } else { return $broken_link; } $iUrl = "[" . $phpbb_root_path . "images/IMG/" . $thumb . "]" . $url; return $iUrl; } // iMinimize_MOD - end Would someone know how to keep transparency for resized GIF images? Thanks a lot! Link to comment https://forums.phpfreaks.com/topic/43830-keep-transparency-on-resized-gif-images/ Share on other sites More sharing options...
Lumio Posted March 22, 2007 Share Posted March 22, 2007 Hi! You can use imagecolortransparent to make the black background transparent. (http://us2.php.net/manual/en/function.imagecolortransparent.php) But thats not the best way. Maybe you create a color, that does not exist in GIF-Images and than you can set that color as transparent. Link to comment https://forums.phpfreaks.com/topic/43830-keep-transparency-on-resized-gif-images/#findComment-212764 Share on other sites More sharing options...
sbourdon Posted March 22, 2007 Author Share Posted March 22, 2007 Thanks! I'm new at this so I guess I'll have to read some more... If you or anyone else has other hints and tips, don't hesitate to share them! Link to comment https://forums.phpfreaks.com/topic/43830-keep-transparency-on-resized-gif-images/#findComment-213040 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.