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-2016 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 <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class Watermark implements \PHPThumb\PluginInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var PHPThumb $watermarkThumb |
23
|
|
|
*/ |
24
|
|
|
protected $watermarkThumb; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array $position |
28
|
|
|
*/ |
29
|
|
|
protected $position = [0, 0]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var float |
33
|
|
|
*/ |
34
|
|
|
protected $scale = .5; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param PHPThumb $watermarkThumb |
38
|
|
|
* @param array $position |
39
|
|
|
* @param float $scale |
40
|
|
|
*/ |
41
|
|
|
public function __construct(PHPThumb $watermarkThumb, $position = [0, 0], $scale = .5) |
42
|
|
|
{ |
43
|
|
|
$this->watermarkThumb = $watermarkThumb; |
44
|
|
|
$this->position = $position; |
45
|
|
|
$this->scale = $scale; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param PHPThumb $phpthumb |
51
|
|
|
* @return PHPThumb |
52
|
|
|
*/ |
53
|
|
|
public function execute($phpthumb) |
54
|
|
|
{ |
55
|
|
|
$currentDimensions = $phpthumb->getCurrentDimensions(); |
56
|
|
|
|
57
|
|
|
$width = $currentDimensions['width']; |
58
|
|
|
$height = $currentDimensions['height']; |
59
|
|
|
$oldImage = $phpthumb->getOldImage(); |
60
|
|
|
|
61
|
|
|
$this->watermarkThumb->resize($width * $this->scale, $height * $this->scale); |
62
|
|
|
|
63
|
|
|
$watermarkImage = $this->watermarkThumb->getOldImage(); |
64
|
|
|
$watermarkCurrentDimensions = $this->watermarkThumb->getCurrentDimensions(); |
65
|
|
|
|
66
|
|
|
$watermarkWidth = $watermarkCurrentDimensions['width']; |
67
|
|
|
$watermarkHeight = $watermarkCurrentDimensions['height']; |
68
|
|
|
|
69
|
|
|
switch ($this->position[0]) { |
70
|
|
|
case -1: |
71
|
|
|
$positionX = 0; |
72
|
|
|
break; |
73
|
|
|
case 1: |
74
|
|
|
$positionX = $width - $watermarkWidth; |
75
|
|
|
break; |
76
|
|
|
default: |
77
|
|
|
$positionX = $width / 2 - $watermarkWidth / 2; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
switch ($this->position[1]) { |
81
|
|
|
case -1: |
82
|
|
|
$positionY = $height - $watermarkHeight; |
83
|
|
|
break; |
84
|
|
|
case 1: |
85
|
|
|
$positionY = 0; |
86
|
|
|
break; |
87
|
|
|
default: |
88
|
|
|
$positionY = $height / 2 - $watermarkHeight / 2; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
imagealphablending($oldImage, true); |
92
|
|
|
imagecopy($oldImage, $watermarkImage, $positionX, $positionY, 0, 0, $watermarkWidth, $watermarkHeight); |
93
|
|
|
|
94
|
|
|
return $phpthumb; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|