From 89c7da2d604f67c5e4d9a7badf8635301296492b Mon Sep 17 00:00:00 2001 From: Karel Wintersky Date: Thu, 29 Oct 2020 13:04:14 +0300 Subject: [PATCH] 1.32.0 * [] GDWrapper::cropImage() + helper cropimage() --- functions/functions.php | 19 ++++++++++++++++ interfaces/GDWrapperInterface.php | 14 ++++++++++++ sources/GDWrapper.php | 36 ++++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/functions/functions.php b/functions/functions.php index c80e677..566df1a 100644 --- a/functions/functions.php +++ b/functions/functions.php @@ -107,6 +107,25 @@ function pluralForm($number, $forms, string $glue = '|'):string } } +if (!function_exists('cropimage')) { + + /** + * CropImage helper + * + * @param string $fn_source + * @param string $fn_target + * @param array $xy_source + * @param array $wh_dest + * @param array $wh_source + * @param null $quality + * @return bool + */ + function cropImage(string $fn_source, string $fn_target, array $xy_source, array $wh_dest, array $wh_source, $quality = null): bool + { + return GDWrapper::cropImage($fn_source, $fn_target, $xy_source, $wh_dest, $wh_source, $quality); + } +} + if (!function_exists('getfixedpicture')) { /** diff --git a/interfaces/GDWrapperInterface.php b/interfaces/GDWrapperInterface.php index e3fdf8b..7b4e748 100644 --- a/interfaces/GDWrapperInterface.php +++ b/interfaces/GDWrapperInterface.php @@ -12,6 +12,20 @@ interface GDWrapperInterface * @param LoggerInterface $logger */ public static function init($options = [], LoggerInterface $logger = null); + + /** + * CROP изображения с сохранением в файл + * = cropimage() + * + * @param string $fn_source + * @param string $fn_target + * @param array $xy_source + * @param array $wh_dest + * @param array $wh_source + * @param null $quality + * @return bool + */ + public static function cropImage(string $fn_source, string $fn_target, array $xy_source, array $wh_dest, array $wh_source, $quality = null): bool; /** * вписывает изображение в указанные размеры diff --git a/sources/GDWrapper.php b/sources/GDWrapper.php index 13f4d8b..3b6629a 100644 --- a/sources/GDWrapper.php +++ b/sources/GDWrapper.php @@ -63,6 +63,39 @@ public static function init($options = [], LoggerInterface $logger = null) : new NullLogger(); } + + public static function cropImage(string $fn_source, string $fn_target, array $xy_source, array $wh_dest, array $wh_source, $quality = null): bool + { + if (!is_readable($fn_source)) { + self::$logger->error("Static method " . __METHOD__ . " wants missing file", [$fn_source]); + return false; + } + + list($width, $height, $type) = getimagesize($fn_source); + list($image_source, $extension) = self::createImageFromFile($fn_source, $type); + + if ($image_source) { + $image_destination = imagecreatetruecolor($wh_dest[0], $wh_dest[1]); + + imagecopyresampled( + $image_destination, + $image_source, + 0, 0, + $xy_source[0], $xy_source[1], + $wh_dest[0], $wh_dest[1], + $wh_source[0], $wh_source[1]); + + self::storeImageToFile($fn_target, $image_destination, $extension, $quality); + + imagedestroy($image_destination); + imagedestroy($image_source); + return true; + } else { + self::$logger->error('Not image: ', [ $fn_source ]); + echo "not image {$fn_source}"; + return false; + } + } public static function resizeImageAspect(string $fn_source, string $fn_target, int $maxwidth, int $maxheight, $image_quality = null):bool { @@ -118,7 +151,8 @@ public static function resizeImageAspect(string $fn_source, string $fn_target, i private static function createImageFromFile($fname, $type) { if ($type == IMAGETYPE_BMP) { - return [null, null]; + $ext = 'bmp'; + $im = imagecreatefrombmp($fname); } else if ($type == IMAGETYPE_PNG) { $ext = 'png'; $im = imagecreatefrompng($fname);