Skip to content

Commit

Permalink
constants are PascalCase
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 4, 2022
1 parent d9a5ce7 commit e299b3c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
override(\Nette\Utils\Arrays::toObject(0), type(1));

expectedArguments(\Nette\Utils\Arrays::grep(), 2, PREG_GREP_INVERT);
expectedArguments(\Nette\Utils\Image::resize(), 2, \Nette\Utils\Image::SHRINK_ONLY, \Nette\Utils\Image::STRETCH, \Nette\Utils\Image::FIT, \Nette\Utils\Image::FILL, \Nette\Utils\Image::EXACT);
expectedArguments(\Nette\Utils\Image::calculateSize(), 4, \Nette\Utils\Image::SHRINK_ONLY, \Nette\Utils\Image::STRETCH, \Nette\Utils\Image::FIT, \Nette\Utils\Image::FILL, \Nette\Utils\Image::EXACT);
expectedArguments(\Nette\Utils\Image::resize(), 2, \Nette\Utils\Image::ShrinkOnly, \Nette\Utils\Image::Stretch, \Nette\Utils\Image::Fit, \Nette\Utils\Image::Fill, \Nette\Utils\Image::Exact);
expectedArguments(\Nette\Utils\Image::calculateSize(), 4, \Nette\Utils\Image::ShrinkOnly, \Nette\Utils\Image::Stretch, \Nette\Utils\Image::Fit, \Nette\Utils\Image::Fill, \Nette\Utils\Image::Exact);
expectedArguments(\Nette\Utils\Json::encode(), 1, \Nette\Utils\Json::PRETTY);
expectedArguments(\Nette\Utils\Json::decode(), 1, \Nette\Utils\Json::FORCE_ARRAY);
expectedArguments(\Nette\Utils\Strings::split(), 2, \PREG_SPLIT_NO_EMPTY | \PREG_OFFSET_CAPTURE);
Expand Down
38 changes: 23 additions & 15 deletions src/Utils/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,27 @@ class Image
use Nette\SmartObject;

/** {@link resize()} only shrinks images */
public const SHRINK_ONLY = 0b0001;
public const ShrinkOnly = 0b0001;

/** {@link resize()} will ignore aspect ratio */
public const STRETCH = 0b0010;
public const Stretch = 0b0010;

/** {@link resize()} fits in given area so its dimensions are less than or equal to the required dimensions */
public const FIT = 0b0000;
public const Fit = 0b0000;

/** {@link resize()} fills given area so its dimensions are greater than or equal to the required dimensions */
public const FILL = 0b0100;
public const Fill = 0b0100;

/** {@link resize()} fills given area exactly */
public const EXACT = 0b1000;
public const Exact = 0b1000;

/** deprecated */
public const SHRINK_ONLY = self::ShrinkOnly;
public const STRETCH = self::Stretch;
public const FIT = self::Fit;
public const FILL = self::Fill;
public const EXACT = self::Exact;
public const EMPTY_GIF = self::EmptyGIF;

/** image types */
public const
Expand All @@ -122,7 +130,7 @@ class Image
AVIF = 19, // IMAGETYPE_AVIF,
BMP = IMAGETYPE_BMP;

public const EMPTY_GIF = "GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00!\xf9\x04\x01\x00\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;";
public const EmptyGIF = "GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00!\xf9\x04\x01\x00\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;";

private const Formats = [self::JPEG => 'jpeg', self::PNG => 'png', self::GIF => 'gif', self::WEBP => 'webp', self::AVIF => 'avif', self::BMP => 'bmp'];

Expand Down Expand Up @@ -349,10 +357,10 @@ public function getImageResource()
* @param int|string|null $height in pixels or percent
* @return static
*/
public function resize($width, $height, int $flags = self::FIT)
public function resize($width, $height, int $flags = self::Fit)
{
if ($flags & self::EXACT) {
return $this->resize($width, $height, self::FILL)->crop('50%', '50%', $width, $height);
if ($flags & self::Exact) {
return $this->resize($width, $height, self::Fill)->crop('50%', '50%', $width, $height);
}

[$newWidth, $newHeight] = static::calculateSize($this->getWidth(), $this->getHeight(), $width, $height, $flags);
Expand Down Expand Up @@ -392,7 +400,7 @@ public static function calculateSize(
int $srcHeight,
$newWidth,
$newHeight,
int $flags = self::FIT
int $flags = self::Fit
): array
{
if ($newWidth === null) {
Expand All @@ -406,17 +414,17 @@ public static function calculateSize(
if ($newHeight === null) {
} elseif (self::isPercent($newHeight)) {
$newHeight = (int) round($srcHeight / 100 * abs($newHeight));
$flags |= empty($percents) ? 0 : self::STRETCH;
$flags |= empty($percents) ? 0 : self::Stretch;
} else {
$newHeight = abs($newHeight);
}

if ($flags & self::STRETCH) { // non-proportional
if ($flags & self::Stretch) { // non-proportional
if (!$newWidth || !$newHeight) {
throw new Nette\InvalidArgumentException('For stretching must be both width and height specified.');
}

if ($flags & self::SHRINK_ONLY) {
if ($flags & self::ShrinkOnly) {
$newWidth = (int) round($srcWidth * min(1, $newWidth / $srcWidth));
$newHeight = (int) round($srcHeight * min(1, $newHeight / $srcHeight));
}
Expand All @@ -434,11 +442,11 @@ public static function calculateSize(
$scale[] = $newHeight / $srcHeight;
}

if ($flags & self::FILL) {
if ($flags & self::Fill) {
$scale = [max($scale)];
}

if ($flags & self::SHRINK_ONLY) {
if ($flags & self::ShrinkOnly) {
$scale[] = 1;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Utils/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class Strings
{
use Nette\StaticClass;

public const TRIM_CHARACTERS = " \t\n\r\0\x0B\u{A0}";
public const TrimCharacters = " \t\n\r\0\x0B\u{A0}";
public const TRIM_CHARACTERS = self::TrimCharacters;


/**
Expand Down Expand Up @@ -366,7 +367,7 @@ public static function length(string $s): int
/**
* Removes all left and right side spaces (or the characters passed as second argument) from a UTF-8 encoded string.
*/
public static function trim(string $s, string $charlist = self::TRIM_CHARACTERS): string
public static function trim(string $s, string $charlist = self::TrimCharacters): string
{
$charlist = preg_quote($charlist, '#');
return self::replace($s, '#^[' . $charlist . ']+|[' . $charlist . ']+$#Du', '');
Expand Down
4 changes: 2 additions & 2 deletions tests/Utils/Image.factories.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ test('', function () {


test('', function () {
$image = Image::fromString(Image::EMPTY_GIF, $format);
$image = Image::fromString(Image::EmptyGIF, $format);
Assert::same(1, $image->getWidth());
Assert::same(1, $image->getHeight());
Assert::same(Image::GIF, $format);

Assert::same(Image::GIF, Image::detectTypeFromString(Image::EMPTY_GIF, $w, $h));
Assert::same(Image::GIF, Image::detectTypeFromString(Image::EmptyGIF, $w, $h));
Assert::same(1, $w);
Assert::same(1, $h);
});
Expand Down
14 changes: 7 additions & 7 deletions tests/Utils/Image.resize.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ test('resizing X', function () use ($main) {

test('resizing Y shrink', function () use ($main) {
$image = clone $main;
$image->resize(null, 150, Image::SHRINK_ONLY);
$image->resize(null, 150, Image::ShrinkOnly);
Assert::same(176, $image->width);
Assert::same(104, $image->height);
});


test('resizing X Y shrink', function () use ($main) {
$image = clone $main;
$image->resize(300, 150, Image::SHRINK_ONLY);
$image->resize(300, 150, Image::ShrinkOnly);
Assert::same(176, $image->width);
Assert::same(104, $image->height);
});
Expand All @@ -62,15 +62,15 @@ test('resizing X Y', function () use ($main) {

test('resizing X Y stretch', function () use ($main) {
$image = clone $main;
$image->resize(300, 100, Image::STRETCH);
$image->resize(300, 100, Image::Stretch);
Assert::same(300, $image->width);
Assert::same(100, $image->height);
});


test('resizing X Y shrink stretch', function () use ($main) {
$image = clone $main;
$image->resize(300, 100, Image::SHRINK_ONLY | Image::STRETCH);
$image->resize(300, 100, Image::ShrinkOnly | Image::Stretch);
Assert::same(176, $image->width);
Assert::same(100, $image->height);
});
Expand Down Expand Up @@ -102,23 +102,23 @@ test('flipping X', function () use ($main) {

test('flipping Y shrink', function () use ($main) {
$image = clone $main;
$image->resize(null, -150, Image::SHRINK_ONLY);
$image->resize(null, -150, Image::ShrinkOnly);
Assert::same(176, $image->width);
Assert::same(104, $image->height);
});


test('flipping X Y shrink', function () use ($main) {
$image = clone $main;
$image->resize(-300, -150, Image::SHRINK_ONLY);
$image->resize(-300, -150, Image::ShrinkOnly);
Assert::same(176, $image->width);
Assert::same(104, $image->height);
});


test('exact resize', function () use ($main) {
$image = clone $main;
$image->resize(300, 150, Image::EXACT);
$image->resize(300, 150, Image::Exact);
Assert::same(300, $image->width);
Assert::same(150, $image->height);
});
Expand Down

0 comments on commit e299b3c

Please sign in to comment.