2 /*==============================================================
\r
4 ==============================================================
\r
5 Copyright (c) 2006 by Dylan Cheon & Kelvin Wong
\r
6 Institute for Assistive Technology / University of Victoria
\r
7 http://www.canassist.ca/
\r
9 This program is free software. You can redistribute it and/or
\r
10 modify it under the terms of the GNU General Public License
\r
11 as published by the Free Software Foundation.
\r
12 ==============================================================
\r
17 * @desc This file handles all the image upload operations
\r
18 * @author Dylan Cheon & Kelvin Wong
\r
19 * @copyright 2006, Institute for Assistive Technology / University of Victoria
\r
20 * @link http://www.canassist.ca/
\r
24 require_once(PATH.'classes/phpThumb_1.7.2/phpthumb.class.php');
\r
25 require_once(PATH.'classes/phpThumb_1.7.2/phpthumb.functions.php');
\r
26 require_once(PATH.'classes/pa.class.php');
\r
27 require_once(PATH.'define.php');
\r
30 * @desc image upload class to upload an image
\r
34 class IMAGE_UPLOAD extends PA{
\r
37 var $file_tmp_src='';
\r
39 var $thumb_image_name='';
\r
40 var $view_image_name='';
\r
41 var $image_copy_required=true;
\r
42 var $user_input_error=0;
\r
43 var $user_input_array=Array();
\r
44 var $temp_folder_path='';
\r
47 * @desc image upload constructor
\r
48 * @param Array $file_array array which contains the image file information (name, size, tmp_src, etc)
\r
49 * @param String $temp_folder_path temp folder path string to copy the image file in.
\r
51 function IMAGE_UPLOAD($file_array, $temp_folder_path){
\r
53 $this->setVariable('temp_folder_path', $temp_folder_path);
\r
54 $this->checkImageFile($file_array);
\r
55 if (($this->getVariable('user_input_error')==0) && ($this->getVariable('image_copy_required')==true)){ //no error, so process image copy operation
\r
56 $this->setVariable('name', $file_array['name']);
\r
57 $this->copyViewImage();
\r
58 $this->copyThumbImage();
\r
63 * @desc This function checks the given file array is properly set up
\r
64 * @param Array $file file array
\r
66 function checkImageFile(&$file){
\r
67 $this->checkImageEmpty($file['name']);
\r
68 $this->checkValidType($file['type']);
\r
69 $this->checkValidNameLength($file['name']);
\r
70 $this->checkValidEscape($file['name']);
\r
71 $this->checkValidSize($file['size']);
\r
72 $this->setVariable('file_tmp_src', $file['tmp_name']);
\r
77 * @desc This function checks whether the image is submitted empty or not
\r
78 * @param String $file image name
\r
80 function checkImageEmpty(&$file){
\r
82 $this->storeUserError("file_empty");
\r
87 * @desc This function stores the user input error
\r
88 * @param String $string error string
\r
90 function storeUserError($string){
\r
91 $error_count=$this->getVariable('user_input_error');
\r
92 $error_array=&$this->user_input_array;
\r
93 $error_array[$error_count]=&$string;
\r
94 $this->setVariable('user_input_error', $error_count+1);
\r
98 * @desc This function checks whether the image size is valid
\r
99 * @param int $file image size
\r
101 function checkValidSize($file){
\r
102 $max_size=get_max_file_size(PA::getVariable('course_id'));
\r
103 if ($max_size==-1){
\r
105 $max_size=$_config['max_file_size'];
\r
107 if ($file > $max_size){
\r
108 $this->storeUserError('file_size');
\r
110 $this->setVariable('file_size', $file);
\r
115 * @desc This function checks whether the image file name uses invalid escape characters or not
\r
116 * @param String $file image file name
\r
118 function checkValidEscape(&$file){
\r
119 if (ereg(INVALID_ESCAPE, $file)){
\r
120 $this->storeUserError('file_escape');
\r
125 * @desc This function checks whether the image file name length is valid
\r
126 * @param String $file image name
\r
128 function checkValidNameLength(&$file){
\r
129 if (count($file)>MAX_FILENAME_LENGTH){
\r
130 $this->storeUserError('file_length');
\r
135 * @desc This function checks whether the image type is valid
\r
136 * @param String $type image type string
\r
138 function checkValidType($type){
\r
139 global $IMAGE_TYPE;
\r
140 if (in_array($type, $IMAGE_TYPE)){
\r
141 $this->setVariable('file_type', $type);
\r
143 $this->storeUserError('file_type');
\r
148 * @desc This function sets the string value for the class object
\r
149 * @param String $string string name to be set
\r
150 * @param mixed $value string value
\r
152 function setVariable($string, $value){
\r
155 case 'image_copy_required':
\r
156 if (is_bool($value)){
\r
157 $this->{$string}=$value;
\r
159 parent::storeError("string ".$string." is not boolean");
\r
163 case 'file_tmp_src':
\r
165 case 'thumb_image_name':
\r
166 case 'view_image_name':
\r
167 case 'temp_folder_path':
\r
168 if (is_string($value)){
\r
169 $this->{$string}=$value;
\r
171 parent::storeError("string ".$string." is not string");
\r
174 case 'user_input_error':
\r
175 if (is_int($value)){
\r
176 $this->{$string}=$value;
\r
178 parent::storeError("string ".$string." is not int");
\r
185 * @desc This function creates a full-sized image of the image file and copies it to the temp folder
\r
187 function copyViewImage(){
\r
188 $temp_folder_path=$this->getVariable('temp_folder_path');
\r
189 $view_image_name=modify_image_name($temp_folder_path, $this->getVariable('name'));
\r
190 $this->setVariable('view_image_name', $view_image_name);
\r
191 image_to_this_location($view_image_name, $this->getVariable('file_tmp_src'), $temp_folder_path);
\r
192 $thumb=new phpThumb();
\r
193 $view_image_path=AT_CONTENT_DIR.$temp_folder_path.$view_image_name;
\r
194 $thumb->setSourceFilename($view_image_path);
\r
195 $size=getimagesize($view_image_path);
\r
196 /* set size for view image */
\r
197 if ($size[0]>MAX_IMAGE_WIDTH){
\r
198 $thumb->w=MAX_IMAGE_WIDTH;
\r
200 if (defined(MAX_IMAGE_HEIGHT) && $size[1]> MAX_IMAGE_HEIGHT){
\r
201 $thumb->h=MAX_IMAGE_HEIGHT;
\r
205 $thumb->config_output_format='jpeg';
\r
207 /* generate for view image */
\r
208 if ($thumb->GenerateThumbnail()){
\r
209 if ($view_image_path){
\r
210 if (!$thumb->RenderToFile($view_image_path)){
\r
211 parent::storeError("render operation failed for view image");
\r
214 parent::storeError("view image path does not exist");
\r
217 parent::storeError("View generateThumbnail operation is failed");
\r
222 * @desc This function creates a thumbnail image of the image file and stores it in the temp folder
\r
224 function copyThumbImage(){
\r
225 $temp_folder_path=$this->getVariable('temp_folder_path');
\r
226 $thumb_image_name=insert_into_image_name($this->getVariable('view_image_name'), THUMB_EXT);
\r
227 $thumb_image_name=modify_image_name($temp_folder_path, $thumb_image_name);
\r
228 $this->setVariable('thumb_image_name', $thumb_image_name);
\r
229 $thumb=new phpThumb();
\r
230 $view_image_path=AT_CONTENT_DIR.$temp_folder_path.$this->getVariable('view_image_name');
\r
231 $thumb->setSourceFilename($view_image_path);
\r
232 $size=getimagesize($view_image_path);
\r
233 /* set size for thumb image */
\r
235 $thumb->config_output_format='jpeg';
\r
237 /* set size for thumb image */
\r
238 $thumb->h=THUMB_IMAGE_HEIGHT;
\r
239 $thumb->w=THUMB_IMAGE_WIDTH;
\r
241 /* generate for thumb image */
\r
242 $thumb_image_path=AT_CONTENT_DIR.$temp_folder_path.$thumb_image_name;
\r
243 if ($thumb->GenerateThumbnail()){
\r
244 if ($view_image_path){
\r
245 if (!$thumb->RenderToFile($thumb_image_path)){
\r
246 parent::storeError("render operation failed for thumb image");
\r
249 parent::storeError("view image path does not exist");
\r
252 parent::storeError("Thumb generateThumbnail operation is failed");
\r