1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Image;
use function count;
use function extension_loaded;
use function function_exists;
use function imagearc;
use function imagecolorallocate;
use function imagecopyresampled;
use function imagecreatefromstring;
use function imagecreatetruecolor;
use function imagedestroy;
use function imagefilledpolygon;
use function imagefilledrectangle;
use function imagejpeg;
use function imageline;
use function imagepng;
use function imagestring;
use function imagesx;
use function imagesy;
use const PHP_VERSION_ID;
final class ImageWrapper
{
/** @var resource */
private $image;
/**
* @param resource $image
*/
private function __construct($image)
{
$this->image = $image;
}
/**
* @return resource
*/
public function getImage()
{
return $this->image;
}
/**
* @param array<string, int>|null $background
* @psalm-param array{red: int, green: int, blue: int} $background
*/
public static function create(int $width, int $height, ?array $background = null): ?self
{
if (! extension_loaded('gd')) {
return null;
}
$image = imagecreatetruecolor($width, $height);
if ($image === false) {
return null;
}
if (! isset($background, $background['red'], $background['green'], $background['blue'])) {
return new self($image);
}
$backgroundColor = imagecolorallocate($image, $background['red'], $background['green'], $background['blue']);
if ($backgroundColor === false) {
imagedestroy($image);
return null;
}
if (! imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $backgroundColor)) {
imagedestroy($image);
return null;
}
return new self($image);
}
public static function fromString(string $data): ?self
{
if (! extension_loaded('gd')) {
return null;
}
$image = imagecreatefromstring($data);
if ($image === false) {
return null;
}
return new self($image);
}
public function arc(
int $centerX,
int $centerY,
int $width,
int $height,
int $startAngle,
int $endAngle,
int $color
): bool {
return imagearc($this->image, $centerX, $centerY, $width, $height, $startAngle, $endAngle, $color);
}
/**
* @return int|false
*/
public function colorAllocate(int $red, int $green, int $blue)
{
return imagecolorallocate($this->image, $red, $green, $blue);
}
public function copyResampled(
ImageWrapper $sourceImage,
int $destinationX,
int $destinationY,
int $sourceX,
int $sourceY,
int $destinationWidth,
int $destinationHeight,
int $sourceWidth,
int $sourceHeight
): bool {
return imagecopyresampled(
$this->image,
$sourceImage->getImage(),
$destinationX,
$destinationY,
$sourceX,
$sourceY,
$destinationWidth,
$destinationHeight,
$sourceWidth,
$sourceHeight
);
}
public function destroy(): bool
{
if (PHP_VERSION_ID >= 80000) {
return true;
}
return imagedestroy($this->image);
}
public function filledPolygon(array $points, int $color): bool
{
if (PHP_VERSION_ID < 80000) {
return imagefilledpolygon($this->image, $points, (int) (count($points) / 2), $color);
}
return imagefilledpolygon($this->image, $points, $color);
}
public function height(): int
{
return imagesy($this->image);
}
/**
* @param resource|string|null $file
*/
public function jpeg($file = null, int $quality = -1): bool
{
if (! function_exists('imagejpeg')) {
return false;
}
return imagejpeg($this->image, $file, $quality);
}
public function line(int $x1, int $y1, int $x2, int $y2, int $color): bool
{
return imageline($this->image, $x1, $y1, $x2, $y2, $color);
}
/**
* @param resource|string|null $file
*/
public function png($file = null, int $quality = -1, int $filters = -1): bool
{
if (! function_exists('imagepng')) {
return false;
}
return imagepng($this->image, $file, $quality, $filters);
}
public function string(int $font, int $x, int $y, string $string, int $color): bool
{
return imagestring($this->image, $font, $x, $y, $string, $color);
}
public function width(): int
{
return imagesx($this->image);
}
}
|