|
1
|
|
|
<?php |
|
2
|
|
|
namespace PlaygroundCore\Service; |
|
3
|
|
|
|
|
4
|
|
|
use ZfcBase\EventManager\EventProvider; |
|
5
|
|
|
use Zend\ServiceManager\ServiceManager; |
|
6
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* require php_exif for some methods |
|
10
|
|
|
*/ |
|
11
|
|
|
class Image extends EventProvider |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
protected $file; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var resource |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $image; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* |
|
23
|
|
|
* @var ServiceManager |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $serviceLocator; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(ServiceLocatorInterface $locator) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->serviceLocator = $locator; |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $file |
|
34
|
|
|
* @throws \Exception if the file does not exists |
|
35
|
|
|
* @return \PlaygroundCore\Service\Image |
|
36
|
|
|
*/ |
|
37
|
|
|
public function setImage($file) |
|
38
|
|
|
{ |
|
39
|
|
|
if (!file_exists($file)) { |
|
40
|
|
|
throw new \Exception('Not a file: "' . $file . '"', null, null); |
|
41
|
|
|
} |
|
42
|
|
|
$this->file = $file; |
|
43
|
|
|
$this->image = imagecreatefromstring( |
|
44
|
|
|
file_get_contents($file) |
|
45
|
|
|
); |
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return boolean |
|
51
|
|
|
*/ |
|
52
|
|
|
public function canCorrectOrientation() |
|
53
|
|
|
{ |
|
54
|
|
|
return function_exists('exif_read_data') |
|
55
|
|
|
&& (substr($this->file, -strlen('.jpg')) === '.jpg' |
|
56
|
|
|
|| substr($this->file, -strlen('.jpeg')) === '.jpeg'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Correct image orientation (if present in the medadata) |
|
61
|
|
|
* use php_exif |
|
62
|
|
|
* @return \PlaygroundCore\Service\Image |
|
63
|
|
|
*/ |
|
64
|
|
|
public function correctOrientation() |
|
65
|
|
|
{ |
|
66
|
|
|
try { |
|
67
|
|
|
$exif = exif_read_data($this->file); |
|
68
|
|
|
} catch (\Exception $e) { |
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
if (!empty($exif['Orientation'])) { |
|
72
|
|
|
switch ($exif['Orientation']) { |
|
73
|
|
|
case 8: |
|
74
|
|
|
$this->image = imagerotate($this->image, 90, 0); |
|
75
|
|
|
break; |
|
76
|
|
|
case 3: |
|
77
|
|
|
$this->image = imagerotate($this->image, 180, 0); |
|
78
|
|
|
break; |
|
79
|
|
|
case 6: |
|
80
|
|
|
$this->image = imagerotate($this->image, -90, 0); |
|
81
|
|
|
break; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* save as jpeg |
|
89
|
|
|
* @param string $path |
|
90
|
|
|
* @return \PlaygroundCore\Service\Image |
|
91
|
|
|
*/ |
|
92
|
|
|
public function save($path = null) |
|
93
|
|
|
{ |
|
94
|
|
|
if (is_null($path)) { |
|
95
|
|
|
$path = $this->file; |
|
96
|
|
|
} |
|
97
|
|
|
imagejpeg($this->image, $path); |
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* output as jpeg |
|
103
|
|
|
*/ |
|
104
|
|
|
public function __toString() |
|
105
|
|
|
{ |
|
106
|
|
|
echo imagejpeg($this->image); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.