Skip to content

Commit 71282e7

Browse files
committed
Image: refactoring
1 parent 9a4f302 commit 71282e7

File tree

3 files changed

+54
-26
lines changed

3 files changed

+54
-26
lines changed

src/Utils/Image.php

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ class Image
105105
const EXACT = 0b1000;
106106

107107
/** image types */
108-
const JPEG = IMAGETYPE_JPEG,
108+
const
109+
JPEG = IMAGETYPE_JPEG,
109110
PNG = IMAGETYPE_PNG,
110111
GIF = IMAGETYPE_GIF;
111112

@@ -114,6 +115,8 @@ class Image
114115
/** @deprecated */
115116
const ENLARGE = 0;
116117

118+
static private $formats = [self::JPEG => 'jpeg', self::PNG => 'png', self::GIF => 'gif'];
119+
117120
/** @var resource */
118121
private $image;
119122

@@ -151,17 +154,12 @@ public static function fromFile($file, & $format = NULL)
151154
throw new Nette\NotSupportedException('PHP extension GD is not loaded.');
152155
}
153156

154-
static $funcs = [
155-
self::JPEG => 'imagecreatefromjpeg',
156-
self::PNG => 'imagecreatefrompng',
157-
self::GIF => 'imagecreatefromgif',
158-
];
159157
$format = @getimagesize($file)[2]; // @ - files smaller than 12 bytes causes read error
160-
161-
if (!isset($funcs[$format])) {
158+
if (!isset(self::$formats[$format])) {
159+
$format = NULL;
162160
throw new UnknownImageFileException(is_file($file) ? "Unknown type of file '$file'." : "File '$file' not found.");
163161
}
164-
return new static(Callback::invokeSafe($funcs[$format], [$file], function ($message) {
162+
return new static(Callback::invokeSafe('imagecreatefrom' . self::$formats[$format], [$file], function ($message) {
165163
throw new ImageException($message);
166164
}));
167165
}
@@ -182,7 +180,7 @@ public static function fromString($s, & $format = NULL)
182180

183181
if (func_num_args() > 1) {
184182
$tmp = @getimagesizefromstring($s)[2]; // @ - strings smaller than 12 bytes causes read error
185-
$format = in_array($tmp, [self::JPEG, self::PNG, self::GIF], TRUE) ? $tmp : NULL;
183+
$format = isset(self::$formats[$tmp]) ? $tmp : NULL;
186184
}
187185

188186
return new static(Callback::invokeSafe('imagecreatefromstring', [$s], function ($message) {
@@ -519,20 +517,12 @@ public function place(Image $image, $left = 0, $top = 0, $opacity = 100)
519517
public function save($file = NULL, $quality = NULL, $type = NULL)
520518
{
521519
if ($type === NULL) {
522-
switch (strtolower($ext = pathinfo($file, PATHINFO_EXTENSION))) {
523-
case 'jpg':
524-
case 'jpeg':
525-
$type = self::JPEG;
526-
break;
527-
case 'png':
528-
$type = self::PNG;
529-
break;
530-
case 'gif':
531-
$type = self::GIF;
532-
break;
533-
default:
520+
$extensions = array_flip(self::$formats) + ['jpg' => self::JPEG];
521+
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
522+
if (!isset($extensions[$ext])) {
534523
throw new Nette\InvalidArgumentException("Unsupported file extension '$ext'.");
535524
}
525+
$type = $extensions[$ext];
536526
}
537527

538528
switch ($type) {
@@ -595,10 +585,10 @@ public function __toString()
595585
*/
596586
public function send($type = self::JPEG, $quality = NULL)
597587
{
598-
if (!in_array($type, [self::JPEG, self::PNG, self::GIF], TRUE)) {
588+
if (!isset(self::$formats[$type])) {
599589
throw new Nette\InvalidArgumentException("Unsupported image type '$type'.");
600590
}
601-
header('Content-Type: ' . image_type_to_mime_type($type));
591+
header('Content-Type: image/' . self::$formats[$type]);
602592
return $this->save(NULL, $quality, $type);
603593
}
604594

tests/Utils/Image.save.phpt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,21 @@ if (!extension_loaded('gd')) {
1616
}
1717

1818

19-
$main = Image::fromFile(__DIR__ . '/images/logo.gif');
19+
$main = Image::fromFile(__DIR__ . '/images/alpha1.png');
20+
21+
22+
test(function () use ($main) {
23+
$main->save(TEMP_DIR . '/foo.png');
24+
Assert::true(is_file(TEMP_DIR . '/foo.png'));
25+
Assert::same(IMAGETYPE_PNG, getimagesize(TEMP_DIR . '/foo.png')[2]);
26+
});
27+
28+
29+
test(function () use ($main) {
30+
$main->save(TEMP_DIR . '/foo.x', NULL, Image::PNG);
31+
Assert::true(is_file(TEMP_DIR . '/foo.x'));
32+
Assert::same(IMAGETYPE_PNG, getimagesize(TEMP_DIR . '/foo.x')[2]);
33+
});
2034

2135

2236
Assert::exception(function () use ($main) { // invalid image type

tests/Utils/Image.send.phpt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,31 @@ if (!extension_loaded('gd')) {
1616
}
1717

1818

19-
$main = Image::fromFile(__DIR__ . '/images/logo.gif');
19+
$main = Image::fromFile(__DIR__ . '/images/alpha1.png');
20+
21+
22+
test(function () use ($main) {
23+
ob_start();
24+
$main->send();
25+
$data = ob_get_clean();
26+
27+
Assert::contains('JFIF', $data);
28+
if (PHP_SAPI !== 'cli') {
29+
Assert::contains('Content-Type: image/jpeg', headers_list());
30+
}
31+
});
32+
33+
34+
test(function () use ($main) {
35+
ob_start();
36+
$main->send(Image::PNG);
37+
$data = ob_get_clean();
38+
39+
Assert::contains('PNG', $data);
40+
if (PHP_SAPI !== 'cli') {
41+
Assert::contains('Content-Type: image/png', headers_list());
42+
}
43+
});
2044

2145

2246
Assert::exception(function () use ($main) { // invalid image type

0 commit comments

Comments
 (0)