82 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			82 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | ||
| 
 | ||
| namespace kernel\helpers;
 | ||
| 
 | ||
| class ImageGD
 | ||
| {
 | ||
|     public \GdImage $img;
 | ||
| 
 | ||
|     public function __construct(string $resource = '', int $width = 200, int $height = 200)
 | ||
|     {
 | ||
|         if ($resource){
 | ||
|             $this->img = imagecreatefrompng($resource);
 | ||
|         }
 | ||
|         else {
 | ||
|             $this->img = imagecreatetruecolor($width, $height);
 | ||
|         }
 | ||
|         imagesavealpha($this->img, true);
 | ||
|     }
 | ||
| 
 | ||
|     public function addText(string $font_size, int $degree, int $x, int $y, string $color, string $font, string $text): void
 | ||
|     {
 | ||
|         $rgbArr = $this->hexToRgb($color);
 | ||
|         $color = imagecolorallocate($this->img, $rgbArr[0], $rgbArr[1], $rgbArr[2]);
 | ||
|         imagettftext($this->img, $font_size, $degree, $x, $y, $color, $font, $text);
 | ||
|     }
 | ||
| 
 | ||
|     public function addImg(\GdImage $gdImage, int $location_x, int $location_y, int $offset_src_x, int $offset_src_y, int $src_width, int $src_height, int $no_transparent): void
 | ||
|     {
 | ||
|         imagecopymerge($this->img, $gdImage, $location_x, $location_y, $offset_src_x, $offset_src_y, $src_width, $src_height, $no_transparent);
 | ||
|     }
 | ||
| 
 | ||
|     public function getImg()
 | ||
|     {
 | ||
|         return $this->img;
 | ||
|     }
 | ||
| 
 | ||
|     public function save(string $path): void
 | ||
|     {
 | ||
|         imagepng($this->img, $path);
 | ||
|         imagedestroy($this->img);
 | ||
|     }
 | ||
| 
 | ||
|     public function makeCornersForImage($radius, $background): void
 | ||
|     {
 | ||
|         // включаем режим сопряжения цветов
 | ||
|         imagealphablending($this->img, true);
 | ||
|         // размер исходной картинки
 | ||
|         $width = imagesx($this->img);
 | ||
|         $height = imagesy($this->img);
 | ||
|         // создаем изображение для углов
 | ||
|         $corner = imagecreatetruecolor($radius, $radius);
 | ||
|         imagealphablending($corner, false);
 | ||
|         // прозрачный цвет
 | ||
|         $trans = imagecolorallocatealpha($corner, 255, 255, 255, 127);
 | ||
|         // заливаем картинку для углов
 | ||
|         imagefill($corner, 0, 0, $background);
 | ||
|         // рисуем прозрачный эллипс
 | ||
|         imagefilledellipse($corner, $radius, $radius, $radius * 2, $radius * 2, $trans);
 | ||
|         // массив положений. Для расположения по углам
 | ||
|         $positions = array(
 | ||
|             array(0, 0),
 | ||
|             array($width - $radius, 0),
 | ||
|             array($width - $radius, $height - $radius),
 | ||
|             array(0, $height - $radius),
 | ||
|         );
 | ||
|         // накладываем на углы картинки изображение с прозрачными эллипсами
 | ||
|         foreach ($positions as $pos) {
 | ||
|             imagecopyresampled($this->img, $corner, $pos[0], $pos[1], 0, 0, $radius, $radius, $radius, $radius);
 | ||
|             // поворачиваем картинку с эллипсов каждый раз на 90 градусов
 | ||
|             $corner = imagerotate($corner, -90, $background);
 | ||
|         }
 | ||
| 
 | ||
|     }
 | ||
| 
 | ||
|     protected function hexToRgb(string $hex)
 | ||
|     {
 | ||
|         return sscanf($hex, "#%02x%02x%02x");
 | ||
|     }
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| } |