. */ namespace SP\Domain\Common\Providers; use GdImage; use SP\Domain\Core\Exceptions\InvalidImageException; use SP\Domain\Core\Exceptions\SPException; use SP\Domain\Core\PhpExtensionCheckerService; use SP\Domain\Image\Ports\ImageService; use SP\Infrastructure\File\FileHandler; use function SP\__u; use function SP\processException; /** * Class Image */ final class Image implements ImageService { private const IMAGE_WIDTH = 48; private const IMAGE_FONT = PUBLIC_PATH . '/vendor/fonts/NotoSans-Regular-webfont.ttf'; private const TMP_PREFFIX = 'syspass'; public function __construct(PhpExtensionCheckerService $checker) { $checker->checkCurl(true); } /** * @inheritDoc * @throws InvalidImageException * @throws SPException */ public function createThumbnail(string $image): string { $im = @imagecreatefromstring($image); if ($im === false) { throw InvalidImageException::error(__u('Invalid image')); } $width = imagesx($im) ?: self::IMAGE_WIDTH; $height = imagesy($im) ?: self::IMAGE_WIDTH; $newHeight = (int)floor($height * (self::IMAGE_WIDTH / $width)); if (($tempImage = imagecreatetruecolor(self::IMAGE_WIDTH, $newHeight)) === false || !imagecopyresized($tempImage, $im, 0, 0, 0, 0, self::IMAGE_WIDTH, $newHeight, $width, $height) ) { throw SPException::error(__u('Unable to create image')); } return $this->createPngImage($tempImage); } /** * @throws SPException */ private function createPngImage(GdImage $gdImage): string { if (($tmpFile = tempnam(TMP_PATH, self::TMP_PREFFIX)) !== false && imagepng($gdImage, $tmpFile) ) { $file = new FileHandler($tmpFile); $out = base64_encode($file->readToString()); $file->delete(); return $out; } throw SPException::error(__u('Unable to create image')); } /** * @inheritDoc */ public function convertText(string $text): false|string { try { $width = strlen($text) * 10; if (($im = @imagecreatetruecolor($width, 30)) === false || ($bgColor = imagecolorallocate($im, 245, 245, 245)) === false || ($fgColor = imagecolorallocate($im, 128, 128, 128)) === false || !imagefilledrectangle($im, 0, 0, $width, 30, $bgColor) || !imagefttext($im, 10, 0, 10, 20, $fgColor, self::IMAGE_FONT, $text) ) { throw SPException::error(__u('Unable to create image')); } return $this->createPngImage($im); } catch (SPException $e) { processException($e); return false; } } }