| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Compolomus\Compomage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Compolomus\Compomage\Interfaces\ImageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class GD extends AbstractImage implements ImageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * GD constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @param string $image | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public function __construct(string $image) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $this->init($image); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @param string $source | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @return ImageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected function tmp(string $source): ImageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $image = imagecreatefromstring($source); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         if (!is_resource($image)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             throw new \Exception('Image create failed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->setImage($image); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->setSizes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         // save transparent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         imagesavealpha($this->getImage(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         imagealphablending($this->getImage(), false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     public function flip(): ImageInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         imageflip($this->getImage(), IMG_FLIP_VERTICAL); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function flop(): ImageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         imageflip($this->getImage(), IMG_FLIP_HORIZONTAL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function grayscale(): ImageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         imagefilter($this->getImage(), IMG_FILTER_GRAYSCALE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param string $text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param string $font | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param string $position | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function copyright(string $text, string $font, string $position = 'SouthWest') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if (!array_key_exists(strtoupper($position), $this->positions)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             throw new \Exception('Wrong position'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         imagecopymerge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $this->getImage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $image = $this->prepareImage($text, $font), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             intval((($this->getWidth() - imagesx($image)) / 2) * $this->positions[strtoupper($position)]['x']) + $this->positions[strtoupper($position)]['padX'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             intval((($this->getHeight() - imagesy($image)) / 2) * $this->positions[strtoupper($position)]['y']) + $this->positions[strtoupper($position)]['padY'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $this->getWidth(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $this->getHeight(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             80 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param string $text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param string $font | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @return resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     private function prepareImage(string $text, string $font) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         if (!$coordinates = imagettfbbox($fontSize = 15, 0, $font, $text)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             throw new \Exception('Does not support font'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $minX = min([$coordinates[0], $coordinates[2], $coordinates[4], $coordinates[6]]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $maxX = max([$coordinates[0], $coordinates[2], $coordinates[4], $coordinates[6]]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $minY = min([$coordinates[1], $coordinates[3], $coordinates[5], $coordinates[7]]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $maxY = max([$coordinates[1], $coordinates[3], $coordinates[5], $coordinates[7]]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $textX = intval(abs($minX)) + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $textY = intval(abs($minY)) + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $image = $this->newImage($maxX - $minX + 2, $maxY - $minY + 2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         imagecolortransparent($image, $white = imagecolorallocate($image, 0, 0, 0)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         imagefilledrectangle($image, 0, 0, $this->getWidth(), 20, $white); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         imagettftext($image, $fontSize, 0, $textX, $textY, $white, $font, $text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         imagettftext($image, $fontSize, 0, $textX + 1, $textY + 1, $blue = imagecolorallocate($image, 0, 128, 128), $font, $text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         imagettftext($image, $fontSize, 0, $textX + 1, $textY + 1, $red = imagecolorallocate($image, 255, 0, 0), $font, $text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         imagettftext($image, $fontSize, 0, $textX + 2, $textY + 2, $black = imagecolorallocate($image, 255, 255, 255), $font, $text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         imagettftext($image, $fontSize, 0, $textX + 2, $textY + 2, $gray = imagecolorallocate($image, 128, 128, 128), $font, $text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         return $image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     protected function setSizes(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $this->setWidth(imagesx($this->getImage())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $this->setHeight(imagesy($this->getImage())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     private function newImage(int $width, int $height) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $newimg = imagecreatetruecolor($width, $height); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $transparent = imagecolorallocatealpha($newimg, 255, 255, 255, 127); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         imagefill($newimg, 0, 0, $transparent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         imagealphablending($newimg, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         imagesavealpha($newimg, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         return $newimg; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     public function resize(int $width, int $height): ImageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $newimage = $this->newImage($width, $height); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         imagecopyresampled($newimage, $this->getImage(), 0, 0, 0, 0, $width, $height, $this->getWidth() , $this->getHeight()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $this->setImage($newimage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $this->setSizes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     public function crop(int $width, int $height, int $x, int $y): ImageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $width = $width - $x; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $height = $height - $y; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $newimage = $this->newImage($width, $height); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         imagecopyresampled($newimage, $this->getImage(), 0, 0, $x, $y, $width, $height, $width, $height); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $this->setImage($newimage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $this->setSizes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     public function watermark(): ImageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     public function rotate(int $angle = 90): ImageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         $transparent = imagecolorallocatealpha($this->image, 0, 0, 0, 127); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         $rotate = imagerotate($this->getImage(), $angle, $transparent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         imagealphablending($rotate, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         imagesavealpha($rotate, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         $this->setImage($rotate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         $this->setSizes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     public function __toString(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         ob_start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         imagepng($this->getImage(), null, 9, PNG_ALL_FILTERS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         $temp = ob_get_contents(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         ob_clean(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         return trim($temp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     public function save(string $filename): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 188 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 189 |  |  |  |