1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jackal\ImageMerge\Model\Format; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Jackal\ImageMerge\Model\File\FileObjectInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ImageReader |
10
|
|
|
* @package Jackal\ImageMerge\Model\Format |
11
|
|
|
*/ |
12
|
|
|
final class ImageReader |
13
|
|
|
{ |
14
|
|
|
const FORMAT_JPG = 'jpg'; |
15
|
|
|
const FORMAT_PNG = 'png'; |
16
|
|
|
const FORMAT_GIF = 'gif'; |
17
|
|
|
const FORMAT_WEBP = 'webp'; |
18
|
|
|
|
19
|
|
|
private $resource; |
20
|
|
|
|
21
|
|
|
private $format; |
22
|
|
|
|
23
|
|
|
private function __construct() |
24
|
|
|
{ |
25
|
|
|
//IMAGETYPE_WEBP is available only from php 7.1 |
26
|
|
|
if (!defined('IMAGETYPE_WEBP')) { |
27
|
|
|
define('IMAGETYPE_WEBP', 18); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param FileObjectInterface $filename |
33
|
|
|
* @return ImageReader |
34
|
|
|
* @throws Exception |
35
|
|
|
*/ |
36
|
|
|
public static function fromPathname(FileObjectInterface $filename) |
37
|
|
|
{ |
38
|
|
|
$ir = new self(); |
39
|
|
|
$ir->resource = @imagecreatefromstring($filename->getContents()); |
40
|
|
|
|
41
|
|
|
switch ($ir->getExifType($filename)) { |
42
|
|
|
case IMAGETYPE_PNG:{ |
43
|
|
|
$ir->format = self::FORMAT_PNG; |
44
|
|
|
|
45
|
|
|
break; |
46
|
|
|
} |
47
|
|
|
case IMAGETYPE_JPEG:{ |
48
|
|
|
$ir->format = self::FORMAT_JPG; |
49
|
|
|
|
50
|
|
|
break; |
51
|
|
|
} |
52
|
|
|
case IMAGETYPE_GIF:{ |
53
|
|
|
$ir->format = self::FORMAT_GIF; |
54
|
|
|
|
55
|
|
|
break; |
56
|
|
|
} |
57
|
|
|
case IMAGETYPE_WEBP:{ |
58
|
|
|
$ir->format = self::FORMAT_WEBP; |
59
|
|
|
|
60
|
|
|
break; |
61
|
|
|
} |
62
|
|
|
default: { |
63
|
|
|
throw new Exception(sprintf('File is not a valid image type [extension: "%s"]', |
64
|
|
|
$ir->getExtension($filename)) |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $ir; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function getExtension(FileObjectInterface $filename) |
74
|
|
|
{ |
75
|
|
|
return strtolower(pathinfo($filename->getPathname(), PATHINFO_EXTENSION)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function getExifType(FileObjectInterface $filename) |
79
|
|
|
{ |
80
|
|
|
$imageType = exif_imagetype($filename->getPathname()); |
81
|
|
|
|
82
|
|
|
if (!$imageType) { |
83
|
|
|
//since IMAGETYPE_WEBP is available only from php 7.1, we guess the type base from the extension |
84
|
|
|
if (version_compare(PHP_VERSION, '7.1.0', '<') and $this->getExtension($filename) == 'webp') { |
85
|
|
|
return IMAGETYPE_WEBP; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $imageType; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function getFormat() |
96
|
|
|
{ |
97
|
|
|
return $this->format; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return resource |
102
|
|
|
*/ |
103
|
|
|
public function getResource() |
104
|
|
|
{ |
105
|
|
|
return $this->resource; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|