-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.php
82 lines (73 loc) · 1.93 KB
/
Image.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php namespace mjolnir\base;
/**
* @package mjolnir
* @category Base
* @author Ibidem Team
* @copyright (c) 2013, Ibidem Team
* @license https://github.com/ibidem/ibidem/blob/master/LICENSE.md
*/
class Image
{
/**
* Removes Orientation metadata and rotates image to match 0 degrees
* relative to the orientation. If the image does not contain an Orientation
* this function does nothing.
*
* [!!] The path may be altered during the process.
*
* @return string potentially altered image path
*/
static function removeorientation($imagepath)
{
$meta = \exif_read_data($imagepath);
if ( ! empty($meta['Orientation']))
{
switch ($meta['Orientation'])
{
case 8:
return static::rotateimage($imagepath, 90);
break;
case 3:
return static::rotateimage($imagepath, 180);
break;
case 6:
return static::rotateimage($imagepath, -90);
break;
}
}
return $imagepath;
}
/**
* Rotates the given file the given degrees.
*
* The function may alter the image such as converting it to jpeg, in the
* event there are no other means of processing it.
*
* @return string potentially altered image path
*/
static function rotateimage($imagepath, $degrees)
{
$parts = null;
if (\preg_match('#(?P<path>.*)\.png$#', $imagepath, $parts))
{
$source = \imagecreatefrompng($imagepath);
if ( ! $source)
{
throw new \Exception('Error opening file '.$imagepath);
}
\imagealphablending($source, false);
\imagesavealpha($source, true);
$image = \imagerotate($source, $degrees, \imageColorAllocateAlpha($source, 0, 0, 0, 127));
\imagealphablending($image, false);
\imagesavealpha($image, true);
\imagepng($image, $imagepath, 9);
}
else # non-png
{
$source = \imagecreatefromstring(\file_get_contents($imagepath));
$image = \imagerotate($source, $degrees, 0);
\imagejpeg($image, $imagepath, 90);
}
return $imagepath;
}
} # class