Skip to content

Commit 6bc8d19

Browse files
author
Peter Bubelíny
committed
Initial watermark plugin implementation #15
1 parent 0f61a6a commit 6bc8d19

File tree

7 files changed

+168
-1
lines changed

7 files changed

+168
-1
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ Service that provides API to manipulate images.
1717
- [Reflection](http://webino-image-thumb.demo.webino.org/application/demo/reflection)
1818
- [Whitespace cropper](http://webino-image-thumb.demo.webino.org/application/demo/whitespace-cropper)
1919
- [Sharpen](http://webino-image-thumb.demo.webino.org/application/demo/sharpen)
20+
- [Watermark](http://webino-image-thumb.demo.webino.org/application/demo/watermark)
2021

2122
## Features
2223

2324
- Resize, crop, pad, rotate, show and save images
2425
- Create image reflection
2526
- Crop whitespace
2627
- Sharpen images
28+
- Watermark
2729

2830
## Setup
2931

@@ -68,6 +70,13 @@ Service that provides API to manipulate images.
6870
$sharpen = $thumbnailer->createSharpen();
6971
$thumb = $thumbnailer->create($imagePath, [], [$sharpen]);
7072

73+
- Use watermark plugin:
74+
75+
$watermarkPath = 'public/images/my_watermark.png';
76+
$watermarkThumb = $this->thumbnailer->create($watermarkFile);
77+
$watermark = $this->thumbnailer->createWatermark($watermarkThumb,[30,30]);
78+
$thumb = $this->thumbnailer->create($image, [], [$watermark]);
79+
7180
## Options
7281

7382
The options array allows you to customize the behavior of the library a bit. Some of these options are implementation-specific, and are noted as such. So, let's first go over what options are available to us:
@@ -185,6 +194,14 @@ Service that provides API to manipulate images.
185194
* `$margin` - What pixels of a margin should be around the original image.
186195
* `$color` - The hex value of the color you would like to crop.
187196

197+
## Watermark plugin
198+
199+
* `createWatermark(PHPThumb $watermarkThumb, $position = [0, 0])`
200+
201+
* `$watermarkThumb` - What pixels of a margin should be around the original image.
202+
* `$position` - Position of watermark, X and Y from the left bottom corner.
203+
204+
188205
## Changelog
189206

190207
### 2.0.0 [UNRELEASED]
@@ -193,6 +210,7 @@ Service that provides API to manipulate images.
193210
- Added Whitespace Cropper plugin
194211
- Added Sharpen plugin
195212
- Removed ZF autoloader support
213+
- Added Watermark plugin
196214

197215
### 1.0.0
198216

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Webino (https://github.com/webino/)
4+
*
5+
* @link https://github.com/webino/WebinoImageThumb/ for the canonical source repository
6+
* @copyright Copyright (c) 2013-2015 Webino, s. r. o. (http://webino.sk/)
7+
* @license BSD-3-Clause
8+
*/
9+
10+
namespace WebinoImageThumb\PhpThumb\Plugin;
11+
12+
use PHPThumb\GD as PHPThumb;
13+
14+
/**
15+
* Add watermark to image
16+
*
17+
* @author Peter Bubelíny <neri@neridev.com>
18+
*/
19+
class Watermark implements \PHPThumb\PluginInterface
20+
{
21+
/**
22+
* @var int
23+
*/
24+
protected $watermarkThumb;
25+
26+
/**
27+
* @var int
28+
*/
29+
protected $position = [0, 0];
30+
31+
/**
32+
* Watermark constructor
33+
* @param int $watermark
34+
* @param int $position
35+
*/
36+
public function __construct($watermarkThumb, $position)
37+
{
38+
$this->watermarkThumb = $watermarkThumb;
39+
$this->position = $position;
40+
}
41+
42+
43+
/**
44+
* @param PHPThumb $phpthumb
45+
* @return PHPThumb
46+
*/
47+
public function execute($phpthumb)
48+
{
49+
50+
$currentDimensions = $phpthumb->getCurrentDimensions();
51+
$height = $currentDimensions['height'];
52+
$oldImage = $phpthumb->getOldImage();
53+
$watermarkThumb = $this->watermarkThumb->getOldImage();
54+
55+
$watermarkCurrentDimensions = $this->watermarkThumb->getCurrentDimensions();
56+
$watermarkWidth = $watermarkCurrentDimensions['width'];
57+
$watermarkHeight = $watermarkCurrentDimensions['height'];
58+
59+
$position[0] = $this->position[0];
60+
$position[1] = $this->position[1];
61+
$positionY = ($height-$watermarkHeight)-$position[1];
62+
63+
imagecopy(
64+
$oldImage,
65+
$watermarkThumb,
66+
$position[0],
67+
$positionY,
68+
0,
69+
0,
70+
$watermarkWidth,
71+
$watermarkHeight
72+
);
73+
74+
$phpthumb->setWorkingImage($oldImage);
75+
76+
return $phpthumb;
77+
}
78+
}

src/WebinoImageThumb/Service/ImageThumb.php

+12
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,16 @@ public function createSharpen($offset = 0, array $matrix = [])
7979
{
8080
return new ExtraPlugins\Sharpen($offset, $matrix);
8181
}
82+
83+
/**
84+
* Create a watermark on image
85+
*
86+
* @param int $watermark
87+
* @param array $position
88+
* @return ExtraPlugins\Watermark
89+
*/
90+
public function createWatermark($watermarkThumb, array $position = [0,0])
91+
{
92+
return new ExtraPlugins\Watermark($watermarkThumb, $position);
93+
}
8294
}
402 KB
Loading

tests/resources/src/Application/Controller/DemoController.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Application\Controller;
1111

12+
use PHPThumb\PHPThumb;
1213
use WebinoImageThumb\Service\ImageThumb;
1314
use Zend\Mvc\Controller\AbstractActionController;
1415

@@ -90,4 +91,27 @@ public function sharpenAction()
9091

9192
return false;
9293
}
93-
}
94+
95+
/**
96+
* Save and show an image with watermark
97+
*/
98+
public function watermarkAction()
99+
{
100+
$watermarkPath = __DIR__ . '/../../../data/media/watermark.png';
101+
$image = __DIR__ . '/../../../data/media/test.jpg';
102+
103+
$watermarkThumb = $this->thumbnailer->create($watermarkPath);
104+
$watermarkThumb
105+
->resize(100, 100);
106+
107+
$watermark = $this->thumbnailer->createWatermark($watermarkThumb,[30,30]);
108+
$thumb = $this->thumbnailer->create($image, [], [$watermark]);
109+
110+
$thumb
111+
->resize(200, 200)
112+
->show()
113+
->save('public/watermark_test.jpg');
114+
115+
return false;
116+
}
117+
}

tests/selenium/WebinoImageThumb/HomeTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,13 @@ public function testSharpen()
4040
$src = file_get_contents($this->uri . 'application/demo/sharpen');
4141
$this->assertJpegImage($src);
4242
}
43+
44+
/**
45+
* Watermark plugin test
46+
*/
47+
public function testWatermark()
48+
{
49+
$src = file_get_contents($this->uri . 'application/demo/watermark');
50+
$this->assertJpegImage($src);
51+
}
4352
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Webino (http://webino.sk/)
4+
*
5+
* @link https://github.com/webino/WebinoImageThumb/ for the canonical source repository
6+
* @copyright Copyright (c) 2013-2015 Webino, s. r. o. (http://webino.sk/)
7+
* @license BSD-3-Clause
8+
*/
9+
10+
use Tester\Assert;
11+
use WebinoImageThum\PHPThumb\Plugins\Watermark;
12+
use WebinoImageThumb\Service\ImageThumb;
13+
14+
require __DIR__ . '/../bootstrap.php';
15+
16+
// TODO test
17+
/*
18+
$thumbnailer = new ImageThumb;
19+
$plugin = $thumbnailer->createWatermark($watermarkThumb);
20+
21+
22+
Assert::type(Watermark::class, $plugin);
23+
*/
24+
25+
Assert::true(true);
26+

0 commit comments

Comments
 (0)