Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Simple plugin to add logo to image #34

Open
hssergey opened this Issue Apr 13, 2011 · 7 comments

Comments

Projects
None yet
7 participants

Hello,

Sometimes we need to place the company logo in all uploaded images. With the following plugin it is now easy to to with PHPThumb. For example, we have company logo in png format. So place the following file to the thumb_plugins directory:

<?php
/**
 * Add logo plugin
 * 
 * This plugin allows you to add plugin to your image
 * 
 * @package PhpThumb
 * @subpackage Plugins
 */
class GdAddLogo {
    /**
     * Instance of GdThumb passed to this class
     * 
     * @var GdThumb
     */
    protected $parentInstance;
    protected $currentDimensions;
    protected $workingImage;
    protected $newImage;
    protected $options;

    /**
     * Add logo to image
     * @param logoFileName - file name of logo image in png format
     */
    public function addLogo($logoFileName, &$that) {
        // bring stuff from the parent class into this class...
        $this->parentInstance       = $that;
        $this->oldImage             = $this->parentInstance->getOldImage();
        $this->parentInstance->setWorkingImage($this->oldImage);
        $this->currentDimensions    = $this->parentInstance->getCurrentDimensions();
        $this->workingImage         = $this->parentInstance->getWorkingImage();
        $this->options              = $this->parentInstance->getOptions();

        $width              = $this->currentDimensions['width'];
        $height             = $this->currentDimensions['height'];

        $logo = imagecreatefrompng($logoFileName);
        imagecopymerge($this->workingImage, $logo, 0, 0, 0, 0, $width, $height, 10);

        return $that;
    }

}


$pt = PhpThumb::getInstance();
$pt->registerPlugin('GdAddLogo', 'gd');

And the following example demonstates the usage of this plugin:

<?php
require_once '../ThumbLib.inc.php';

$thumb = PhpThumbFactory::create('test.jpg');
$thumb->addLogo($_SERVER["DOCUMENT_ROOT"]."/images/imageForLogo.png");
$thumb->show();

?>

best regards,
Sergey

Expanded your code a little to suite my needs :-)
Now you can:

  • position the logo where your want it
  • use jpg or png as sourcefile
  • can adjust the alpha value for the imagecopymerge
    all from the functioncall itself as parameter
<?php
/**
 * Add logo plugin
 * 
 * This plugin allows you to add plugin to your image
 * 
 * @package PhpThumb
 * @subpackage Plugins
 * @original-author Sergey "hssergey"
 * @author Sebastian "DNightmare"
 */
class GdAddLogo {
    /**
     * Instance of GdThumb passed to this class
     * 
     * @var GdThumb
     */
    protected $parentInstance;
    protected $currentDimensions;
    protected $workingImage;
    protected $newImage;
    protected $options;

    /**
     * Add logo to image
     * @param logoFileName - file name of logo image in jpg or png format
     * @param positionX - Position of logo image on X-axis ('left', 'center', 'right' or plain number)
     * @param positionY - Position of logo image on X-axis ('top', 'center', 'bottom' or plain number)
     * @param alpha - alpha value for logo merging in percent
     */
    public function addLogo($logoFileName, $positionX, $positionY, $alpha, &$that) {
        $logo_size                  = getimagesize($logoFileName);
        // bring stuff from the parent class into this class...
        $this->parentInstance       = $that;
        $this->oldImage             = $this->parentInstance->getOldImage();
        $this->parentInstance->setWorkingImage($this->oldImage);
        $this->currentDimensions    = $this->parentInstance->getCurrentDimensions();
        $this->workingImage         = $this->parentInstance->getWorkingImage();
        $this->options              = $this->parentInstance->getOptions();

        $src_dimension              = array(
                                        "x" => $this->currentDimensions['width'], 
                                        "y" => $this->currentDimensions['height']);
        $logo_dimension             = array(
                                        "x" => $logo_size[0],
                                        "y" => $logo_size[1]);

        $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["y"] = $positionY; } 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;
        }

        imagecopymerge($this->workingImage, $logo, $logo_position["x"], $logo_position["y"], 0, 0, $logo_dimension["x"], $logo_dimension["y"], $alpha);
        return $that;
    }
}
$pt = PhpThumb::getInstance();
$pt->registerPlugin('GdAddLogo', 'gd');

Your example now turns into:

<?php
require_once '../ThumbLib.inc.php';

$thumb = PhpThumbFactory::create('test.jpg');
// Logo at top-center position 25% visible
$thumb->addLogo($_SERVER["DOCUMENT_ROOT"]."/images/imageForLogo.png", 'top', 'center', 25);
// Logo at bottom position, 100px from the left side 10% visible
// $thumb->addLogo($_SERVER["DOCUMENT_ROOT"]."/images/imageForLogo.png", 'top', 100, 10);

$thumb->show();

?>

cheers,
Sebastian

quaddin commented Jul 24, 2011

Hi!
I thinkin', how can I expand addLogo fnc for strings instead of logos. I thought, i'll ask if someone has a working version, before i startin' coding. Anybody? :-)

There is a plugin for watermarking floating around, is this any different? You pass an image and co-ords in the watermarking plugin

That watermarking is basicly the one from here but tweaked a bit and added directly to the core rather than using it as a plugin.

Arkon06 commented Aug 12, 2011

Thanks for this script

I've found a little bug... a bad copy/paste :

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

You can notice that I use imagecopy instead of imagecopymerge to keep transparency (without $alpha parameter)

and I must call the function with the last parameter :

$thumb->addLogo($_SERVER["DOCUMENT_ROOT"]."/images/imageForLogo.png", 'top', 'center', 25, &$thumb);

Thanks for pointing out the little typo, corrected it.

vugh commented Mar 19, 2012

This plugin didn't work correctly with my PNGs with transparency, that is, there is a black matting on the transparent parts. I found another one that works better: #36 (alpha % transparency doesn't work correctly yet though, it's always set at 100%)

@ghost ghost assigned sirshurf Jan 7, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment