├── README.md └── thumb_helper.php /README.md: -------------------------------------------------------------------------------- 1 | Codeigniter-Thumbnail-Generator 2 | ============================== 3 | 4 | A Codeigniter helper to generate 'On-The-Fly' image thumbnails. 5 | 6 | Installation & Usage 7 | -------------------- 8 | 9 | 1. Download the thumb_helper.php file to your /application/helpers directory 10 | 2. In your /config/autoload.php file, add 'thumb_helper' to your helpers array 11 | 3. The function expects the original file path (the filesystem one), a width and height for the thumbnail. 12 | 13 | ```php 14 | 15 | ``` 16 | 17 | The fourth parameter is optional and contains the full path to save the thumbnail. Eg: 18 | 19 | 20 | ```php 21 | 26 | ``` 27 | 28 | Where 200 is the thumbnail width and 100 is the height. 29 | 30 | If the fourth param is not provided, a default filename is generated based on the original filename and it's dimensions. 31 | Eg: ````my_image.png```` will become ````my_image_200_100.png````, and will be stored in the same directory as the original source image. 32 | 33 | 4. To output a thumbnail, include the function as part of the source of your image. Eg: 34 | 35 | 36 | ```php 37 | 38 | ``` 39 | 40 | Where 200 is the thumbnail's width and 100 is the height. thumb() will return the thumb's filename. 41 | 42 | 43 | Philosophy 44 | ---------- 45 | 46 | Thumbs should be generated as needed by your current site layout and without modifing the original image. If the layout changes there won't be 47 | the need to upload every image again, which is very conveniently. 48 | 49 | **Where should I place those thumbs?** Well, a thumb should be localizable by two ways: it's origin and it's applied manipulation (resize, crop, border-radius, polaroid-effect, etc) 50 | 51 | By it's origin, becouse if the original image gets removed, it's thumbs should be removed as well. 52 | By it's applied manipulation, becouse, as different types of thumbs may exist for the same image, if design changes and older thumbs ain't needed anymore they are all easyly removable. 53 | 54 | > A hierarchical folder organization is cheap, fast and for your thumbs should be enough, 55 | > no need for an extra database table 56 | 57 | **But I need feature X implemented** Great! Instead forcing this helper to fit in every possibly use case, I tried to make it easyly extensible/adaptable for your custom needs. Take this as a boilerplate codebase and please share your adaptation so is useful for everybody :) 58 | 59 | 60 | Credits 61 | ------- 62 | 63 | This script is an adaptation of one by JR Tashjian. The original blog post can be found here: 64 | http://jrtashjian.com/2009/02/image-thumbnail-creation-caching-with-codeigniter/ 65 | 66 | -------------------------------------------------------------------------------- /thumb_helper.php: -------------------------------------------------------------------------------- 1 | 9 | * @access public 10 | * @param string $src 11 | * @param int $width 12 | * @param int $height 13 | * @param string $image_thumb 14 | * @return String 15 | * 16 | */ 17 | 18 | function thumb($src, $width, $height, $image_thumb = '') { 19 | 20 | // Get the CodeIgniter super object 21 | $CI = &get_instance(); 22 | 23 | // get src file's dirname, filename and extension 24 | $path = pathinfo($src); 25 | 26 | // Path to image thumbnail 27 | if( !$image_thumb ) 28 | $image_thumb = $path['dirname'] . DIRECTORY_SEPARATOR . $path['filename'] . "_" . $height . '_' . $width . "." . $path['extension']; 29 | 30 | if ( !file_exists($image_thumb) ) { 31 | 32 | // LOAD LIBRARY 33 | $CI->load->library('image_lib'); 34 | 35 | // CONFIGURE IMAGE LIBRARY 36 | $config['source_image'] = $src; 37 | $config['new_image'] = $image_thumb; 38 | $config['width'] = $width; 39 | $config['height'] = $height; 40 | 41 | $CI->image_lib->initialize($config); 42 | $CI->image_lib->resize(); 43 | $CI->image_lib->clear(); 44 | 45 | // get our image attributes 46 | list($original_width, $original_height, $file_type, $attr) = getimagesize($image_thumb); 47 | 48 | // set our cropping limits. 49 | $crop_x = ($original_width / 2) - ($width / 2); 50 | $crop_y = ($original_height / 2) - ($height / 2); 51 | 52 | // initialize our configuration for cropping 53 | $config['source_image'] = $image_thumb; 54 | $config['new_image'] = $image_thumb; 55 | $config['x_axis'] = $crop_x; 56 | $config['y_axis'] = $crop_y; 57 | $config['maintain_ratio'] = FALSE; 58 | 59 | $CI->image_lib->initialize($config); 60 | $CI->image_lib->crop(); 61 | $CI->image_lib->clear(); 62 | 63 | } 64 | 65 | return basename($image_thumb); 66 | } 67 | 68 | /* End of file thumb_helper.php */ 69 | /* Location: ./application/helpers/thumb_helper.php */ 70 | --------------------------------------------------------------------------------