This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pnmedia_imagegdapi.php
298 lines (249 loc) · 11.1 KB
/
pnmedia_imagegdapi.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
// $Id$
//
// Mediashare by Jorn Lind-Nielsen (C)
//
class mediashare_imageHandlerGD
{
function getTitle()
{
$dom = ZLanguage::getModuleDomain('mediashare');
return __('Mediashare GD Image Handler', $dom);
}
function getMediaTypes()
{
return array(
array('mimeType' => 'image/pjpeg',
'fileType' => 'jpg',
'foundMimeType' => 'image/jpeg',
'foundFileType' => 'jpg'),
array('mimeType' => 'image/jpeg',
'fileType' => 'jpeg',
'foundMimeType' => 'image/jpeg',
'foundFileType' => 'jpg'),
array('mimeType' => 'image/png',
'fileType' => 'png',
'foundMimeType' => 'image/png',
'foundFileType' => 'png'),
array('mimeType' => 'image/gif',
'fileType' => 'gif',
'foundMimeType' => 'image/gif',
'foundFileType' => 'gif'));
}
function createPreviews($args, $previews)
{
$mediaFilename = $args['mediaFilename'];
$mimeType = $args['mimeType'];
$mediaFileType = $args['fileType'];
$originalImageData = $this->createImageFromFile($mediaFilename, $mimeType);
if ($originalImageData == false) {
return false;
}
$result = array();
foreach ($previews as $preview)
{
$imPreview = $this->createResizedImage($originalImageData, $mimeType, $preview['imageSize']);
if ($mimeType == 'image/jpeg' && !$preview['isThumbnail']) {
imagejpeg($imPreview, $preview['outputFilename'], 90);
$result[] = array('fileType' => 'jpg',
'mimeType' => 'image/jpg',
'width' => imagesx($imPreview),
'height' => imagesy($imPreview),
'bytes' => filesize($preview['outputFilename']));
} else {
imagepng($imPreview, $preview['outputFilename']);
$result[] = array('fileType' => 'png',
'mimeType' => 'image/png',
'width' => imagesx($imPreview),
'height' => imagesy($imPreview),
'bytes' => filesize($preview['outputFilename']));
}
imagedestroy($imPreview);
}
$result[] = array('fileType' => $mediaFileType,
'mimeType' => $mimeType,
'width' => imagesx($originalImageData),
'height' => imagesy($originalImageData),
'bytes' => filesize($mediaFilename));
imagedestroy($originalImageData);
return $result;
}
function getMediaDisplayHtml($url, $width, $height, $id, $args)
{
if ((string) (int)$width == "$width") {
$width = "{$width}px";
}
$widthHtml = ($width == null ? '' : " width:{$width}");
$heightHtml = ($height == null ? '' : " height:$height");
$onclickHtml = (empty($args['onclick']) ? '' : " onclick=\"$args[onclick]\"");
$classHtml = (empty($args['class']) ? '' : " class=\"$args[class]\"");
$idHtml = ($id != '' ? " id=\"$id\"" : '');
$style = " style=\"$widthHtml$heightHtml\"";
$title = (isset($args['title']) ? htmlspecialchars($args['title']) : '');
$url = pnGetBaseURI() . '/' . $url;
$html = "<img src=\"$url\"$style$idHtml title=\"$title\" alt=\"$title\"$onclickHtml$classHtml/>";
if (isset($args['url'])) {
$rel = (isset($args['urlRel']) ? " rel=\"$args[urlRel]\"" : '');
$html = "<a href=\"$args[url]\"$rel>$html</a>";
}
return $html;
}
// Internal functions
function createImageFromFile($filename, $mimeType)
{
$dom = ZLanguage::getModuleDomain('mediashare');
$im = false;
if ($mimeType == 'image/jpeg') {
$im = @imagecreatefromjpeg($filename);
} else if ($mimeType == 'image/gif') {
$im = @imagecreatefromgif($filename);
} else if ($mimeType == 'image/png') {
$im = @imagecreatefrompng($filename);
}
if (!$im) {
return LogUtil::registerError(__f('Unknown image format %1$s/%2$s.', array($mimeType, $filename), $dom));
}
return $im;
}
function createResizedImage(&$im, $mimeType, $newSize)
{
$xs = imagesx($im);
$ys = imagesy($im);
// Calculate thumbnail X and Y sizes
if ($xs > $ys) {
$resizedXSize = $newSize;
$resizedYSize = ($ys * $newSize) / $xs;
} else {
$resizedYSize = $newSize;
$resizedXSize = ($xs * $newSize) / $ys;
}
// If thumbnail area becomes bigger than original, then use original's size
if ($resizedXSize * $resizedYSize >= $xs * $ys) {
$resizedXSize = $xs;
$resizedYSize = $ys;
}
$isTrueColor = false;
$thumbnail = $this->createEmptyImage($resizedXSize, $resizedYSize, $mimeType, $isTrueColor);
imagealphablending($thumbnail, false);
imagesavealpha($thumbnail, true);
$white = imagecolorallocate($thumbnail, 255, 255, 255); // First allocated - becomes background
$gray = imagecolorallocate($thumbnail, 100, 100, 100);
// Copy resized image into center of thumbnail
if ($isTrueColor && function_exists("imagecopyresampled")) {
if (!@imagecopyresampled($thumbnail, $im, 0, 0, 0, 0, $resizedXSize, $resizedYSize, $xs, $ys)) {
mediashareImageCopyResampleBicubic($thumbnail, $im, 0, 0, 0, 0, $resizedXSize, $resizedYSize, $xs, $ys);
}
} else {
imagecopyresized($thumbnail, $im, 0, 0, 0, 0, $resizedXSize, $resizedYSize, $xs, $ys);
}
if (pnModGetVar('mediashare', 'enableSharpen')) {
$thumbnailEnhanced = $this->UnsharpMask($thumbnail, 80, .5, 3);
} else {
$thumbnailEnhanced = $thumbnail;
}
return $thumbnailEnhanced;
}
// Unsharp mask algorithm by Torstein H�nsi 2003 (thoensi_at_netcom_dot_no)
// Modified by Christoph Erdmann: changed it a little, cause i could not reproduce the
// darker blurred image, now it is up to 15% faster with same results
// See http://www.cerdmann.com/thumb/
// Adopted for Mediashare by Jorn Wildt
function UnsharpMask($img, $amount, $radius, $threshold)
{
// Attempt to calibrate the parameters to Photoshop:
if ($amount > 500) {
$amount = 500;
}
$amount = $amount * 0.016;
if ($radius > 50) {
$radius = 50;
}
$radius = $radius * 2;
if ($threshold > 255) {
$threshold = 255;
}
$radius = abs(round($radius)); // Only integers make sense.
if ($radius == 0) {
return null;
}
$w = imagesx($img);
$h = imagesy($img);
$imgCanvas = $img;
$imgCanvas2 = $img;
$imgBlur = imagecreatetruecolor($w, $h);
// Gaussian blur matrix:
// 1 2 1
// 2 4 2
// 1 2 1
// Move copies of the image around one pixel at the time and merge them with weight
// according to the matrix. The same matrix is simply repeated for higher radii.
for ($i = 0; $i < $radius; $i++) {
imagecopy($imgBlur, $imgCanvas, 0, 0, 1, 1, $w - 1, $h - 1); // up left
imagecopymerge($imgBlur, $imgCanvas, 1, 1, 0, 0, $w, $h, 50); // down right
imagecopymerge($imgBlur, $imgCanvas, 0, 1, 1, 0, $w - 1, $h, 33.33333); // down left
imagecopymerge($imgBlur, $imgCanvas, 1, 0, 0, 1, $w, $h - 1, 25); // up right
imagecopymerge($imgBlur, $imgCanvas, 0, 0, 1, 0, $w - 1, $h, 33.33333); // left
imagecopymerge($imgBlur, $imgCanvas, 1, 0, 0, 0, $w, $h, 25); // right
imagecopymerge($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 20); // up
imagecopymerge($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 16.666667); // down
imagecopymerge($imgBlur, $imgCanvas, 0, 0, 0, 0, $w, $h, 50); // center
}
$imgCanvas = $imgBlur;
// Calculate the difference between the blurred pixels and the original and set the pixels
for ($x = 0; $x < $w; $x++) {
// each row
for ($y = 0; $y < $h; $y++) {
// each pixel
$rgbOrig = ImageColorAt($imgCanvas2, $x, $y);
$rOrig = (($rgbOrig >> 16) & 0xFF);
$gOrig = (($rgbOrig >> 8) & 0xFF);
$bOrig = ($rgbOrig & 0xFF);
$rgbBlur = ImageColorAt($imgCanvas, $x, $y);
$rBlur = (($rgbBlur >> 16) & 0xFF);
$gBlur = (($rgbBlur >> 8) & 0xFF);
$bBlur = ($rgbBlur & 0xFF);
// When the masked pixels differ less from the original
// than the threshold specifies, they are set to their original value.
$rNew = (abs($rOrig - $rBlur) >= $threshold) ? max(0, min(255, ($amount * ($rOrig - $rBlur)) + $rOrig)) : $rOrig;
$gNew = (abs($gOrig - $gBlur) >= $threshold) ? max(0, min(255, ($amount * ($gOrig - $gBlur)) + $gOrig)) : $gOrig;
$bNew = (abs($bOrig - $bBlur) >= $threshold) ? max(0, min(255, ($amount * ($bOrig - $bBlur)) + $bOrig)) : $bOrig;
if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew)) {
$pixCol = ImageColorAllocate($img, $rNew, $gNew, $bNew);
ImageSetPixel($img, $x, $y, $pixCol);
}
}
}
return $img;
}
function createEmptyImage($xsize, $ysize, $mimeType, &$isTrueColor)
{
// Detect presense of ImageCreateTrueColor() - although this apparently doesn't say anything about GD2 existing ...
$hasTrueColorImage = false;
$gdfunctions = get_extension_funcs("gd");
foreach ($gdfunctions as $f) {
if ($f == 'imagecreatetruecolor') {
$hasTrueColorImage = true;
}
}
// If the check for imagecreatetruecolor fails then insert the next line
// $hasTrueColorImage = false;
$isTrueColor = false;
// Create actual image
if (/*$mimeType == 'image/gif' || */!$hasTrueColorImage) {
$image = ImageCreate($xsize, $ysize); // Create white background image
} else {
$image = @ImageCreateTrueColor($xsize, $ysize); // Create black background image
// Didn't work ... perhaps ImageCreateTrueColor doesn't exist?
if (!$image) {
$image = ImageCreate($xsize, $ysize); // Create white background image
} else {
$isTrueColor = true;
}
}
return $image;
}
}
function mediashare_media_imagegdapi_buildHandler($args)
{
return new mediashare_imageHandlerGD();
}