Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Gd
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
7
100.00% covered (success)
100.00%
1 / 1
 overlay
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Image\Layer;
15
16/**
17 * Layer class for Gd
18 *
19 * @category   Pop
20 * @package    Pop\Image
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    4.0.0
25 */
26class Gd extends AbstractLayer
27{
28
29    /**
30     * Opacity
31     * @var int|float|null
32     */
33    protected int|float|null $opacity = 100;
34
35    /**
36     * Overlay an image onto the current image.
37     *
38     * @param  string $image
39     * @param  int    $x
40     * @param  int    $y
41     * @throws Exception
42     * @return Gd
43     */
44    public function overlay(string $image, int $x = 0, int $y = 0): Gd
45    {
46        if ($this->hasImage()) {
47            imagealphablending($this->image->getResource(), true);
48
49            // Create an image resource from the overlay image.
50            if (stripos($image, '.gif') !== false) {
51                $overlay = imagecreatefromgif($image);
52            } else if (stripos($image, '.png') !== false) {
53                $overlay = imagecreatefrompng($image);
54            } else if (stripos($image, '.jp') !== false) {
55                $overlay = imagecreatefromjpeg($image);
56            } else {
57                throw new Exception('Error: The overlay image must be either a JPG, GIF or PNG.');
58            }
59
60            if ($this->opacity > 0) {
61                if ($this->opacity == 100) {
62                    imagecopy($this->image->getResource(), $overlay, $x, $y, 0, 0, imagesx($overlay), imagesy($overlay));
63                } else {
64                    imagecopymerge(
65                        $this->image->getResource(), $overlay, $x, $y, 0, 0, imagesx($overlay), imagesy($overlay), $this->opacity
66                    );
67                }
68            }
69        }
70
71        return $this;
72    }
73
74}