Jump to content

Keep transparency on resized GIF images


sbourdon

Recommended Posts

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

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.

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.