Skip to content

Commit ca49bdf

Browse files
committed
moved getEmbeddedImageInfo to a trait
1 parent 0da94bf commit ca49bdf

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

src/ImageInfo/Curl.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
class Curl implements ImageInfoInterface
99
{
10+
use UtilsTrait;
11+
1012
protected static $mimetypes = [
1113
'image/jpeg',
1214
'image/png',
@@ -169,22 +171,4 @@ public function writeCallback($connection, $string)
169171

170172
return -1;
171173
}
172-
173-
protected static function getEmbeddedImageInfo($content)
174-
{
175-
$pieces = explode(';', $content, 2);
176-
177-
if ((count($pieces) !== 2) || (strpos($pieces[0], 'image/') === false) || (strpos($pieces[1], 'base64,') !== 0)) {
178-
return false;
179-
}
180-
181-
$info = getimagesizefromstring(base64_decode(substr($pieces[1], 7)));
182-
183-
return [
184-
'width' => $info[0],
185-
'height' => $info[1],
186-
'size' => $info[0] * $info[1],
187-
'mime' => $info['mime'],
188-
];
189-
}
190174
}

src/ImageInfo/UtilsTrait.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Embed\ImageInfo;
4+
5+
trait UtilsTrait
6+
{
7+
/**
8+
* Extract image info from embedded images (data:image/jpeg;base64,...)
9+
*
10+
* @param string $content
11+
*
12+
* @return array|false
13+
*/
14+
protected static function getEmbeddedImageInfo($content)
15+
{
16+
$pieces = explode(';', $content, 2);
17+
18+
if ((count($pieces) !== 2) || (strpos($pieces[0], 'image/') === false) || (strpos($pieces[1], 'base64,') !== 0)) {
19+
return false;
20+
}
21+
22+
$info = getimagesizefromstring(base64_decode(substr($pieces[1], 7)));
23+
24+
return [
25+
'width' => $info[0],
26+
'height' => $info[1],
27+
'size' => $info[0] * $info[1],
28+
'mime' => $info['mime'],
29+
];
30+
}
31+
}

0 commit comments

Comments
 (0)