-
Notifications
You must be signed in to change notification settings - Fork 264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simple plugin to add logo to image #34
Comments
Expanded your code a little to suite my needs :-)
<?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, |
Hi! |
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. |
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. |
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%) |
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:
And the following example demonstates the usage of this plugin:
best regards,
Sergey
The text was updated successfully, but these errors were encountered: