sprain / class.Images.php

A PHP class to handle image manipulation
28 stars 18 forks source link

Watermark transparency #2

Open webbird opened 12 years ago

webbird commented 12 years ago

We discussed this on your HP some time ago. I maybe have a solution. Try this code:


public function writeWatermark($opacity=50, $marginH=0, $marginV=0, $positionWatermarkLeftRight="c", $positionWatermarkTopBottom="c"){

    //Watermark
    list($image_create_func, $image_save_func) = $this->Watermark->getFunctionNames();
    $watermark = $image_create_func($this->Watermark->getImage());

    //get base image
    list($image_create_func, $image_save_func) = $this->getFunctionNames();
    $baseImage = $image_create_func($this->image);

    imagealphablending($baseImage, true);
    imagesavealpha($baseImage, true);

    imagealphablending($watermark, true);
    imagesavealpha($watermark, true);

    // resize the watermark to fit original image size
    //imagecopy( $baseImage, $watermark, 0, 0, 0, 0, imagesx($watermark), imagesy($watermark));
    imagecopyresampled( $baseImage, $watermark, 0, 0, 0, 0, imagesx($baseImage), imagesy($baseImage), imagesx($watermark), imagesy($watermark));

    //Set image
    if(!$image_save_func($baseImage, $this->tmpfile)){
        throw new Exception("Cannot save file ".$this->tmpfile);
    }//if

    //Set new main image
    $this->setNewMainImage($this->tmpfile);

    //Free memory!
    imagedestroy($baseImage);
    unset($Watermark);

}//function

I tried this with PNG and JPG as base image and PNG as watermark image. It also resizes the watermark to fit the base image size. I've still got to do some testing, but maybe you'd like to check this meanwhile.

webbird commented 12 years ago

I have a solution for gif now, too. The only drawback is that there's no opacity anymore, because imagecopymerge() can't be used. As you know, this causes problems with Alpha transparency. But I don't think this is really a problem, because one can just tweak the transparency of the watermark image.

What do you think?

webbird commented 12 years ago

Code:

public function writeWatermark($opacity=50, $marginH=0, $marginV=0, $positionWatermarkLeftRight="c", $positionWatermarkTopBottom="c") {

    //Watermark
    list($image_create_func, $image_save_func) = $this->Watermark->getFunctionNames();
    $watermark = $image_create_func($this->Watermark->getImage());
    if ( $this->Watermark->getType() == 'png' ) {
        imagealphablending($watermark, true);
        imagesavealpha($watermark, true);
    }

    //get base image
    list($image_create_func, $image_save_func) = $this->getFunctionNames();
    $baseImage = $image_create_func($this->image);
    if ( $this->getType() == 'png' ) {
        imagealphablending($baseImage, true);
        imagesavealpha($baseImage, true);
    }

    if ( $this->getType() == 'gif' && $this->Watermark->getType() != 'gif' )
    {
        // we have to convert the watermark to gif, too
        $tempimg = imagecreatetruecolor(imagesx($baseImage),imagesy($baseImage));
        imagefill($tempimg, 0, 0, $bgcolor = imagecolorallocate($tempimg,0,0,0));
        imagecopyresampled($tempimg, $watermark, 0, 0, 0, 0, imagesx($baseImage), imagesy($baseImage), imagesx($watermark), imagesy($watermark) );
        imagecolortransparent($tempimg, $bgcolor);
        $dest_path = pathinfo( $this->tmpfile, PATHINFO_DIRNAME );
        $dest_file = 'tmp.gif';
        imagegif($tempimg,$dest_path.'/'.$dest_file);
        imagedestroy($tempimg);
        $watermark = imagecreatefromgif($dest_path.'/'.$dest_file);
        // we don't need the temp file anymore
        unlink($dest_path.'/'.$dest_file);
    }

    if ( $this->getType() == 'gif' ) {
        imagecopy( $baseImage, $watermark, 0, 0, 0, 0, imagesx($baseImage), imagesy($baseImage));
    }
    else {
        // resize the watermark to fit original image size
        imagecopyresampled( $baseImage, $watermark, 0, 0, 0, 0, imagesx($baseImage), imagesy($baseImage), imagesx($watermark), imagesy($watermark));
    }

    //Set image
    if(!$image_save_func($baseImage, $this->tmpfile)){
        throw new Exception("Cannot save file ".$this->tmpfile);
    }//if

    //Set new main image
    $this->setNewMainImage($this->tmpfile);

    //Free memory!
    imagedestroy($baseImage);
    unset($Watermark);

}//function
sprain commented 12 years ago

Thx. I'll have a look at this once I have time.