Skip to content
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

Watermark and Trim plugins #129

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions examples/trim.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* PhpThumb Library Example File
*
* This file contains example usage for the PHP Thumb Library
*
* PHP Version 5 with GD 2.0+
* PhpThumb : PHP Thumb Library <http://phpthumb.gxdlabs.com>
* Copyright (c) 2009, Ian Selby/Gen X Design
*
* Author(s): Ian Selby <[email protected]>
*
* Licensed under the MIT License
* Redistributions of files must retain the above copyright notice.
*
* @author Oleg Sherbakov <[email protected]>
* @copyright Copyright (c) 2016
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @version 3.0
* @package PhpThumb
* @subpackage Examples
* @filesource
*/

require_once '../vendor/autoload.php';

$thumb = new PHPThumb\GD(__DIR__ .'/../tests/resources/test.jpg', array(), array(
new PHPThumb\Plugins\Trim(array(255, 255, 255), 'TBLR')
));

$thumb->show();
33 changes: 33 additions & 0 deletions examples/watermark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* PhpThumb Library Example File
*
* This file contains example usage for the PHP Thumb Library
*
* PHP Version 5 with GD 2.0+
* PhpThumb : PHP Thumb Library <http://phpthumb.gxdlabs.com>
* Copyright (c) 2009, Ian Selby/Gen X Design
*
* Author(s): Ian Selby <[email protected]>
*
* Licensed under the MIT License
* Redistributions of files must retain the above copyright notice.
*
* @author Oleg Sherbakov <[email protected]>
* @copyright Copyright (c) 2016
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @version 3.0
* @package PhpThumb
* @subpackage Examples
* @filesource
*/

require_once '../vendor/autoload.php';

$watermark = new PHPThumb\GD(__DIR__ .'/../tests/resources/test.jpg');

$thumb = new PHPThumb\GD(__DIR__ .'/../tests/resources/test.jpg', array(), array(
new PHPThumb\Plugins\Watermark($watermark->resizePercent(20), 'center', 50, 0, 0)
));

$thumb->show();
20 changes: 14 additions & 6 deletions src/PHPThumb/GD.php
Original file line number Diff line number Diff line change
Expand Up @@ -762,9 +762,10 @@ public function rotateImage($direction = 'CW')
* Rotates image specified number of degrees
*
* @param int $degrees
* @param array $fillColor
* @return \PHPThumb\GD
*/
public function rotateImageNDegrees($degrees)
public function rotateImageNDegrees($degrees, $fillColor = array(0, 0, 0))
{
if (!is_numeric($degrees)) {
throw new \InvalidArgumentException('$degrees must be numeric');
Expand All @@ -774,13 +775,20 @@ public function rotateImageNDegrees($degrees)
throw new \RuntimeException('Your version of GD does not support image rotation');
}

$this->workingImage = imagerotate($this->oldImage, $degrees, 0);
$this->workingImage = imagerotate(
$this->oldImage,
$degrees,
imagecolorallocate(
$this->oldImage,
$fillColor[0],
$fillColor[1],
$fillColor[2]
)
);

$newWidth = $this->currentDimensions['height'];
$newHeight = $this->currentDimensions['width'];
$this->oldImage = $this->workingImage;
$this->currentDimensions['width'] = $newWidth;
$this->currentDimensions['height'] = $newHeight;
$this->currentDimensions['width'] = imagesx($this->workingImage);
$this->currentDimensions['height'] = imagesy($this->workingImage);

return $this;
}
Expand Down
221 changes: 221 additions & 0 deletions src/PHPThumb/Plugins/Trim.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
<?php

namespace PHPThumb\Plugins;

/**
* GD Trim Lib Plugin Definition File
*
* This file contains the plugin definition for the GD Trim Lib for PHP Thumb
*
* PHP Version 5.3 with GD 2.0+
* PhpThumb : PHP Thumb Library <http://phpthumb.gxdlabs.com>
* Copyright (c) 2009, Ian Selby/Gen X Design
*
* Author(s): Ian Selby <[email protected]>
*
* Licensed under the MIT License
* Redistributions of files must retain the above copyright notice.
*
* @author Oleg Sherbakov <[email protected]>
* @copyright Copyright (c) 2016
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @version 1.0
* @package PhpThumb
* @filesource
*/

/**
* GD Trim Lib Plugin
*
* This plugin allows you to trim unnecessary single color borders from any side of image
*
* @package PhpThumb
* @subpackage Plugins
*/
class Trim implements \PHPThumb\PluginInterface
{
/**
* @var array Contains trimmed color in array of RGB parts
*/
protected $color;

/**
* @var array Contains array of sides which will be trim
*/
protected $sides;

/**
* Validate whether RGB color parts array valid or not
*
* @param $colors
* @return bool
*/
private function validateColor($colors)
{
if (!(is_array($colors) && count($colors) == 3))
return false;

foreach($colors as $color) {
if ($color < 0 || $color > 255)
return false;
}

return true;
}

/**
* Validates whether sides is valid or not
*
* @param $sidesString
* @return bool
*/
private function validateSides($sidesString)
{
$sides = str_split($sidesString);

if (count($sides) > 4 || count($sides) == 0)
return false;

foreach($sides as $side) {
if (!in_array($side, array('T', 'B', 'L', 'R')))
return false;
}

return true;
}

/**
* Trim constructor
*
* @param array $color
* @param string $sides
*/
public function __construct($color = array(255, 255, 255), $sides = 'TBLR')
{
// make sure our arguments are valid
if (!$this->validateColor($color)) {
throw new \InvalidArgumentException('Color must be array of RGB color model parts');
}

if (!$this->validateSides($sides)) {
throw new \InvalidArgumentException('Sides must be string with T, B, L, and/or R coordinates');
}

$this->color = $color;
$this->sides = str_split($sides);
}

/**
* Converts rgb parts array to integer representation
*
* @param array $rgb
* @return number
*/
private function rgb2int(array $rgb)
{
return hexdec(
sprintf("%02x%02x%02x", $rgb[0], $rgb[1], $rgb[2])
);
}

/**
* @param \PHPThumb\GD $phpthumb
* @return \PHPThumb\GD
*/
public function execute($phpthumb)
{
$currentImage = $phpthumb->getOldImage();
$currentDimensions = $phpthumb->getCurrentDimensions();

$borderTop = 0;
$borderBottom = 0;
$borderLeft = 0;
$borderRight = 0;

if (in_array('T', $this->sides)) {
for (; $borderTop < $currentDimensions['height']; ++$borderTop) {
for ($x = 0; $x < $currentDimensions['width']; ++$x) {
if (imagecolorat(
$currentImage,
$x,
$borderTop
) != $this->rgb2int($this->color)) {
break 2;
}
}
}
}

if (in_array('B', $this->sides)) {
for (; $borderBottom < $currentDimensions['height']; ++$borderBottom) {
for ($x = 0; $x < $currentDimensions['width']; ++$x) {
if (imagecolorat(
$currentImage,
$x,
$currentDimensions['height'] - $borderBottom - 1
) != $this->rgb2int($this->color)) {
break 2;
}
}
}
}

if (in_array('L', $this->sides)) {
for (; $borderLeft < $currentDimensions['width']; ++$borderLeft) {
for ($y = 0; $y < $currentDimensions['height']; ++$y) {
if (imagecolorat(
$currentImage,
$borderLeft,
$y
) != $this->rgb2int($this->color)) {
break 2;
}
}
}
}

if (in_array('R', $this->sides)) {
for (; $borderRight < $currentDimensions['width']; ++$borderRight) {
for ($y = 0; $y < $currentDimensions['height']; ++$y) {
if (imagecolorat(
$currentImage,
$currentDimensions['width'] - $borderRight - 1,
$y
) != $this->rgb2int($this->color)) {
break 2;
}
}
}
}

$newWidth = $currentDimensions['width'] - ($borderLeft + $borderRight);
$newHeight = $currentDimensions['height'] - ($borderTop + $borderBottom);

$newImage = imagecreatetruecolor(
$newWidth,
$newHeight
);

imagecopy(
$newImage,
$currentImage,
0,
0,
$borderLeft,
$borderTop,
$currentDimensions['width'],
$currentDimensions['height']
);

$phpthumb->setOldImage($newImage);

$phpthumb->setCurrentDimensions(array(
'width' => $newWidth,
'height' => $newHeight
));

return $phpthumb;
}
}


Loading