├── README.md ├── .gitattributes ├── .gitignore ├── config └── image_nation.php ├── controllers └── Imagination.php └── libraries └── Image_nation.php /README.md: -------------------------------------------------------------------------------- 1 | CodeIgniter-Image-nation 2 | ======================== 3 | 4 | Codeigniter image library for automatic crop and/or resize of images 5 | 6 | Please, read the tutorial at: http://avenir.ro/image_nation-codeigniter-library/ for an in-depth explanation of install and usage. 7 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # ========================= 18 | # Operating System Files 19 | # ========================= 20 | 21 | # OSX 22 | # ========================= 23 | 24 | .DS_Store 25 | .AppleDouble 26 | .LSOverride 27 | 28 | # Icon must end with two \r 29 | Icon 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | 38 | # Directories potentially created on remote AFP share 39 | .AppleDB 40 | .AppleDesktop 41 | Network Trash Folder 42 | Temporary Items 43 | .apdisk 44 | -------------------------------------------------------------------------------- /config/image_nation.php: -------------------------------------------------------------------------------- 1 | 'center','horizontal'=>'center'); // here we say what is our preference when the cropping is done. we want the cropping to be done from the center of the image (where the action is actually happening inside a photo) 11 | $config['overwrite_images'] = FALSE; // if we are sure that there is no possibility that the overwriting of existing files would affect our application, we can set this one to TRUE. but I think is much better to leave it to FALSE 12 | $config['default_quality'] = '70%'; // we can set a default quality for the processed image. Usually should be set to 70% -------------------------------------------------------------------------------- /controllers/Imagination.php: -------------------------------------------------------------------------------- 1 | load->library('image_nation'); 12 | 13 | 14 | 15 | // with source() method you can add images. The library will look for the image inside de default source folder that is set in the configuration file 16 | $this->image_nation->source('source-image-01.jpg'); 17 | $this->image_nation->source('source-image-02.jpg'); 18 | $this->image_nation->source('source-image-03.jpg'); 19 | 20 | // you can also add more than one image in one step by passing them in an array 21 | // $this->image_nation->source(array('source-image-01.jpg','source-image-02.jpg','source-image-03.jpg')); 22 | 23 | //you can add sources by mentioning full path, if you do this the second parameter must be set to TRUE 24 | //$this->image_nation->source(array(FCPATH.'upload/source-image-01.jpg', FCPATH.'upload/source-image-02.jpg'),TRUE); 25 | 26 | // you can reset the sizes. clear_sizes() method erases all default sizes set inside the config file 27 | //$this->image_nation->clear_sizes(); 28 | 29 | // you can add or modify a size by using add_size() method and passing it an array 30 | 31 | $dimensions = array( 32 | '200x200' => array( 33 | 'master_dim' => 'width', 34 | 'keep_aspect_ratio' => FALSE, 35 | 'style' => array('vertical'=>'center','horizontal'=>'center'), 36 | 'overwrite' => TRUE, 37 | 'quality' => '70%', 38 | 'directory' => 'test', 39 | 'file_name' => 'test_me' 40 | ) 41 | ); 42 | //$this->image_nation->add_size($dimensions); 43 | 44 | $images = $this->image_nation->process(); 45 | 46 | //if you've set alot of available dimensions in the configuration file, you can also process only specific dimension(s). ** ATTENTION: IT ONLY WORKS IF THE DIMENSIONS MENTIONED ARE PRESENT IN THE DEFAULT DIMENSIONS ** 47 | //$this->image_nation->process('400x350|500x200'); 48 | 49 | // you can also process only the dimensions you want by passing the array with the dimensions 50 | //$this->image_nation->process($dimension) 51 | 52 | // don't do this at home... use the views for output 53 | print_r($images); 54 | 55 | // get_errors() method is returning the eventual errors, or, if there were no errors, will return FALSE 56 | if(!$this->image_nation->get_errors()) { 57 | echo 'Images were created'; 58 | // get_processed() method returns the processed images' paths 59 | $processed_images = $this->image_nation->get_processed(); 60 | 61 | 62 | // don't do this at home... use the views to output 63 | echo '
'; 64 | print_r($processed_images); 65 | echo ''; 66 | } 67 | } 68 | 69 | public function verifyimglib() 70 | { 71 | $config = array( 72 | 'image_library' => 'gd2', 73 | 'create_thumb' =>FALSE, 74 | 'source_image' => 'D:/Dropbox/server/basicsetup/upload/chinatwo.jpg', 75 | 'quality' => '100%', 76 | 'new_image' => 'D:/Dropbox/server/basicsetup/upload/200x200/chinatwo.jpg', 77 | 'width' => '1098', 78 | 'height' => '1098', 79 | 'y_axis' => '280'); 80 | 81 | $this->load->library('image_lib'); 82 | $this->image_lib->initialize($config); 83 | $this->image_lib->crop(); 84 | } 85 | 86 | 87 | 88 | } 89 | 90 | /* End of file Imagination.php */ 91 | /* Location: ./application/controllers/Imagination.php */ -------------------------------------------------------------------------------- /libraries/Image_nation.php: -------------------------------------------------------------------------------- 1 | load->config('image_nation', TRUE); 25 | $this->_image_library = $this->config->item('image_library', 'image_nation'); 26 | $this->_source_directory = $this->config->item('source_directory', 'image_nation'); 27 | $this->_parent_directory = str_replace('\\','/',FCPATH.$this->config->item('parent_directory', 'image_nation')); 28 | $this->_size_folders = $this->config->item('size_folders', 'image_nation'); 29 | $this->_default_sizes = $this->config->item('default_sizes', 'image_nation'); 30 | $this->_keep_aspect_ratio = $this->config->item('keep_aspect_ratio', 'image_nation'); 31 | $this->_default_master_dim = $this->config->item('default_master_dim', 'image_nation'); 32 | $this->_default_style = $this->config->item('default_style', 'image_nation'); 33 | $this->_overwrite_images = $this->config->item('overwrite_images', 'image_nation'); 34 | $this->_default_quality = $this->config->item('default_quality', 'image_nation'); 35 | if(strlen($this->_parent_directory)==0) $this->_parent_directory = $this->_source_directory; 36 | $this->_sizes = $this->_set_defaults(); 37 | 38 | } 39 | 40 | /** 41 | * @return mixed sets the default sizes depending on th configuration; function required in the constructor 42 | */ 43 | private function _set_defaults() 44 | { 45 | if(strlen($this->_default_sizes)>0) 46 | { 47 | $sizes = explode('|', $this->_default_sizes); 48 | foreach($sizes as $size) 49 | { 50 | $image_path = $this->_parent_directory.'/'; 51 | if($this->_size_folders == true) { 52 | $image_path .= $size.'/'; 53 | } 54 | $width_height = explode('x',$size); 55 | $sizes_arr[$size] = array( 56 | 'width' => $width_height[0], 57 | 'height' => $width_height[1], 58 | 'master_dim' => $this->_default_master_dim, 59 | 'keep_aspect_ratio' => $this->_keep_aspect_ratio, 60 | 'style' => $this->_default_style, 61 | 'quality' => $this->_default_quality, 62 | 'directory' => $image_path, 63 | 'file_name' => FALSE, 64 | 'overwrite' => $this->_overwrite_images 65 | ); 66 | } 67 | return $sizes_arr; 68 | } 69 | } 70 | 71 | /** 72 | * source($str, $full_path = FALSE) 73 | * functions allows the insert of images inside the $this->_images, by receiving the source paths ($str). If the source paths are not relative to the $this->_source_directory, the second parameter should be set to TRUE 74 | * @param array|str $str 75 | * @param bool $full_path 76 | * @return array 77 | */ 78 | public function source($str, $full_path = FALSE) 79 | { 80 | if(is_array($str)) 81 | { 82 | foreach($str as $key => $value) 83 | { 84 | $str[$key] = $this->source($str[$key], $full_path); 85 | } 86 | return $str; 87 | } 88 | 89 | if($full_path) 90 | { 91 | $source_image = str_replace('\\','/',$str); 92 | $path = explode('/',$source_image); 93 | $image_name = $path[sizeof($path)-1]; 94 | } 95 | else 96 | { 97 | $source_image = str_replace('\\','/',FCPATH.$this->_source_directory).'/'.$str; 98 | $image_name = $str; 99 | } 100 | 101 | 102 | if(file_exists($source_image)) 103 | { 104 | $source_size = getimagesize($source_image); 105 | $this->_images[] = array('source_image' => $source_image, 'image_name' => $image_name, 'source_width'=>$source_size[0],'source_height'=>$source_size[1]); 106 | } 107 | else 108 | { 109 | $this->_errors[] = 'Image_nation: Couldn\'t find the image '.$source_image; 110 | } 111 | } 112 | 113 | /** 114 | * _set_sizes($parameters = NULL) 115 | * private function allowing the $this->_images to receive the dimensions 116 | * @param null $parameters 117 | */ 118 | private function _set_sizes($parameters = NULL) 119 | { 120 | foreach($this->_images as $key => $value) 121 | { 122 | if(is_null($parameters)) 123 | { 124 | $this->_images[$key]['sizes'] = $this->_sizes; 125 | } 126 | else 127 | { 128 | $dimensions = explode('|',$parameters); 129 | foreach($dimensions as $dimension) 130 | { 131 | $this->_images[$key]['sizes'][$dimension] = $this->_sizes[$dimension]; 132 | } 133 | } 134 | } 135 | } 136 | 137 | /** 138 | * clear_sizes() 139 | * public function that will clear all sizes 140 | * @return bool 141 | */ 142 | public function clear_sizes() 143 | { 144 | $this->_sizes = array(); 145 | return TRUE; 146 | } 147 | 148 | /** 149 | * add_size($parameters) 150 | * function allowing the adding of sizes and/or modifying the already added sizes 151 | * @param array $parameters 152 | * @return bool 153 | */ 154 | public function add_size($parameters) 155 | { 156 | if(is_array($parameters)) 157 | { 158 | $this->_sizes = array_merge($this->_sizes, $parameters); 159 | } 160 | foreach($parameters as $key => $value) 161 | { 162 | $width_height = explode('x',$key); 163 | $this->_sizes[$key]['width'] = $width_height[0]; 164 | $this->_sizes[$key]['height'] = $width_height[1]; 165 | } 166 | return TRUE; 167 | } 168 | 169 | /** 170 | * process($parameters = NULL) 171 | * function will allow the processing of the images. If $parameters are given, only the images mentioned in the parameters will be created 172 | * @param null $parameters 173 | */ 174 | public function process($parameters = NULL) 175 | { 176 | if(!is_null($parameters) && is_array($parameters)) 177 | { 178 | $this->clear_sizes(); 179 | $this->add_size($parameters); 180 | } 181 | elseif(!is_null($parameters) && !is_array($parameters)) 182 | { 183 | $this->_set_sizes($parameters); 184 | } 185 | else 186 | { 187 | $this->_set_sizes(); 188 | } 189 | 190 | return $this->_create_images(); 191 | } 192 | 193 | /** 194 | * _iterate_file_exists($path, $overwrite = FALSE) 195 | * private function that will look if a file exists and, if $overwrite is set to FALSE, will look for another possible name by iterating from 1 to $this->_max_filename_increment 196 | * @param $path 197 | * @param bool $overwrite 198 | * @return string 199 | */ 200 | private function _iterate_file_exists($path, $overwrite = FALSE) 201 | { 202 | if(file_exists($path) && ($overwrite===FALSE)) 203 | { 204 | for($i=1;$i<=$this->_max_filename_increment;$i++) 205 | { 206 | $ext = pathinfo($path, PATHINFO_EXTENSION); 207 | $new_file = rtrim($path,'.'.$ext); 208 | $new_file .= '-'.$i.'.'.$ext; 209 | if(!file_exists($new_file)) 210 | { 211 | break; 212 | } 213 | } 214 | return $new_file; 215 | } 216 | else 217 | { 218 | return $path; 219 | } 220 | } 221 | 222 | /** 223 | * get_errors() 224 | * public function that will return the errors in working with images if there are any, or FALSE if no error was encountered 225 | * @return array|bool 226 | */ 227 | public function get_errors() 228 | { 229 | if(!empty($this->_errors)) 230 | { 231 | return $this->_errors; 232 | } 233 | else 234 | { 235 | return FALSE; 236 | } 237 | } 238 | 239 | /** 240 | * get_processed() 241 | * public function that will return an array with the processed files ($this->_processed_images) 242 | * @return array 243 | */ 244 | public function get_processed() 245 | { 246 | return $this->_processed_images; 247 | } 248 | 249 | /** 250 | * _create_images() 251 | * private function that will iterate through the $this->_images and process the images according to the parameters given. returns $this->_processed_images 252 | * @return array 253 | */ 254 | private function _create_images() 255 | { 256 | foreach($this->_images as $key => $image) 257 | { 258 | $master_config['image_library'] = $this->_image_library; 259 | $master_config['create_thumb'] = FALSE; 260 | foreach($image['sizes'] as $image_size => $params) 261 | { 262 | $size_config = array(); 263 | $size_config['source_image'] = $image['source_image']; 264 | $size_config['quality'] = '100%'; 265 | if(!isset($params['directory'])) { 266 | $params['directory'] = $this->_parent_directory.'/'; 267 | if($this->_size_folders == true) { 268 | $params['directory'] .= $image_size.'/'; 269 | } 270 | } 271 | 272 | if(!file_exists($params['directory'])) 273 | { 274 | mkdir($params['directory']); 275 | } 276 | if($this->_size_folders===TRUE) 277 | { 278 | if(!file_exists($params['directory'])) 279 | { 280 | if(!mkdir($params['directory'])) 281 | { 282 | show_error('Couldn\'t create directory '.$image_size); 283 | } 284 | } 285 | } 286 | $ext = pathinfo($image['source_image'], PATHINFO_EXTENSION); 287 | if(($this->_size_folders===FALSE) && ($params['file_name']===FALSE)) 288 | { 289 | $file_name = rtrim($image['image_name'],'.'.$ext); 290 | $file_name .= '-'.$image_size.'.'.$ext; 291 | } 292 | elseif(isset($params['file_name']) && strlen($params['file_name'])>0) 293 | { 294 | $file_name = $params['file_name'].'.'.$ext; 295 | } 296 | else 297 | { 298 | $file_name = $image['image_name']; 299 | } 300 | $size_config['new_image'] = $params['directory'].'/'.$file_name; 301 | $size_config['new_image'] = $this->_iterate_file_exists($size_config['new_image'],$params['overwrite']); 302 | $source_ratio = $image['source_width'] / $image['source_height']; 303 | $new_ratio = $params['width'] / $params['height']; 304 | if($params['keep_aspect_ratio']===FALSE && ($source_ratio!=$new_ratio)) 305 | { 306 | if($new_ratio > $source_ratio || (($new_ratio == 1) && ($source_ratio < 1))) 307 | { 308 | $size_config['width'] = $image['source_width']; 309 | $size_config['height'] = round($image['source_width']/$new_ratio); 310 | switch($params['style']['vertical']) 311 | { 312 | case 'center' : 313 | $size_config['y_axis'] = round(($image['source_height'] - $size_config['height'])/2); 314 | break; 315 | case 'top' : 316 | $size_config['y_axis'] = 0; 317 | break; 318 | case 'bottom' : 319 | $size_config['y_axis'] = round(($image['source_height'] - $size_config['height'])); 320 | break; 321 | } 322 | $size_config['x_axis'] = 0; 323 | } 324 | else 325 | { 326 | $size_config['width'] = round($image['source_height'] * $new_ratio); 327 | $size_config['height'] = $image['source_height']; 328 | switch($params['style']['horizontal']) 329 | { 330 | case 'center' : 331 | case 'middle' : 332 | $size_config['x_axis'] = round(($image['source_width'] - $size_config['width'])/2); 333 | break; 334 | case 'left' : 335 | $size_config['x_axis'] = 0; 336 | break; 337 | case 'right' : 338 | $size_config['x_axis'] = round(($image['source_width'] - $size_config['width'])); 339 | break; 340 | } 341 | $size_config['y_axis'] = 0; 342 | } 343 | } 344 | 345 | $config = array_merge($master_config, $size_config); 346 | $this->load->library('image_lib'); 347 | if($params['keep_aspect_ratio']===FALSE && ($source_ratio!=$new_ratio)) 348 | { 349 | $config['maintain_ratio'] = FALSE; 350 | $this->image_lib->initialize($config); 351 | if(!$this->image_lib->crop()) 352 | { 353 | $this->_errors[] = 'One or more errors was encountered while cropping image '.$config['source_image']; 354 | $errors[] = $this->image_lib->display_errors(); 355 | } 356 | $this->image_lib->clear(); 357 | } 358 | 359 | $config['maintain_ratio'] = TRUE; 360 | $config['master_dim'] = isset($params['master_dim']) ? $params['master_dim'] : $this->_default_master_dim; 361 | $config['width'] = $params['width']; 362 | $config['height'] = $params['height']; 363 | $config['quality'] = isset($params['quality']) ? $params['quality'] : $this->_default_quality; 364 | $this->image_lib->initialize($config); 365 | if(!$this->image_lib->resize()) 366 | { 367 | $this->_errors[] = 'One or more errors was encountered while resizing image '.$config['source_image']; 368 | $errors[] = $this->image_lib->display_errors(); 369 | } 370 | $this->image_lib->clear(); 371 | 372 | if(!isset($errors)) $errors = array(); 373 | 374 | $file_name_arr = explode('/',$file_name); 375 | $file_name = $file_name_arr[sizeof($file_name_arr)-1]; 376 | $this->_processed_images[$key][$image_size] = array('file_name'=>$file_name,'path'=>$config['new_image'],'errors'=>$errors); 377 | } 378 | } 379 | return $this->_processed_images; 380 | } 381 | 382 | public function __get($var) 383 | { 384 | return $CI =& get_instance()->$var; 385 | } 386 | } 387 | --------------------------------------------------------------------------------