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