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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
<?php
/*
* Copyright 2014 Florian "Bluewind" Pritz <bluewind@server-speed.net>
*
* Licensed under AGPLv3
* (see COPYING for full license text)
*
*/
class Imglib {
/*
* This returns a square thumbnail for the input image
* Source: http://salman-w.blogspot.co.at/2009/04/crop-to-fit-image-using-aspphp.html
*/
public function makeThumb($file, $size = 150, $target_type = null)
{
$source_gdim = imagecreatefromstring(file_get_contents($file));
if ($source_gdim === false) {
show_error("Unsupported image type");
}
imagealphablending($source_gdim,false);
imagesavealpha($source_gdim,true);
list($source_width, $source_height, $source_type) = getimagesize($file);
if ($target_type === null) {
$target_type = $source_type;
}
$target_width = $size;
$target_height = $size;
$source_aspect_ratio = $source_width / $source_height;
$desired_aspect_ratio = $target_width / $target_height;
if ($source_aspect_ratio > $desired_aspect_ratio) {
// Triggered when source image is wider
$temp_height = $target_height;
$temp_width = round(($target_height * $source_aspect_ratio));
} else {
// Triggered otherwise (i.e. source image is similar or taller)
$temp_width = $target_width;
$temp_height = round(($target_width / $source_aspect_ratio));
}
/*
* Resize the image into a temporary GD image
*/
$temp_gdim = imagecreatetruecolor($temp_width, $temp_height);
imagealphablending($temp_gdim,false);
imagesavealpha($temp_gdim,true);
imagecopyresampled(
$temp_gdim,
$source_gdim,
0, 0,
0, 0,
$temp_width, $temp_height,
$source_width, $source_height
);
/*
* Copy cropped region from temporary image into the desired GD image
*/
$x0 = ($temp_width - $target_width) / 2;
$y0 = ($temp_height - $target_height) / 2;
$thumb = imagecreatetruecolor($target_width, $target_height);
imagealphablending($thumb,false);
imagesavealpha($thumb,true);
imagecopy(
$thumb,
$temp_gdim,
0, 0,
$x0, $y0,
$target_width, $target_height
);
/*
* Fix orientation according to exif tag
*/
try {
$exif = exif_read_data($file);
} catch (ErrorException $e) {
}
if (isset($exif['Orientation'])) {
$mirror = false;
$deg = 0;
switch ($exif['Orientation']) {
case 2:
$mirror = true;
break;
case 3:
$deg = 180;
break;
case 4:
$deg = 180;
$mirror = true;
break;
case 5:
$deg = 270;
$mirror = true;
break;
case 6:
$deg = 270;
break;
case 7:
$deg = 90;
$mirror = true;
break;
case 8:
$deg = 90;
break;
}
if ($deg) $thumb = imagerotate($thumb, $deg, 0);
if ($mirror) $thumb = $this->_mirrorImage($thumb);
}
ob_start();
switch ($target_type) {
case IMAGETYPE_GIF:
$ret = imagegif($thumb);
break;
case IMAGETYPE_JPEG:
$ret = imagejpeg($thumb);
break;
case IMAGETYPE_PNG:
$ret = imagepng($thumb);
break;
default:
assert(0);
}
$result = ob_get_clean();
if (!$ret) {
show_error("Failed to create thumbnail");
}
imagedestroy($thumb);
imagedestroy($temp_gdim);
imagedestroy($source_gdim);
return $result;
}
private function _mirrorImage($imgsrc)
{
$width = imagesx($imgsrc);
$height = imagesy($imgsrc);
$src_x = $width -1;
$src_y = 0;
$src_width = -$width;
$src_height = $height;
$imgdest = imagecreatetruecolor($width, $height);
imagealphablending($imgdest,false);
imagesavealpha($imgdest,true);
if (imagecopyresampled($imgdest, $imgsrc, 0, 0, $src_x, $src_y, $width, $height, $src_width, $src_height)) {
return $imgdest;
}
return $imgsrc;
}
}
|