Jump to content

PNG Transparency


drisate

Recommended Posts

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: 47002-amour.png

 

This is the image after it's passed in the thumnail script: 47002-amour.png~19

 

<?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

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

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.