drisate Posted May 13, 2011 Share Posted May 13, 2011 Hey guys i have a script that create thumnail version of any JPG, PNG, GIF images. It works great but the only problem is it's not keeping the transparency. It adds a black background instead. I made a very big search and tryed at least 100 things and i keep gething the black background ... This is the original image with the transparancy: This is the image after it's passed in the thumnail script: <?php // ... // Create output image $outImg = imagecreatetruecolor ($outWidth, $outHeight); // Load src image switch($srcType) { case "png": $srcImg = imagecreatefrompng($uri); $background = imagecolorallocate($srcImg, 255, 255, 255); imagecolortransparent($srcImg, $background); imagealphablending($srcImg, true); imagesavealpha($srcImg, true); break; case "gif": $srcImg = imagecreatefromgif($uri); break; case "jpeg": $srcImg = imagecreatefromjpeg($uri); break; default: diewith("unsupported file type '$uri'"); }; // Resize image imagecopyresampled($outImg, $srcImg, 0, 0, 0, 0, $outWidth, $outHeight, $srcWidth, $srcHeight); // Save to cached thumb switch($srcType) { case "png": $res = imagepng($outImg, $cacheFile); break; case "gif": $res = imagegif($outImg, $cacheFile); break; case "jpeg": $res = imagejpeg($outImg, $cacheFile); break; default: diewith("unsupported file type '$uri'"); } //... ?> Link to comment https://forums.phpfreaks.com/topic/236313-png-transparency/ Share on other sites More sharing options...
Adam Posted May 13, 2011 Share Posted May 13, 2011 imagecreatetruecolor imagecreatetruecolor() returns an image identifier representing a black image of the specified size. You need to use imagecreate instead, which creates a blank image identifier. Link to comment https://forums.phpfreaks.com/topic/236313-png-transparency/#findComment-1214938 Share on other sites More sharing options...
drisate Posted May 13, 2011 Author Share Posted May 13, 2011 thanks but unfortunatly i get the same result <?php // ... // Compute name of cache image $cacheName = md5($uri).'-'.basename($uri).'#'.$outWidth.'x'.$outHeight; $cacheFile = dirname(__FILE__) . '/'. $CACHE_DIR . '/' . $cacheName; // If cache doesn't exist or too old, build it. if (!file_exists($cacheFile) or ($srcTime > filectime($cacheFile))) { if ($imgInfo[0]<$outWidth){$outWidth=$imgInfo[0];} if ($imgInfo[1]<$outHeight){$outHeight=$imgInfo[1];} // Create output image $outImg = imagecreate ($outWidth, $outHeight); // Load src image switch($srcType) { case "png": $srcImg = imagecreatefrompng($uri); $background = imagecolorallocate($srcImg, 255, 255, 255); imagecolortransparent($srcImg, $background); imagealphablending($srcImg, true); imagesavealpha($srcImg, true); break; case "gif": $srcImg = imagecreatefromgif($uri); break; case "jpeg": $srcImg = imagecreatefromjpeg($uri); break; default: diewith("unsupported file type '$uri'"); }; // Resize image imagecopyresampled($outImg, $srcImg, 0, 0, 0, 0, $outWidth, $outHeight, $srcWidth, $srcHeight); // Save to cached thumb switch($srcType) { case "png": $res = imagepng($outImg, $cacheFile); break; case "gif": $res = imagegif($outImg, $cacheFile); break; case "jpeg": $res = imagejpeg($outImg, $cacheFile); break; default: diewith("unsupported file type '$uri'"); } // Check result if (!$res) diewith("Unable to save thumb to '$cacheFile'. Check the access right of the HTTP server."); } // HTTP Header header("Content-Type:image/$srcType"); // Dump cache file readfile($cacheFile) or diewith("Unable to open cached thumb '$cacheFile'"); ?> Link to comment https://forums.phpfreaks.com/topic/236313-png-transparency/#findComment-1214940 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.