Jump to content

how to change black background of thumbnail???


sheraz

Recommended Posts

hi, i am trying to generate image gallery. but the problem is that when i upload the png files

its thumbnail is generated with black background. JPEG an GIF thumbnail are generated successfully. i want to make background transparent. is there any solution that how i can get rid of it. :(

 

i am using imagecreatetruecolor($thumbwidth, $thumbheight ); to generate thumbnail

$black = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $black);

Resize example

<?php
function setTransparency($new_image,$image_source){
            $transparencyIndex = imagecolortransparent($image_source);
            $transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255);
            
            if ($transparencyIndex >= 0) {
                $transparencyColor    = imagecolorsforindex($image_source, $transparencyIndex);   
            }
           
            $transparencyIndex    = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']);
            imagefill($new_image, 0, 0, $transparencyIndex);
             imagecolortransparent($new_image, $transparencyIndex);
       
    }
?>


Sample Usage: (resizing)

<?php
$image_source = imagecreatefrompng('test.png');
$new_image = imagecreatetruecolor($width, $height);
setTransparency($new_image,$image_source);
imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); 

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.