Completed
Push — develop ( 0f61a6...99462d )
by Peter
13:32
created

Watermark::execute()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 19
nc 1
nop 1
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 <[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
     * @param PHPThumb $watermarkThumb
33
     * @param array $position
34
     */
35
    public function __construct(PHPThumb $watermarkThumb, $position = [0, 0])
36
    {
37
        $this->watermarkThumb = $watermarkThumb;
38
        $this->position = $position;
39
    }
40
41
42
    /**
43
     * @param PHPThumb $phpthumb
44
     * @return PHPThumb
45
     */
46
    public function execute($phpthumb)
47
    {
48
        $currentDimensions = $phpthumb->getCurrentDimensions();
49
        $height            = $currentDimensions['height'];
50
        $oldImage          = $phpthumb->getOldImage();
51
        $watermarkImage    = $this->watermarkThumb->getOldImage();
52
53
        $watermarkCurrentDimensions = $this->watermarkThumb->getCurrentDimensions();
54
        $watermarkWidth             = $watermarkCurrentDimensions['width'];
55
        $watermarkHeight            = $watermarkCurrentDimensions['height'];
56
        $positionY                  = ($height - $watermarkHeight) - $this->position[1];
57
58
        imagecopy(
59
            $oldImage,
60
            $watermarkImage,
61
            $this->position[0],
62
            $positionY,
63
            0,
64
            0,
65
            $watermarkWidth,
66
            $watermarkHeight
67
        );
68
69
        return $phpthumb;
70
    }
71
}
72