2025-01-03 02:11:09 +03:00
|
|
|
|
<?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);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 20:02:06 +03:00
|
|
|
|
public function getBase64(): string
|
|
|
|
|
{
|
|
|
|
|
ob_start ();
|
|
|
|
|
imagepng($this->img);
|
|
|
|
|
$image_data = ob_get_contents ();
|
|
|
|
|
ob_end_clean ();
|
|
|
|
|
|
|
|
|
|
imagedestroy($this->img);
|
|
|
|
|
return base64_encode ($image_data);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-19 17:15:58 +03:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-03 02:11:09 +03:00
|
|
|
|
protected function hexToRgb(string $hex)
|
|
|
|
|
{
|
|
|
|
|
return sscanf($hex, "#%02x%02x%02x");
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-19 17:15:58 +03:00
|
|
|
|
|
|
|
|
|
|
2025-01-03 02:11:09 +03:00
|
|
|
|
}
|