-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.php
executable file
·144 lines (129 loc) · 4.12 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
class Yadda_Image {
const JPEG = 'jpeg';
/**
* Converts an image from one format to another.
*
* @param string $in Input filename
* @param string $out Output filename
* @param string $type Output type
* @throws Yadda_Image_Exception
* @return void
*/
public static function convert($in, $out, $type = self::JPEG) {
// read input
if (!file_exists($in)) {
throw new Yadda_Image_Exception('Input file doesn\'t exist');
}
$image = self::_read($in);
switch ($type) {
case self::JPEG:
@imagejpeg($image, $out, 100);
break;
default:
imagedestroy($image);
throw new Yadda_Image_Exception('Invalid output format');
}
// clean up
imagedestroy($image);
if (!file_exists($out)) {
throw new Yadda_Image_Exception('Error creating output');
}
}
/**
* Resizes an image.
*
* @param string $in Input filename
* @param string $out Output filename
* @param array $config Resize config. Allowed keys are:
* width - int max width
* height - int - max height
* crop - boolean - whether or not to crop the image
* @param string $type Output format
* @throws Yadda_Image_Exception
* @return void
*/
public static function resize($in, $out, array $config, $type = self::JPEG) {
// read input
if (!file_exists($in)) {
throw new Yadda_Image_Exception('Input file doesn\'t exist');
}
$source = self::_read($in);
$sourceWidth = imagesx($source);
$sourceHeight = imagesy($source);
$sourceRatio = $sourceWidth / $sourceHeight;
$destWidth = isset($config['width']) ? (int) $config['width'] : null;
$destHeight = isset($config['height']) ? (int) $config['height'] : null;
$crop = isset($config['crop']) ? (bool) $config['crop'] : false;
if ($destWidth === null || $destHeight === null) {
if ($destWidth === null) {
$destWidth = round($destHeight / $sourceHeight * $sourceWidth);
} else {
$destHeight = round($destWidth / $sourceWidth * $sourceHeight);
}
$resized = imagecreatetruecolor($destWidth, $destHeight);
imagecopyresampled($resized, $source, 0, 0, 0, 0, $destWidth, $destHeight, $sourceWidth, $sourceHeight);
} else {
$destRatio = $destWidth / $destHeight;
if ($crop === false) {
if ($destRatio > $sourceRatio) {
$destWidth = round($destHeight / $sourceHeight * $sourceWidth);
} else {
$destHeight = round($destWidth / $sourceWidth * $sourceHeight);
}
$resized = imagecreatetruecolor($destWidth, $destHeight);
imagecopyresampled($resized, $source, 0, 0, 0, 0, $destWidth, $destHeight, $sourceWidth, $sourceHeight);
} else {
$resized = imagecreatetruecolor($destWidth, $destHeight);
if ($destRatio > $sourceRatio) {
$sliverWidth = $sourceWidth;
$sourceX = 0;
$sliverHeight = round($sliverWidth / $destWidth * $destHeight);
$sourceY = round(($sourceHeight - $sliverHeight) / 2);
} else {
$sliverHeight = $sourceHeight;
$sourceY = 0;
$sliverWidth = round($sliverHeight / $destHeight * $destWidth);
$sourceX = round(($sourceWidth - $sliverWidth) / 2);
}
imagecopyresampled($resized, $source, 0, 0, $sourceX, $sourceY, $destWidth, $destHeight, $sliverWidth, $sliverHeight);
}
}
// write the resized image
switch ($type) {
case self::JPEG:
@imagejpeg($resized, $out, 100);
break;
default:
imagedestroy($source);
imagedestroy($resized);
throw new Yadda_Image_Exception('Invalid output format');
}
// clean up
imagedestroy($source);
imagedestroy($resized);
if (!file_exists($out)) {
throw new Yadda_Image_Exception('Error creating output');
}
}
/**
* Reads an image from disk into a resource.
*
* @param string $filename Input filename
* @throws Yadda_Image_Exception
* @return resource
*/
protected static function _read($filename) {
$image = @imagecreatefromjpeg($filename);
if (!$image) {
$image = @imagecreatefrompng($filename);
if (!$image) {
$image = @imagecreatefromgif($filename);
if (!$image) {
throw new Yadda_Image_Exception('Input file isn\'t a valid image');
}
}
}
return $image;
}
}