Skip to content

Commit

Permalink
1.32.0
Browse files Browse the repository at this point in the history
* [] GDWrapper::cropImage() + helper cropimage()
  • Loading branch information
KarelWintersky committed Oct 29, 2020
1 parent 478c185 commit 89c7da2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
19 changes: 19 additions & 0 deletions functions/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {

/**
Expand Down
14 changes: 14 additions & 0 deletions interfaces/GDWrapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* вписывает изображение в указанные размеры
Expand Down
36 changes: 35 additions & 1 deletion sources/GDWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 89c7da2

Please sign in to comment.