Skip to content

Commit 99d28e9

Browse files
committed
qual: de-duplicate code for image size calculation
1 parent 43ee1c4 commit 99d28e9

File tree

5 files changed

+43
-83
lines changed

5 files changed

+43
-83
lines changed

src/Format/JPEG.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,12 @@ public static function fromResource($gd)
8181
*/
8282
public static function fromString($string)
8383
{
84-
$imageSize = getimagesizefromstring($string);
85-
if ($imageSize === false) {
86-
throw new \Exception('Invalid image data');
87-
}
88-
89-
$width = $imageSize[0];
90-
$height = $imageSize[1];
91-
9284
$stream = fopen('php://temp', 'r+');
9385
fwrite($stream, $string);
9486
rewind($stream);
9587

9688
$jpeg = self::fromStream($stream);
97-
98-
$jpeg->width = $width;
99-
$jpeg->height = $height;
89+
$jpeg->setSizeFromString($string);
10090

10191
return $jpeg;
10292
}
@@ -121,6 +111,8 @@ public static function fromImagick(\Imagick $imagick)
121111
*
122112
* @return self
123113
* @throws \Exception
114+
*
115+
* @todo calculate and set image size
124116
*/
125117
public static function fromStream($fileHandle)
126118
{
@@ -206,18 +198,8 @@ public static function fromFile($filename)
206198
throw new \Exception(sprintf('Could not open file %s', $filename));
207199
}
208200

209-
$imageSize = getimagesize($filename);
210-
if ($imageSize === false) {
211-
throw new \Exception(sprintf('Could not get image size for %s', $filename));
212-
}
213-
214-
$width = $imageSize[0];
215-
$height = $imageSize[1];
216-
217201
$jpeg = self::fromStream($fileHandle);
218-
219-
$jpeg->width = $width;
220-
$jpeg->height = $height;
202+
$jpeg->setSizeFromFile($filename);
221203

222204
return $jpeg;
223205
}

src/Format/PNG.php

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public function __construct($contents)
4040
}
4141

4242
$this->chunks = $this->getChunksFromContents($contents);
43+
44+
$this->setSizeFromString($contents);
4345
}
4446

4547
/**
@@ -101,20 +103,12 @@ public function getIptc()
101103
*/
102104
public static function fromFile($filename)
103105
{
104-
$imageSize = getimagesize($filename);
105-
if ($imageSize === false) {
106-
throw new \Exception(sprintf('Could not get image size for %s', $filename));
106+
$contents = file_get_contents($filename);
107+
if ($contents === false) {
108+
throw new \Exception(sprintf('Could not open file %s', $filename));
107109
}
108110

109-
$width = $imageSize[0];
110-
$height = $imageSize[1];
111-
112-
$png = new self(file_get_contents($filename));
113-
114-
$png->width = $width;
115-
$png->height = $height;
116-
117-
return $png;
111+
return new self($contents);
118112
}
119113

120114
/**
@@ -124,20 +118,7 @@ public static function fromFile($filename)
124118
*/
125119
public static function fromString($string)
126120
{
127-
$imageSize = getimagesizefromstring($string);
128-
if ($imageSize === false) {
129-
throw new \Exception('Invalid PNG data');
130-
}
131-
132-
$width = $imageSize[0];
133-
$height = $imageSize[1];
134-
135-
$png = new self($string);
136-
137-
$png->width = $width;
138-
$png->height = $height;
139-
140-
return $png;
121+
return new self($string);
141122
}
142123

143124
/**

src/Format/PSD.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use CSD\Image\Metadata\Exif;
66
use CSD\Image\Metadata\Iptc;
7+
use CSD\Image\Metadata\UnsupportedException;
78
use CSD\Image\Metadata\Xmp;
89
use CSD\Image\Image;
910

@@ -70,6 +71,8 @@ public static function fromResource($gd)
7071

7172
/**
7273
* Load PSD from string.
74+
*
75+
* @todo calculate and set image size
7376
*/
7477
public static function fromString($string)
7578
{
@@ -96,6 +99,8 @@ public static function fromImagick(\Imagick $imagick)
9699
*
97100
* @return self
98101
* @throws \Exception
102+
*
103+
* @todo calculate and set image size
99104
*/
100105
public static function fromStream($fileHandle)
101106
{
@@ -194,7 +199,9 @@ public static function fromFile($filename)
194199
throw new \Exception(sprintf('Could not open file %s', $filename));
195200
}
196201

197-
return self::fromStream($fileHandle);
202+
$psd = self::fromStream($fileHandle);
203+
$psd->setSizeFromFile($filename);
204+
return $psd;
198205
}
199206

200207
/**

src/Format/WebP.php

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public function __construct($contents)
4747
if (!$this->isExtendedFormat()) {
4848
// throw new \Exception('Only extended WebP format is supported');
4949
}
50+
51+
$this->setSizeFromString($contents);
5052
}
5153

5254
/**
@@ -144,20 +146,12 @@ public function getIptc()
144146
*/
145147
public static function fromFile($filename)
146148
{
147-
$imageSize = getimagesize($filename);
148-
if ($imageSize === false) {
149-
throw new \Exception(sprintf('Could not get image size for %s', $filename));
149+
$contents = file_get_contents($filename);
150+
if ($contents === false) {
151+
throw new \Exception(sprintf('Could not open file %s', $filename));
150152
}
151153

152-
$width = $imageSize[0];
153-
$height = $imageSize[1];
154-
155-
$webp = new self(file_get_contents($filename));
156-
157-
$webp->width = $width;
158-
$webp->height = $height;
159-
160-
return $webp;
154+
return new self($contents);
161155
}
162156

163157

@@ -168,20 +162,7 @@ public static function fromFile($filename)
168162
*/
169163
public static function fromString($string)
170164
{
171-
$imageSize = getimagesizefromstring($string);
172-
if ($imageSize === false) {
173-
throw new \Exception('Invalid WebP data');
174-
}
175-
176-
$width = $imageSize[0];
177-
$height = $imageSize[1];
178-
179-
$webp = new self($string);
180-
181-
$webp->width = $width;
182-
$webp->height = $height;
183-
184-
return $webp;
165+
return new self($string);
185166
}
186167

187168
/**

src/Image.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,6 @@ public static function fromFile($fileName)
198198
if (!$result) {
199199
throw new \Exception('Unrecognised file name');
200200
}
201-
202-
$size = getimagesize($fileName);
203-
$result->width = $size[0];
204-
$result->height = $size[1];
205201
return $result;
206202
}
207203

@@ -219,8 +215,6 @@ public static function fromString($string)
219215
return false;
220216
}
221217

222-
$width = $imageInfo[0];
223-
$height = $imageInfo[1];
224218
$mime = $imageInfo['mime'];
225219

226220
$mimeToClass = [
@@ -232,11 +226,26 @@ public static function fromString($string)
232226
if (isset($mimeToClass[$mime])) {
233227
$class = $mimeToClass[$mime];
234228
$image = $class::fromString($string);
235-
$image->width = $width;
236-
$image->height = $height;
237229
return $image;
238230
}
239231

240232
return false;
241233
}
234+
235+
protected function setSizeFromFile($fileName)
236+
{
237+
$imageSize = getimagesize($fileName);
238+
if ($imageSize === false) {
239+
throw new \Exception(sprintf('Could not get image size for %s', $fileName));
240+
}
241+
$this->width = $imageSize[0];
242+
$this->height = $imageSize[1];
243+
}
244+
245+
protected function setSizeFromString($string)
246+
{
247+
$size = getimagesizefromstring($string);
248+
$this->width = $size[0];
249+
$this->height = $size[1];
250+
}
242251
}

0 commit comments

Comments
 (0)