1: <?php
2:
3: namespace webfilesframework\core\datasystem\file\format\media\image;
4:
5:
6: use webfilesframework\core\datasystem\file\format\media\image\handler\MGdHandler;
7: use webfilesframework\core\datasystem\file\format\media\image\handler\MImageMagickHandler;
8: use webfilesframework\core\datasystem\file\system\MFile;
9: use webfilesframework\core\datatypes\time\MTimestampHelper;
10:
11:
12: 13: 14: 15: 16: 17:
18: class MImage extends MFile
19: {
20:
21: protected $m_oImage;
22:
23: protected $m_sType;
24:
25: protected $handler;
26:
27:
28: public function __construct($filePath, $loadImageResourceOnCreation = false, $type = "jpg") {
29:
30: parent::__construct($filePath);
31:
32: $this->m_sType = $type;
33:
34: if ($loadImageResourceOnCreation) {
35: $this->loadImage();
36: }
37: }
38:
39: 40: 41:
42: public function loadImage() {
43:
44: if (MImage::isImageMagickInstalled() && false) {
45: $this->handler = new MImageMagickHandler();
46: } else if (MImage::isGdInstalled()) {
47: $this->handler = new MGdHandler();
48: } else {
49:
50: }
51:
52: if ($this->exists()) {
53: if ($this->m_sType == "jpg") {
54: $this->m_oImage = $this->handler->loadJpg($this->getPath());
55: } else if ($this->m_sType == "png") {
56: $this->m_oImage = $this->handler->loadPng($this->getPath());
57: }
58: }
59: }
60:
61: public static function isImageMagickInstalled()
62: {
63: return (function_exists('NewMagickWand') === TRUE);
64: }
65:
66: public static function isGdInstalled()
67: {
68: if (function_exists("gd_info")) {
69: return true;
70: }
71: return false;
72: }
73:
74: 75: 76:
77: public static function isGd2Installed()
78: {
79: if (function_exists("gd_info")) {
80: $info = gd_info();
81: if (substr_count($info["GD Version"], "2")) {
82:
83: return true;
84: }
85: }
86: return false;
87: }
88:
89: public function destroy()
90: {
91: imagedestroy($this->m_oImage);
92: }
93:
94: 95: 96:
97: public function outputInBrowser()
98: {
99:
100: if ($this->m_sType == "jpg") {
101: header("Content-Type: image/jpeg");
102: imagejpeg($this->m_oImage);
103: } else if ($this->m_sType == "png") {
104: header("Content-Type: image/png");
105: imagepng($this->m_oImage);
106: }
107:
108: imagedestroy($this->m_oImage);
109: }
110:
111: public function readExifDate() {
112: $exifData = @exif_read_data($this->getPath());
113:
114: if ( isset($exifData['DateTimeDigitized']) ) {
115: return MTimestampHelper::getTimestampFromExifFormatedDateTime(
116: $exifData['DateTimeDigitized']);
117: } else if ( isset($exifData['DateTimeOriginal']) ) {
118: return MTimestampHelper::getTimestampFromExifFormatedDateTime(
119: $exifData['DateTimeOriginal']);
120: } else {
121: echo "warn: " . $this->getPath() . " does not have defined exifdate.\n";
122:
123: }
124: return null;
125: }
126:
127: 128: 129:
130: public function outputAsDownload()
131: {
132:
133: if ($this->m_sType == "jpg") {
134: header("Content-Type: image/jpeg");
135: imagejpeg($this->m_oImage, $this->getPath());
136: } else if ($this->m_sType == "png") {
137: header("Content-Type: image/png");
138: imagepng($this->m_oImage, $this->getPath());
139: }
140:
141: imagedestroy($this->m_oImage);
142:
143: }
144:
145: 146: 147: 148: 149:
150: public function saveAsFile($filePath = "", $quality = 80)
151: {
152:
153: if (!empty($filePath)) {
154: $sFilePath = $filePath;
155: } else {
156: $sFilePath = $this->getPath();
157: }
158:
159: if ($this->m_sType == "jpg") {
160:
161: imagejpeg($this->m_oImage, $sFilePath, $quality);
162: } else if ($this->m_sType == "png") {
163:
164: imagepng($this->m_oImage, $sFilePath);
165: }
166:
167: }
168:
169: 170: 171: 172: 173: 174: 175: 176: 177: 178:
179: public function saveScaledImgAsFile($width, $height, $filePath = "")
180: {
181:
182: if (!$this->exists()) {
183: $iErrorCode = 30;
184: throw new \Exception(
185: "File does not exists. Given File in the Image Object has to be created or has to be an existent file.",
186: $iErrorCode
187: );
188: }
189:
190: if (!empty($filePath)) {
191: $sFilePath = $filePath;
192: } else {
193: $sFilePath = $this->getPath();
194: }
195:
196: $oScaledImage = imagecreatetruecolor($width, $height);
197: imagecopyresampled($oScaledImage, $this->m_oImage, 0, 0, 0, 0, $width, $height, $this->getImageWidth(), $this->getImageHeight());
198:
199: if ($this->m_sType == "jpg") {
200:
201: imagejpeg($oScaledImage, $sFilePath);
202: } else if ($this->m_sType == "png") {
203:
204: imagepng($oScaledImage, $sFilePath);
205: }
206: imagedestroy($oScaledImage);
207: }
208:
209: public function getImageWidth()
210: {
211:
212: $oImageSize = getimagesize($this->getPath());
213:
214:
215: $sSizeString = $oImageSize[3];
216: $oSizeStringArray = explode("\"", $sSizeString);
217: return $oSizeStringArray[1];
218: }
219:
220: public function getImageHeight()
221: {
222:
223: $oImageSize = getimagesize($this->getPath());
224:
225:
226: $sSizeString = $oImageSize[3];
227: $oSizeStringArray = explode("\"", $sSizeString);
228: return $oSizeStringArray[3];
229: }
230:
231:
232: public function saveScaledImgAsFileWithHeight($p_iHeight, $p_sFilePath = "")
233: {
234:
235: $iWidth = $this->getImageWidth();
236: $iHeight = $this->getImageHeight();
237: $iProportion = $p_iHeight / $iHeight;
238:
239: $iNewWidth = ceil($iProportion * $iWidth);
240:
241: $this->saveScaledImgAsFile($iNewWidth, $p_iHeight, $p_sFilePath);
242: }
243:
244: public function saveScaledImgAsFileWithWidth($p_iWidth, $p_sFilePath = "")
245: {
246:
247: $iWidth = $this->getImageWidth();
248: $iProportion = $p_iWidth / $iWidth;
249: $iHeight = $this->getImageHeight();
250:
251: $iNewHeight = ceil($iProportion * $iHeight);
252:
253: $this->saveScaledImgAsFile($p_iWidth, $iNewHeight, $p_sFilePath);
254: }
255:
256:
257: public function saveScaledImgAsFileWithBiggerSize($p_iBiggerSize, $p_sFilePath = "")
258: {
259:
260:
261: ini_set ('gd.jpeg_ignore_warning', 1);
262: if ($this->getImageWidth() > $this->getImageHeight()) {
263: $this->saveScaledImgAsFileWithWidth($p_iBiggerSize, $p_sFilePath);
264: } else {
265: $this->saveScaledImgAsFileWithHeight($p_iBiggerSize, $p_sFilePath);
266: }
267: }
268:
269: 270: 271: 272: 273: 274: 275:
276: public function saveScaledImgAsFileWithPercent($p_iPercent, $p_sFilePath = "")
277: {
278:
279: $iNewHeigt = ceil($this->getImageHeight() * ($p_iPercent / 100));
280: $iNewWidth = ceil($this->getImageWidth() * ($p_iPercent / 100));
281:
282: $this->saveScaledImgAsFile($iNewWidth, $iNewHeigt, $p_sFilePath);
283: }
284:
285: 286: 287:
288: public function detroy()
289: {
290: imagedestroy($this->m_oImage);
291: }
292:
293: 294: 295:
296: public function mirror()
297: {
298:
299: $img_x = imagesx($this->m_oImage);
300: $img_y = imagesy($this->m_oImage);
301:
302: $oTempImage = imagecreatetruecolor($img_x, $img_y);
303: imagecopyresampled($oTempImage, $this->m_oImage, 0, 0, $img_x, 0, $img_x, $img_y, -$img_x, $img_y);
304: $this->m_oImage = $oTempImage;
305: }
306:
307: public function __toString()
308: {
309: return "<img src=\"" . $this->getPath() . "\" width=\"300\"><br />";
310: }
311:
312: public static function hasImageTypeExtension(MFile $image)
313: {
314: return ($image->getExtension() == "jpg"
315: || $image->getExtension() == "jpeg"
316: || $image->getExtension() == "png");
317: }
318:
319: }