Welcome Guest, Not a member yet? Register   Sign In
Easy function, library, helper for "echoing" thumbnail
#1

[eluser]CARP[/eluser]
Hi guys
I've been reading the thumbnail creation process in the user_guide
I've found that it is not possible to show only the thumbnail.

I've found this lib, but it is too much
http://codeigniter.com/wiki/Category:Ima...and_Cache/

Does any1 have a CI way to create a thumbnail from an image, but only for showing in the browser? (not storing or creating thumbnail files)

Thanks a lot in advance,
#2

[eluser]Unknown[/eluser]
you can use <img src="original_file" width="120" height="120"> but this is a bad practice. you can`t create thumbnails withous resizing the original file
#3

[eluser]Colin Williams[/eluser]
Why wouldn't you want to cache it (save it to the filesystem)?

Also, enough of "a CI way" requests already. The only CI ways are how URLs are routed, and the Loader class (okay, and maybe the Database class). Everything else is just pure PHP loaded by the Loader class.

Anyway... I would recommend caching the thumbnails, and maybe have a purge process to get rid of discarded ones. The Resize and Cache library looks good.
#4

[eluser]hvalente13[/eluser]
http://ellislab.com/forums/viewreply/430693/
#5

[eluser]CARP[/eluser]
@Colin
I don't want to write thumbnails because user will be changing the images very frequently. Also, I'm storing each image's info in a table, for when user changes products, I can delete image by image (without taking care of thumbnail files)

I'll try going with thumbnail writing to disk, and will see what happens when the app goes into production
#6

[eluser]JoostV[/eluser]
This is how I create, save and delete multiple sizes of the same image:

1. create an array that holds all image sizes in the __construct of your controller, e.g:
Code:
$this->img_instances[] = array('width' => 400, 'height' => 300, 'prefix' => '');
$this->img_instances[] = array('width' => 160, 'height' => 120, 'prefix' => 'tn_');
$this->img_instances[] = array('width' => 160, 'height' => 120, 'prefix' => 'tn_2_');

2. loop through that array, resizing and saving each image:
Code:
// Set filename without prefix
$filename = '198723.jpg';

// Resize, name and save all image instances
foreach ($this->img_instances as $instance) {
    $new_filename = $instance['prefix'] . $filename;
    // Resize and save $new_filename using $instance['width'] and $instance['height']
}

3. store the image name to the database, without any prefixes (e.g. 198723.jpg)
Code:
$this->db->set('img', $filename);
// Etc.

Then, when it's time to delete the images you simply:

1. retrieve the image name from the database and store it in a variable, say $img

2. loop through array $this->img_instances again and delete all instances
Code:
filepath = 'gfx/pages/'; // path to your image folder

// Set default error message
$error = '';

// loop through every image instance
foreach ($this->img_instances as $instance) {
    
    // Set the filename of the image to be deleted, using prefix from array
    $filename = $instance['prefix'] . $img;

    if (file_exists($filepath . $filename) && !is_dir($filepath . $filename)) {
        // The image exists, so delete it
        unlink($filepath . $filename);
    }
    else {
        // No such image, set error message
        $error .= 'Could not find ' . $filepath . $filename . ': file was not deleted';
    }
}

You can add other stuff to the array for more functionalities. E.g., I include 'square' => true to determine whether the thumbnails has to be cropped to a square image.




Theme © iAndrew 2016 - Forum software by © MyBB