1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* FileUPloadHelper is the helper to the component FileUploadComponent. |
4
|
|
|
* This helper REQUIRES the FileUploadComponent. |
5
|
|
|
* |
6
|
|
|
* @author: Nick Baker |
7
|
|
|
* @version: 6.1.1 |
8
|
|
|
* @email: [email protected] |
9
|
|
|
* @link: http://www.webtechnick.com/blogs/view/221/CakePHP_File_Upload_Plugin |
10
|
|
|
* |
11
|
|
|
* @example |
12
|
|
|
* Show an already uploaded image |
13
|
|
|
* $fileUpload->image('filename.jpg', array('width' => 250)); //resizes a thumbnail of 'filename.jpg' to 250 |
14
|
|
|
* $fileUpload->image('filename.jpg', array('width' => 250, 'uploadDir' => 'custom/dir')); //resizes a thumbnail of 'webroot/custom/dir/filename.jpg' to 250 |
15
|
|
|
* |
16
|
|
|
* Show the upload field form |
17
|
|
|
* $fileUpload->input(); //builds the input form based on your FileUploadComponent defaults |
18
|
|
|
* -or- |
19
|
|
|
* $fileUpload->input(array('var' => 'fileVar', 'model' => 'Picture')); //customized input form. |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
App::import('Config', 'FileUpload.file_upload_settings'); |
23
|
|
|
class FileUploadHelper extends AppHelper{ |
24
|
|
|
var $helpers = array('Html', 'Form'); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* the name of the file passed in. |
28
|
|
|
*/ |
29
|
|
|
var $fileName = NULL; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Holds the FileUpload component |
33
|
|
|
*/ |
34
|
|
|
var $FileUpload = NULL; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Counts the number of inputs, for multiple fileUpload inputing. |
38
|
|
|
*/ |
39
|
|
|
var $inputCount = 0; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Default options for showImage |
43
|
|
|
* |
44
|
|
|
* - width: the width of the image to display (0 means no resizing (default)) |
45
|
|
|
* - resizedDir: is the directory in which to save the resized files into (resized by default) |
46
|
|
|
* - imagePathOnly: will only return the requested image_path (false by default) |
47
|
|
|
* - autoResize: will resize the file automatically if given a valid width. (true by default) |
48
|
|
|
* - resizeThumbOnly: will only resize the image down -- not up past the original's size (true by default) |
49
|
|
|
*/ |
50
|
|
|
var $options = array( |
51
|
|
|
'width' => 0, //0 means no resizing |
52
|
|
|
'resizedDir' => 'resized', // make sure webroot/files/resized is chmod 777 |
53
|
|
|
'imagePathOnly' => false, //if true, will only return the requested image_path |
54
|
|
|
'autoResize' => true, //if true, will resize the file automatically if given a valid width. |
55
|
|
|
'resizeThumbOnly' => true //if true, will only resize the image down -- not up past the original's size |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* FileUpload Settings set in config/file_upload_settings.php |
60
|
|
|
*/ |
61
|
|
|
var $settings = array(); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Constructor, initiallizes the FileUpload Component |
65
|
|
|
* and sets the default options. |
66
|
|
|
*/ |
67
|
|
|
function __construct(View $View, $settings = array()){ |
|
|
|
|
68
|
|
|
parent::__construct($View, $settings); |
69
|
|
|
$this->FileUploadSettings = new FileUploadSettings; |
|
|
|
|
70
|
|
|
|
71
|
|
|
//setup settings |
72
|
|
|
$this->settings = array_merge($this->FileUploadSettings->defaults, $this->options); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Reset the helper to its initial state |
77
|
|
|
* @access public |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
function reset(){ |
|
|
|
|
81
|
|
|
$this->fileName = null; |
82
|
|
|
$this->options = array( |
83
|
|
|
'width' => 0, |
84
|
|
|
'resizedDir' => 'resized', |
85
|
|
|
'imagePathOnly' => false, |
86
|
|
|
'autoResize' => true, |
87
|
|
|
'resizeThumbOnly' => true |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
//setup settings |
91
|
|
|
$this->settings = array_merge($this->FileUploadSettings->defaults, $this->options); |
|
|
|
|
92
|
|
|
unset($this->newImage); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* image takes a file_name or Upload.id and returns the HTML image |
97
|
|
|
* |
98
|
|
|
* @param String|Int $name takes a file_name or ID of uploaded file. |
99
|
|
|
* @param Array|Int $options takes an array of options passed to the image helper, or an integer representing the width of the image to display |
100
|
|
|
* options: width = 100 (default), if width is set along with autoResize the uploaded image will be resized. |
101
|
|
|
* @access public |
102
|
|
|
* @return mixed html tag, url string, or false if unable to find image. |
103
|
|
|
*/ |
104
|
|
|
function image($name, $options = array()){ |
|
|
|
|
105
|
|
|
$this->fileName = $name; |
106
|
|
|
//options takes in a width as well |
107
|
|
|
if(is_int($options)){ |
108
|
|
|
$width = $options; |
109
|
|
|
$options = array(); |
110
|
|
|
$options['width'] = $width; |
111
|
|
|
} |
112
|
|
|
$this->options = array_merge($this->options, $options); |
113
|
|
|
$this->settings = array_merge($this->settings, $options); |
114
|
|
|
|
115
|
|
|
$img = false; |
116
|
|
|
if(is_string($name)){ |
117
|
|
|
$img = $this->_getImageByName(); |
118
|
|
|
} |
119
|
|
|
elseif(is_int($name)){ |
120
|
|
|
$img = $this->_getImageById(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if($img){ |
|
|
|
|
124
|
|
|
return $img; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// $this->log("Unable to find $img"); |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* input takes an array of options and display the file browser html input |
133
|
|
|
* options. |
134
|
|
|
* @param Array $options of model and file options. Defaults to default FileUpload component configuration |
135
|
|
|
* @return String HTML form input element configured for the FileUploadComponent |
136
|
|
|
* @access public |
137
|
|
|
*/ |
138
|
|
|
function input($options = array()){ |
|
|
|
|
139
|
|
|
$options = array_merge( |
140
|
|
|
array('var' => $this->settings['fileVar'],'model' => $this->settings['fileModel']), |
141
|
|
|
$options |
142
|
|
|
); |
143
|
|
|
$configs = $options; |
144
|
|
|
if($configs['model']){ |
145
|
|
|
unset($options['model'], $options['var']); |
146
|
|
|
|
147
|
|
|
return $this->Form->input("{$configs['model']}.".$this->inputCount++.".{$configs['var']}", array_merge(array('type'=>'file'), $options)); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
else { |
150
|
|
|
return "<input type='file' name='data[{$configs['var']}][".$this->inputCount++."]' />"; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @access protected |
156
|
|
|
*/ |
157
|
|
|
function _getImageById(){ |
|
|
|
|
158
|
|
|
App::import('Component', 'FileUpload.FileUpload'); |
159
|
|
|
$this->FileUpload = new FileUploadComponent; |
160
|
|
|
|
161
|
|
|
$id = $this->fileName; |
162
|
|
|
$this->FileUpload->options['fileModel'] = $this->settings['fileModel']; |
163
|
|
|
$Model =& $this->FileUpload->getModel(); |
164
|
|
|
$Model->recursive = -1; |
165
|
|
|
$upload = $Model->findById($id); |
166
|
|
|
if(!empty($upload)){ |
167
|
|
|
$this->fileName = $upload[$this->settings['fileModel']][$this->settings['fields']['name']]; |
168
|
|
|
return $this->_getImageByName(); |
169
|
|
|
} |
170
|
|
|
else{ |
171
|
|
|
return false; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* _getFullPath returns the full path of the file name |
177
|
|
|
* @access protected |
178
|
|
|
* @return String full path of the file name |
179
|
|
|
*/ |
180
|
|
|
function _getFullPath(){ |
|
|
|
|
181
|
|
|
if($this->_isOutsideSource()){ |
182
|
|
|
return $this->fileName; |
183
|
|
|
} |
184
|
|
|
else { |
185
|
|
|
return WWW_ROOT . $this->_getUploadPath(); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* _getImagePath returns the image path of the file name |
191
|
|
|
* @access protected |
192
|
|
|
* @return String full path of the file name |
193
|
|
|
*/ |
194
|
|
|
function _getImagePath(){ |
|
|
|
|
195
|
|
|
if($this->_isOutsideSource()){ |
196
|
|
|
return $this->fileName; |
197
|
|
|
} |
198
|
|
|
else { |
199
|
|
|
return '/' . $this->_getUploadPath(); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* _getUploadPath returns the upload path of all files |
205
|
|
|
* @access protected |
206
|
|
|
* @return String upload path of all files |
207
|
|
|
*/ |
208
|
|
|
function _getUploadPath(){ |
|
|
|
|
209
|
|
|
return $this->settings['uploadDir'] . '/' . $this->fileName; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* _getExt returns the extension of the filename. |
214
|
|
|
* @access protected |
215
|
|
|
* @return String extension of filename |
216
|
|
|
*/ |
217
|
|
|
function _getExt(){ |
|
|
|
|
218
|
|
|
return strrchr($this->fileName,"."); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get the image by name and width. |
223
|
|
|
* if width is not specified return full image |
224
|
|
|
* if width is specified, see if width of image exists |
225
|
|
|
* if not, make it, save it, and return it. |
226
|
|
|
* @return String HTML of resized or full image. |
227
|
|
|
*/ |
228
|
|
|
function _getImageByName(){ |
|
|
|
|
229
|
|
|
//only proceed if we actually have the file in question |
230
|
|
|
if(!$this->_isOutsideSource() && !file_exists($this->_getFullPath())) return false; |
231
|
|
|
|
232
|
|
|
//resize if we have resize on, a width, and if it doesn't already exist. |
233
|
|
|
if($this->options['autoResize'] && $this->options['width'] > 0 && !file_exists($this->_getResizeNameOrPath($this->_getFullPath()))){ |
234
|
|
|
$this->_resizeImage(); |
235
|
|
|
} |
236
|
|
|
return $this->_htmlImage(); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @return String of the resizedpath of a filename or path. |
241
|
|
|
* @access protected |
242
|
|
|
*/ |
243
|
|
|
function _getResizeNameOrPath($file_name_or_path){ |
|
|
|
|
244
|
|
|
$file_name = basename($file_name_or_path); |
245
|
|
|
$path = substr($file_name_or_path, 0, strlen($file_name_or_path) - strlen($file_name)); |
246
|
|
|
$temp_path = substr($file_name,0,strlen($file_name) - strlen($this->_getExt())) . "x" . $this->options['width'] . $this->_getExt(); |
247
|
|
|
$full_path = (strlen($this->options['resizedDir']) > 0) ? $path . $this->options['resizedDir'] . '/' . $temp_path : $path . $temp_path; |
248
|
|
|
return $full_path; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* _resizeImage actually resizes the passed in image. |
253
|
|
|
* @access protected |
254
|
|
|
* @return null |
255
|
|
|
*/ |
256
|
|
|
function _resizeImage(){ |
|
|
|
|
257
|
|
|
$this->newImage = new RResizeImage($this->_getFullPath()); |
|
|
|
|
258
|
|
|
if($this->newImage->imgWidth > $this->options['width']){ |
|
|
|
|
259
|
|
|
$this->newImage->resize_limitwh($this->options['width'], 0, $this->_getResizeNameOrPath($this->_getFullPath())); |
|
|
|
|
260
|
|
|
} |
261
|
|
|
else { |
262
|
|
|
//$this->autoResize = false; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* _htmlImage returns the atual HTML of the resized/full image asked for |
268
|
|
|
* @access protected |
269
|
|
|
* @return String HTML image asked for |
270
|
|
|
*/ |
271
|
|
|
function _htmlImage(){ |
|
|
|
|
272
|
|
|
if(!$this->_isOutsideSource() && $this->options['autoResize'] && $this->options['width'] > 0){ |
273
|
|
|
if(isset($this->newImage) && $this->newImage->imgWidth && $this->newImage->imgWidth <= $this->options['width']){ |
|
|
|
|
274
|
|
|
$image = $this->_getImagePath(); |
275
|
|
|
} |
276
|
|
|
else { |
277
|
|
|
$image = $this->_getResizeNameOrPath($this->_getImagePath()); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
else { |
281
|
|
|
$image = $this->_getImagePath(); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
$options = $this->options; //copy |
285
|
|
|
//unset the default options |
286
|
|
|
unset($options['resizedDir'], $options['uploadDir'], $options['imagePathOnly'], $options['autoResize'], $options['resizeThumbOnly']); |
287
|
|
|
//unset width only if we're not an outsourced image, we have resize turned on, or we don't have a width to begin with. |
288
|
|
|
if(!$this->_isOutsideSource() && ($this->options['resizeThumbOnly'] || !$options['width'])) unset($options['width']); |
289
|
|
|
|
290
|
|
|
//return the impage path or image html |
291
|
|
|
if($this->options['imagePathOnly']){ |
292
|
|
|
return $image; |
293
|
|
|
} |
294
|
|
|
else { |
295
|
|
|
return $this->Html->image($image, $options); |
|
|
|
|
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* _isOutsideSource searches the fileName string for :// to determine if the image source is inside or outside our server |
301
|
|
|
*/ |
302
|
|
|
function _isOutsideSource(){ |
|
|
|
|
303
|
|
|
return !!strpos($this->fileName, '://'); |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
|
308
|
|
|
|
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Image Resizer. |
312
|
|
|
* @author : Harish Chauhan |
313
|
|
|
* @copyright : Freeware |
314
|
|
|
* About :This PHP script will resize the given image and can show on the fly or save as image file. |
315
|
|
|
* |
316
|
|
|
*/ |
317
|
|
|
//define("HAR_AUTO_NAME",1); |
318
|
|
|
Class RResizeImage { |
319
|
|
|
var $imgFile=""; |
320
|
|
|
var $imgWidth=0; |
321
|
|
|
var $imgHeight=0; |
322
|
|
|
var $imgType=""; |
323
|
|
|
var $imgAttr=""; |
324
|
|
|
var $type=NULL; |
325
|
|
|
var $_img=NULL; |
326
|
|
|
var $_error=""; |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Constructor |
330
|
|
|
* |
331
|
|
|
* @param [String $imgFile] Image File Name |
|
|
|
|
332
|
|
|
* @return RESIZEIMAGE (Class Object) |
|
|
|
|
333
|
|
|
*/ |
334
|
|
|
function __construct($imgFile=""){ |
|
|
|
|
335
|
|
|
if (!function_exists("imagecreate")){ |
336
|
|
|
$this->_error="Error: GD Library is not available."; |
337
|
|
|
return false; |
|
|
|
|
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
$this->type=Array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG', 4 => 'SWF', 5 => 'PSD', 6 => 'BMP', 7 => 'TIFF', 8 => 'TIFF', 9 => 'JPC', 10 => 'JP2', 11 => 'JPX', 12 => 'JB2', 13 => 'SWC', 14 => 'IFF', 15 => 'WBMP', 16 => 'XBM'); |
341
|
|
|
if(!empty($imgFile)){ |
342
|
|
|
$this->setImage($imgFile); |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Error occured while resizing the image. |
348
|
|
|
* |
349
|
|
|
* @return String |
350
|
|
|
*/ |
351
|
|
|
function error(){ |
|
|
|
|
352
|
|
|
return $this->_error; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Set image file name |
357
|
|
|
* |
358
|
|
|
* @param String $imgFile |
359
|
|
|
* @return void |
360
|
|
|
*/ |
361
|
|
|
function setImage($imgFile){ |
|
|
|
|
362
|
|
|
$this->imgFile=$imgFile; |
363
|
|
|
return $this->_createImage(); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @return void |
368
|
|
|
*/ |
369
|
|
|
function close(){ |
|
|
|
|
370
|
|
|
return @imagedestroy($this->_img); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Resize a image to given width and height and keep it's current width and height ratio |
375
|
|
|
* |
376
|
|
|
* @param Number $imgwidth |
377
|
|
|
* @param Numnber $imgheight |
378
|
|
|
* @param String $newfile |
379
|
|
|
*/ |
380
|
|
|
function resize_limitwh($imgwidth,$imgheight,$newfile=NULL){ |
|
|
|
|
381
|
|
|
$image_per = 100; |
382
|
|
|
list($width, $height, $type, $attr) = @getimagesize($this->imgFile); |
|
|
|
|
383
|
|
|
if($width > $imgwidth && $imgwidth > 0){ |
384
|
|
|
$image_per = (double)(($imgwidth * 100) / $width); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
if(floor(($height * $image_per)/100)>$imgheight && $imgheight > 0){ |
388
|
|
|
$image_per = (double)(($imgheight * 100) / $height); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
$this->resize_percentage($image_per,$newfile); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Resize an image to given percentage. |
396
|
|
|
* |
397
|
|
|
* @param Number $percent |
398
|
|
|
* @param String $newfile |
399
|
|
|
* @return Boolean |
400
|
|
|
*/ |
401
|
|
|
function resize_percentage($percent=100,$newfile=NULL) { |
|
|
|
|
402
|
|
|
$newWidth=($this->imgWidth*$percent)/100; |
403
|
|
|
$newHeight=($this->imgHeight*$percent)/100; |
404
|
|
|
return $this->resize($newWidth,$newHeight,$newfile); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Resize an image to given X and Y percentage. |
409
|
|
|
* |
410
|
|
|
* @param Number $xpercent |
411
|
|
|
* @param Number $ypercent |
412
|
|
|
* @param String $newfile |
413
|
|
|
* @return Boolean |
414
|
|
|
*/ |
415
|
|
|
function resize_xypercentage($xpercent=100,$ypercent=100,$newfile=NULL) { |
|
|
|
|
416
|
|
|
$newWidth=($this->imgWidth*$xpercent)/100; |
417
|
|
|
$newHeight=($this->imgHeight*$ypercent)/100; |
418
|
|
|
return $this->resize($newWidth,$newHeight,$newfile); |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Resize an image to given width and height |
423
|
|
|
* |
424
|
|
|
* @param Number $width |
425
|
|
|
* @param Number $height |
426
|
|
|
* @param String $newfile |
427
|
|
|
* @return Boolean |
428
|
|
|
*/ |
429
|
|
|
function resize($width,$height,$newfile=NULL){ |
|
|
|
|
430
|
|
|
if(empty($this->imgFile)){ |
431
|
|
|
$this->_error="File name is not initialised."; |
432
|
|
|
return false; |
433
|
|
|
} |
434
|
|
|
if($this->imgWidth<=0 || $this->imgHeight<=0){ |
435
|
|
|
$this->_error="Could not resize given image"; |
436
|
|
|
return false; |
437
|
|
|
} |
438
|
|
|
if($width<=0) $width=$this->imgWidth; |
439
|
|
|
if($height<=0) $height=$this->imgHeight; |
440
|
|
|
|
441
|
|
|
return $this->_resize($width,$height,$newfile); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Get the image attributes |
446
|
|
|
* @access Private |
447
|
|
|
* |
448
|
|
|
*/ |
449
|
|
|
function _getImageInfo() |
|
|
|
|
450
|
|
|
{ |
451
|
|
|
@list($this->imgWidth,$this->imgHeight,$type,$this->imgAttr)=@getimagesize($this->imgFile); |
|
|
|
|
452
|
|
|
$this->imgType=$this->type[$type]; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Create the image resource |
457
|
|
|
* @access Private |
458
|
|
|
* @return Boolean |
459
|
|
|
*/ |
460
|
|
|
function _createImage(){ |
|
|
|
|
461
|
|
|
$this->_getImageInfo(); |
462
|
|
|
if($this->imgType=='GIF'){ |
463
|
|
|
$this->_img=@imagecreatefromgif($this->imgFile); |
464
|
|
|
} |
465
|
|
|
elseif($this->imgType=='JPG'){ |
466
|
|
|
$this->_img=@imagecreatefromjpeg($this->imgFile); |
467
|
|
|
} |
468
|
|
|
elseif($this->imgType=='PNG'){ |
469
|
|
|
$this->_img=@imagecreatefrompng($this->imgFile); |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
if(!$this->_img || !@is_resource($this->_img)){ |
473
|
|
|
$this->_error="Error loading ".$this->imgFile; |
474
|
|
|
return false; |
475
|
|
|
} |
476
|
|
|
return true; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* Function is used to resize the image |
481
|
|
|
* |
482
|
|
|
* @access Private |
483
|
|
|
* @param Number $width |
484
|
|
|
* @param Number $height |
485
|
|
|
* @param String $newfile |
486
|
|
|
* @return Boolean |
487
|
|
|
*/ |
488
|
|
|
function _resize($width,$height,$newfile=NULL){ |
|
|
|
|
489
|
|
|
if (!function_exists("imagecreate")){ |
490
|
|
|
$this->_error="Error: GD Library is not available."; |
491
|
|
|
return false; |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
$newimg=@imagecreatetruecolor($width,$height); |
495
|
|
|
//imagecolortransparent( $newimg, imagecolorat( $newimg, 0, 0 ) ); |
496
|
|
|
|
497
|
|
|
if($this->imgType=='GIF' || $this->imgType=='PNG') { |
498
|
|
|
/** Code to keep transparency of image **/ |
499
|
|
|
$colorcount = imagecolorstotal($this->_img); |
500
|
|
|
if ($colorcount == 0) $colorcount = 256; |
501
|
|
|
imagetruecolortopalette($newimg,true,$colorcount); |
502
|
|
|
imagepalettecopy($newimg,$this->_img); |
503
|
|
|
$transparentcolor = imagecolortransparent($this->_img); |
504
|
|
|
imagefill($newimg,0,0,$transparentcolor); |
505
|
|
|
imagecolortransparent($newimg,$transparentcolor); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
@imagecopyresampled ( $newimg, $this->_img, 0,0,0,0, $width, $height, $this->imgWidth,$this->imgHeight); |
|
|
|
|
509
|
|
|
|
510
|
|
|
if($newfile===1) { |
511
|
|
|
if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches)){ |
512
|
|
|
$newfile=@substr_replace($this->imgFile,"_har",-@strlen($matches[0]),0); |
513
|
|
|
} |
514
|
|
|
} |
515
|
|
|
elseif(!empty($newfile)){ |
516
|
|
|
if(!@preg_match("/\..*+$/",@basename($newfile))){ |
517
|
|
|
if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches)){ |
518
|
|
|
$newfile=$newfile.$matches[0]; |
519
|
|
|
} |
520
|
|
|
} |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
if($this->imgType=='GIF'){ |
524
|
|
|
if(!empty($newfile)){ |
525
|
|
|
@imagegif($newimg,$newfile); |
|
|
|
|
526
|
|
|
} |
527
|
|
|
else { |
528
|
|
|
@header("Content-type: image/gif"); |
|
|
|
|
529
|
|
|
@imagegif($newimg); |
|
|
|
|
530
|
|
|
} |
531
|
|
|
} |
532
|
|
View Code Duplication |
elseif($this->imgType=='JPG'){ |
|
|
|
|
533
|
|
|
if(!empty($newfile)){ |
534
|
|
|
@imagejpeg($newimg,$newfile); |
|
|
|
|
535
|
|
|
} |
536
|
|
|
else { |
537
|
|
|
@header("Content-type: image/jpeg"); |
|
|
|
|
538
|
|
|
@imagejpeg($newimg); |
|
|
|
|
539
|
|
|
} |
540
|
|
|
} |
541
|
|
View Code Duplication |
elseif($this->imgType=='PNG'){ |
|
|
|
|
542
|
|
|
if(!empty($newfile)){ |
543
|
|
|
@imagepng($newimg,$newfile); |
|
|
|
|
544
|
|
|
} |
545
|
|
|
else{ |
546
|
|
|
@header("Content-type: image/png"); |
|
|
|
|
547
|
|
|
@imagepng($newimg); |
|
|
|
|
548
|
|
|
} |
549
|
|
|
} |
550
|
|
|
@imagedestroy($newimg); |
|
|
|
|
551
|
|
|
} |
552
|
|
|
} |
553
|
|
|
?> |
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.