Welcome Guest, Not a member yet? Register   Sign In
On-the-fly thumbnail generator
#1

[eluser]Unknown[/eluser]
Hi everyone,

This is my first post and first contribution to the CodeIgniter community.

I am working with CodeIgniter and Smarty (With Dwayne Charrington's library).

Recently I used a Smarty Library that creates thumbnails for images "On the fly", and I found the idea really good, but not well integrated with CodeIgniter.

I looked for CodeIgniter solutions, but didn't find my Graal.

So here comes the solution I created (attachment), composed of 3 parts:

1. A Resize class inside library.
This is a class I found on the Internet and modified a bit to fit my needs, I didn't used CodeIgniter library for various reasons:
1.1 There is no "Resize then Crop" feature.
1.2 Implementing such a feature would mean writing the file 2 times to disk.
1.3 I wanted to add Gray-scale conversions feature.
2. A thumbnail configuration file.
3. A thumbnail helper, which it the file you will load and use.
3b. A thumbnail Smarty function, that you can use if you are working with Dwayne Charrington's library


Here is an example in how to use the helper (I assume it is already loaded):
Code:
<img src="&lt;?=thumbnail('images/background.jpg', 400, 300)?&gt;" alt="Background Thumbnail" />
Or with Smarty :
Code:
<img src="{thumbnail src='images/background.jpg' width=400 height=300}" alt="Background Thumbnail" />

Each one can take 2 optional arguments:
option, can be :
crop: The image will be resize then cropped to fill the exact requested size.
exact: The image will be resize to the exact requested size.
landscape: The image will be resize to fit the width, and height will be calculated to keep the same ratio.
portrait: The image will be resize to fit the height, and width will be calculated to keep the same ratio.
auto: The image will be resize, keeping the ration, and never being larger then the requested size.
grayscale, if set to true, thumbnail will be converted to gray-scale.


And here is the configuration file, as you can see, it is pretty flexible on where to save resized pictures :
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* -------------------------------------------------------------------------
* Smarty Thumbnail config
* -------------------------------------------------------------------------
* This file allows you to configure folders for temp files used with thumbnail
*/
/* Base folder used to find image files, will be prepended to the image src */
$config['thumbnail_base_folder'] = SYSDIR."/../";

/* How to save cached files
*  same_path: Save in the same folder, with dimention added at the end of the file name.
*  subfolder: Save in a sub folder of the original file's folder
*  folder: Save in the cache folder, each file must have an unique name, or you'll have.
*  folder_: Save in the cache folder, with name that is an md5 of the image path
*/
$config['thumbnail_cache_type'] = 'subfolder';

# CONFIG used for 'same_path' and 'subfolder' mode #
/* The url path corresponding to the folder, can be any valid http directory */
$config['thumbnail_base_url'] = site_url();

# CONFIG used for 'subfolder' mode #
/* The url path corresponding to the folder, can be any valid http directory */
$config['thumbnail_subfolder_name'] = 'thumbnails';

# CONFIG used for 'folder' and 'folder_' #
/* The cache folder where resize images will be saved, must be accessible from the web */
$config['thumbnail_cache_folder'] = APPPATH."../thumbnails/";

/* The url path corresponding to the cache folder */
$config['thumbnail_cache_url'] = site_url()."thumbnails/";

/* Set to true to force recaching each time */
$config['thumbnail_disable_caching'] = false;
?&gt;
Features:
1. Supports JPG, GIF and PNGs.
2. Resized images are generated only one time
3. If the original file is modified, is automatically refreshed.
4. Transparency is not lost.

Installation:
1. Copy all files from the zip to you "application" folder, merge folders.
2. Modify thumnbail.php inside config folder to fit you needs.
3. Add the helper function anywhere you need a thumbnail.

Hope you will find this useful, any request, remark or improvements are welcome !
#2

[eluser]cmarfil[/eluser]
Hello,

The link is broke, can you upload again?

Thanks Smile
#3

[eluser]Unknown[/eluser]
same here, please reupload !

Cheers




Theme © iAndrew 2016 - Forum software by © MyBB