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
|
<?php
namespace Bitweaver\Liberty;
use Bitweaver\KernelTools;
/**
* $Header$
*
* Image processor - extension: php-gd
* @package liberty
* @subpackage plugins_processor
* @author spider <spider@steelsun.com>
*/
/**
* liberty_gd_resize_image
*
* @param array $pFileHash
* @access public
* @return bool true on success, false on failure - mErrors will contain reason for failure
*/
function liberty_gd_resize_image( &$pFileHash ) {
global $gBitSystem;
$ret = null;
list($iwidth, $iheight, $itype, $iattr) = @getimagesize( $pFileHash['source_file'] );
list($type, $ext) = explode( '/', strtolower( $pFileHash['type'] ) );
if( ( empty( $pFileHash['max_width'] ) || empty( $pFileHash['max_height'] ) ) || ( $iwidth <= $pFileHash['max_width'] && $iheight <= $pFileHash['max_height'] && ( $ext == 'gif' || $ext == 'png' || $ext == 'jpg' || $ext == 'jpeg' ) ) ) {
// Keep the same dimensions as input file
$pFileHash['max_width'] = $iwidth;
$pFileHash['max_height'] = $iheight;
} elseif( $iheight && (($iwidth / $iheight) > 0) && !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] ) ) {
// we have a portrait image, flip everything
$temp = $pFileHash['max_width'];
$pFileHash['max_height'] = $pFileHash['max_width'];
$pFileHash['max_width'] = $temp;
}
// we need to scale and/or reformat
$fp = fopen( $pFileHash['source_file'], "rb" );
$data = fread( $fp, filesize( $pFileHash['source_file'] ) );
fclose ($fp);
if( function_exists( "ImageCreateFromString" ) ) {
$img = @imagecreatefromstring($data);
}
if( !empty( $img ) ) {
$size_x = imagesx($img);
$size_y = imagesy($img);
}
if( !empty( $img ) && $size_x && $size_y ) {
if( $size_x > $size_y && !empty( $pFileHash['max_width'] ) ) {
$tscale = (int)$size_x / $pFileHash['max_width'];
} elseif( !empty( $pFileHash['max_height'] ) ) {
$tscale = (int)$size_y / $pFileHash['max_height'];
} else {
$tscale = 1;
}
$tw = (int)( $size_x / $tscale );
$ty = (int)( $size_y / $tscale );
if( get_gd_version() > 1 ) {
$t = imagecreatetruecolor( $tw, $ty );
imagesavealpha( $t, true );
imagealphablending( $t, false );
imagecopyresampled( $t, $img, 0, 0, 0, 0, $tw, $ty, $size_x, $size_y );
} else {
$t = imagecreate( $tw, $ty );
//$imagegallib->ImageCopyResampleBicubic($t, $img, 0, 0, 0, 0, $tw, $ty, $size_x, $size_y);
}
// override $mimeExt if we have a custom setting for it
if( $gBitSystem->isFeatureActive( 'liberty_thumbnail_format' )) {
$mimeExt = $gBitSystem->getConfig( 'liberty_thumbnail_format' );
} else {
list( $type, $mimeExt ) = explode( '/', strtolower( image_type_to_mime_type( $itype )));
}
if( $mimeExt = preg_replace( "!^(x-)?(jpeg|png|gif)$!", "$2", $mimeExt )) {
$targetType = $mimeExt;
$destExt = '.'.$mimeExt;
}
if( !$mimeExt || $mimeExt == 'jpeg' ) {
$targetType = 'jpeg';
$destExt = '.jpg';
}
$destFile = !empty( $pFileHash['dest_file'] )
? $pFileHash['dest_file']
: STORAGE_PKG_PATH.$pFileHash['dest_branch'].$pFileHash['dest_base_name'].$destExt;
switch( $targetType ) {
case 'png':
if( imagetypes() & IMG_PNG ) {
// png alpha stuff - needs more testing - spider
// imagecolorallocatealpha ( $t, 0, 0, 0, 127 );
// $ImgWhite = imagecolorallocate($t, 255, 255, 255);
// imagefill($t, 0, 0, $ImgWhite);
// imagecolortransparent($t, $ImgWhite);
imagepng( $t, $destFile );
break;
}
case 'gif':
// This must go immediately before default so default will be hit for PHP's without gif support
if( imagetypes() & IMG_GIF ) {
imagecolortransparent( $t );
imagegif( $t, $destFile );
break;
}
default:
imagejpeg( $t, $destFile );
break;
}
// set permissions if possible - necessary for some wonky shared hosting environments
if( chmod( $pFileHash['source_file'], 0644 )){
// does nothing, but fails elegantly
}
$pFileHash['name'] = $pFileHash['dest_base_name'].$destExt;
$pFileHash['size'] = filesize( $destFile );
$ret = $destFile;
} elseif( $iwidth && $iheight ) {
$ret = liberty_process_generic( $pFileHash, false );
}
return $ret;
}
/**
* liberty_gd_rotate_image
*
* @param array $pFileHash
* @param array $pFormat
* @access public
* @return bool true on success, false on failure - mErrors will contain reason for failure
*/
function liberty_gd_rotate_image( &$pFileHash, $pFormat = null ) {
if( !function_exists( 'imagerotate' ) ) {
$pFileHash['error'] = "Rotate is not available on this webserver.";
} elseif( empty( $pFileHash['degrees'] ) || !is_numeric( $pFileHash['degrees'] ) ) {
$pFileHash['error'] = KernelTools::tra( 'Invalid rotation amount' );
} else {
// we need to scale and/or reformat
$fp = fopen( $pFileHash['source_file'], "rb" );
$data = fread( $fp, filesize( $pFileHash['source_file'] ) );
fclose ($fp);
if( function_exists("ImageCreateFromString") ) {
$img = @imagecreatefromstring($data);
}
if( !empty( $img ) ) {
// image rotate degrees seems back ass words.
$rotateImg = imagerotate ( $img, -1 * $pFileHash['degrees'], 0 );
if( !empty( $rotateImg ) ) {
imagejpeg( $rotateImg, $pFileHash['source_file'] );
} else {
$pFileHash['error'] = "Image rotation failed.";
}
} else {
$pFileHash['error'] = "Image could not be opened for rotation.";
}
}
return empty( $pFileHash['error'] );
}
/**
* liberty_gd_can_thumbnail_image
*
* @param string $pMimeType
* @access public
* @return string|bool true on success, false on failure
*/
function liberty_gd_can_thumbnail_image( $pMimeType ) {
$ret = false;
if( !empty( $pMimeType )) {
$ret = preg_match( '/^image/i', $pMimeType );
}
return $ret;
}
/**
* get_gd_version
*
* @access public
* @return string
*/
function get_gd_version( $pFullVersion = false ) {
if( empty( $_SESSION['gd_version'] )) {
$gd = gd_info();
$_SESSION['gd_version'] = preg_replace( "!\D*([\d|\.]*)!", "$1", $gd['GD Version'] );
}
if( $pFullVersion ) {
return $_SESSION['gd_version'];
}
return preg_replace( "!^(\d)+.*$!", "$1", $_SESSION['gd_version'] );
}
// nicked from http://at2.php.net/manual/en/function.gd-info.php
if( !function_exists( 'gd_info' )) {
function gd_info() {
$array = [
"GD Version" => "",
"FreeType Support" => 0,
"FreeType Linkage" => "",
"T1Lib Support" => 0,
"GIF Read Support" => 0,
"GIF Create Support" => 0,
"JPG Support" => 0,
"PNG Support" => 0,
"WBMP Support" => 0,
"XBM Support" => 0,
];
$gif_support = 0;
ob_start();
eval("phpinfo();");
$info = ob_get_contents();
ob_end_clean();
foreach(explode("\n", $info) as $line) {
if(strpos($line, "GD Version")!==false)
$array["GD Version"] = trim(str_replace("GD Version", "", strip_tags($line)));
if(strpos($line, "FreeType Support")!==false)
$array["FreeType Support"] = trim(str_replace("FreeType Support", "", strip_tags($line)));
if(strpos($line, "FreeType Linkage")!==false)
$array["FreeType Linkage"] = trim(str_replace("FreeType Linkage", "", strip_tags($line)));
if(strpos($line, "T1Lib Support")!==false)
$array["T1Lib Support"] = trim(str_replace("T1Lib Support", "", strip_tags($line)));
if(strpos($line, "GIF Read Support")!==false)
$array["GIF Read Support"] = trim(str_replace("GIF Read Support", "", strip_tags($line)));
if(strpos($line, "GIF Create Support")!==false)
$array["GIF Create Support"] = trim(str_replace("GIF Create Support", "", strip_tags($line)));
if(strpos($line, "GIF Support")!==false)
$gif_support = trim(str_replace("GIF Support", "", strip_tags($line)));
if(strpos($line, "JPG Support")!==false)
$array["JPG Support"] = trim(str_replace("JPG Support", "", strip_tags($line)));
if(strpos($line, "PNG Support")!==false)
$array["PNG Support"] = trim(str_replace("PNG Support", "", strip_tags($line)));
if(strpos($line, "WBMP Support")!==false)
$array["WBMP Support"] = trim(str_replace("WBMP Support", "", strip_tags($line)));
if(strpos($line, "XBM Support")!==false)
$array["XBM Support"] = trim(str_replace("XBM Support", "", strip_tags($line)));
}
if($gif_support==="enabled") {
$array["GIF Read Support"] = 1;
$array["GIF Create Support"] = 1;
}
if($array["FreeType Support"]==="enabled")
$array["FreeType Support"] = 1;
if($array["T1Lib Support"]==="enabled")
$array["T1Lib Support"] = 1;
if($array["GIF Read Support"]==="enabled")
$array["GIF Read Support"] = 1;
if($array["GIF Create Support"]==="enabled")
$array["GIF Create Support"] = 1;
if($array["JPG Support"]==="enabled")
$array["JPG Support"] = 1;
if($array["PNG Support"]==="enabled")
$array["PNG Support"] = 1;
if($array["WBMP Support"]==="enabled")
$array["WBMP Support"] = 1;
if($array["XBM Support"]==="enabled")
$array["XBM Support"] = 1;
return $array;
}
}
|