masterexploder / PHPThumb

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

Watermark #36

Open cigraphics opened 13 years ago

cigraphics commented 13 years ago

I tried the plugin from the issues but it gived me errors so i made some modifications and added it to the core Add this to: GdThumb.inc.php <?php /* ............ */ public function watermark($logoFileName, $positionX = 'center', $positionY = 'center', $alpha = 25) { $logo_size = getimagesize($logoFileName); $logo_dimension = array("x" => $logo_size[0], "y" => $logo_size[1]); $src_dimension = array("x" => $this->currentDimensions['width'], "y" => $this->currentDimensions['height']); $center = array("x" => (($src_dimension["x"] / 2) - ($logo_dimension["x"]/2)), "y" => (($src_dimension["y"] / 2) - ($logo_dimension["y"]/2))); $logo_postionX["left"] = 0; $logo_postionX["center"] = $center["x"]; $logo_postionX["right"] = $src_dimension["x"] - $logo_dimension["x"]; $logo_postionY["top"] = 0; $logo_postionY["center"] = $center["y"]; $logo_postionY["bottom"] = $src_dimension["y"] - $logo_dimension["y"];

    if(is_numeric($positionX)){ $logo_position["x"] = $positionX; } else { $logo_position["x"] = $logo_postionX[$positionX]; }
    if(is_numeric($positionY)){ $logo_position["x"] = $positionX; } else { $logo_position["y"] = $logo_postionY[$positionY]; }

    switch(exif_imagetype($logoFileName)){
        case IMAGETYPE_JPEG:
            $logo = imagecreatefromjpeg($logoFileName);
        break;

        case IMAGETYPE_PNG:
            $logo = imagecreatefrompng($logoFileName);
        break;

        case IMAGETYPE_GIF:
            $logo = imagecreatefromgif($logoFileName);
        break;
    }
    if ( is_resource($this->workingImage) ) {
        $img = $this->workingImage;
    } else {
        $img = $this->oldImage;
    }
    if ( $logo_dimension["x"] > $src_dimension["x"] ) {
        imagecopyresampled($img, $logo, $logo_position["x"] + 75, $logo_position["y"], 0, 0, $logo_dimension["x"] / 2, $logo_dimension["y"] / 2, $logo_dimension["x"], $logo_dimension["y"]);
    } else {
        imagecopy($img, $logo, $logo_position["x"], $logo_position["y"], 0, 0, $logo_dimension["x"], $logo_dimension["y"]);
    }

    return $this;
}

/* ............ */ ?>

Usage: <?php require_once 'ThumbLib.inc.php'; $th = PhpThumbFactory::create($image)->resize($config['image']['full_w'], $config['image']['full_h'])->watermark('assets/images/rubmaps-watermark.png', 'center', 'center', 25)->save(); // or $th = PhpThumbFactory::create($image)->watermark('assets/images/rubmaps-watermark.png', 'center', 'center', 25)->resize($config['image']['full_w'], $config['image']['full_h'])->show(); // or $th = PhpThumbFactory::create($image)->watermark('assets/images/rubmaps-watermark.png', 'center', 'center', 25)->show(); ?>

vugh commented 12 years ago

This works great except the $alpha param doesn't seem to work, since it's not used anywhere in the code at all. Watermark image is always at 100% no matter what the $alpha value is.

cigolpl commented 12 years ago

It should work very well with alpha param.

    public function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ 
        // creating a cut resource 
        $cut = imagecreatetruecolor($src_w, $src_h); 
        // copying relevant section from background to the cut resource 
        imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h); 
        // copying relevant section from watermark to the cut resource 
        imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h); 
        // insert cut resource to destination image 
        imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct); 
    } 

    public function watermark($logoFileName, $positionX = 'center', $positionY = 'center', $alpha = 25) {

        $logo_size = getimagesize($logoFileName);
        $logo_dimension = array("x" => $logo_size[0], "y" => $logo_size[1]);
        $src_dimension = array("x" => $this->currentDimensions['width'], "y" => $this->currentDimensions['height']);
        $center = array("x" => (($src_dimension["x"] / 2) - ($logo_dimension["x"] / 2)), "y" => (($src_dimension["y"] / 2) - ($logo_dimension["y"] / 2)));
        $logo_postionX["left"] = 0;
        $logo_postionX["center"] = $center["x"];
        $logo_postionX["right"] = $src_dimension["x"] - $logo_dimension["x"];
        $logo_postionY["top"] = 0;
        $logo_postionY["center"] = $center["y"];
        $logo_postionY["bottom"] = $src_dimension["y"] - $logo_dimension["y"];

        if (is_numeric($positionX)) {
            $logo_position["x"] = $positionX;
        } else {
            $logo_position["x"] = $logo_postionX[$positionX];
        }
        if (is_numeric($positionY)) {
            $logo_position["x"] = $positionX;
        } else {
            $logo_position["y"] = $logo_postionY[$positionY];
        }

        switch (exif_imagetype($logoFileName)) {
            case IMAGETYPE_JPEG:
                $logo = imagecreatefromjpeg($logoFileName);
                break;

            case IMAGETYPE_PNG:
                $logo = imagecreatefrompng($logoFileName);
                break;

            case IMAGETYPE_GIF:
                $logo = imagecreatefromgif($logoFileName);
                break;
        }
        if (is_resource($this->workingImage)) {
            $img = $this->workingImage;
        } else {
            $img = $this->oldImage;
        }
        if ($logo_dimension["x"] > $src_dimension["x"]) {
            imagecopyresampled($img, $logo, $logo_position["x"] + 75, $logo_position["y"], 0, 0, $logo_dimension["x"] / 2, $logo_dimension["y"] / 2, $logo_dimension["x"], $logo_dimension["y"]);
        } else {
            $this->imagecopymerge_alpha($img, $logo, $logo_position["x"], $logo_position["y"], 0, 0, $logo_dimension["x"], $logo_dimension["y"], $alpha);
        }

        return $this;
    }