masterexploder / PHPThumb

PHP Thumbnail & Image Manipulation Library
http://phpthumb.gxdlabs.com
980 stars 264 forks source link

Transparency using GD::pad (v2.0) #111

Open luke83 opened 10 years ago

luke83 commented 10 years ago

Is there a way to make the method use transparent background (or preserve it from original image) for the padded output image?

// this will show image with white background:
$thumb = new GD($imgPath);
$thumb->resize(40, 40);
$thumb->pad(40, 40, [255, 255, 255]);
$thumb->save($thumbFilePath);

// this (I WOULD LIKE) will show image with transparent background:
$thumb = new GD($imgPath);
$thumb->resize(40, 40);
$thumb->pad(40, 40, false); // or any other call format to specify we wont transparent background
$thumb->save($thumbFilePath);

this is how i patched GD class, i don't know if it is the correct way, also tried with preserveAlpha with no result!

// near line 147
        if (!$color) {
            imagealphablending($this->workingImage, false);
            $fillColor = imagecolorallocatealpha($this->workingImage, 0, 0, 0, 127);
            imagefill($this->workingImage, 0, 0, $fillColor);
            imagesavealpha($this->workingImage, true);
        } else {
            // create the fill color
            $fillColor = imagecolorallocate(
                            $this->workingImage, $color[0], $color[1], $color[2]
            );

            // fill our working image with the fill color
            imagefill(
                            $this->workingImage, 0, 0, $fillColor
            );
        }
bacinsky commented 10 years ago

Hi, there is a fix:

public function pad($width, $height, $color = array(255, 255, 255, 127)) // <- add the alpha value
{
    // ...

   // (add following)
   imagealphablending($this->workingImage, false);
   imagesavealpha($this->workingImage, true);

    // create the fill color
    $fillColor = imagecolorallocatealpha(    // <- change to ...allocatealpha
        $this->workingImage,
        $color[0],
        $color[1],
        $color[2],
        $color[3]    // <- add the alpha value param
    );