Welcome Guest, Not a member yet? Register   Sign In
Image manipulation thumbs (resize, and crop)
#1

[eluser]Piter[/eluser]
Hi,

I prepared a little script to generate thumbnails with a fixed size does not depend on the size of a large picture.

Directions:

Code:
$this->load->library('Images');

$this->images->resize('full_path on image', 'path of new image', '120','90'));

librares/Images.php

Code:
<?php
if (! defined('BASEPATH'))
    exit('No direct script access allowed');
    
/**
* Handle common image manipulation using the CI image_lib class.
*
* @category   Webcoding.CMS
* @package    Webcoding_Library
* @name Images.php
* @version 1.0
* @author Jarolewski Piotr
* @copyright Webcoding Jarolewski Piotr
* @created: 13.01.2011
*/

class Images
{
     private $CI;
    
     public function __construct()
     {
         $this->CI = & get_instance();
     }
    
     /**
     * Resize Images and Crop width and height
     *
     * @param $oldFile Full path and filename of original image
     * @param $newFile The full destination path and filename
     * @param $width The new width new image
     * @param $height The new height new image    
     * @return void
     */
    
     public function resize($oldFile, $newFile, $width, $height)
     {
         /*
         *    Resie image
         */              
         $config['image_library'] = 'gd2';
        $config['source_image'] = $oldFile;
        $config['new_image'] = $newFile;
        $config['maintain_ratio'] = TRUE;
        $config['master_dim'] = 'width';
        $config['width'] = $width + 2;
        $config['height'] = $height + 2;
        
        $this->CI->load->library('image_lib', $config);
        
        $this->CI->image_lib->resize();
        
        $size = $this->_get_size($newFile);

        unset($config); // clear $config
        
        /*
         *    Crop image  in weight, height
         */
        
        $config['image_library'] = 'gd2';
        $config['source_image'] = $newFile;
        $config['maintain_ratio'] = FALSE;
        $config['width'] = $width;
        $config['height'] = $height;
        $config['y_axis'] = round(($size['height'] - $height) / 2);
        $config['x_axis'] = 0;

        $this->CI->image_lib->clear();
        $this->CI->image_lib->initialize($config);
        if ( ! $this->CI->image_lib->crop())
        {
            echo $this->CI->image_lib->display_errors();
        }
     }
    
     private function _get_size($image)
     {
         $img = getimagesize($image);
         return Array('width'=>$img['0'], 'height'=>$img['1']);
    }
    
}

I greet in the Polish Smile
#2

[eluser]CARP[/eluser]
Hi
I've been looking for a library for creating thumbnails.
Is there any way to avoid the file creation and just output the thumbnail to the browser in each function call?

Thanks
#3

[eluser]Jazmo[/eluser]
This may be a very stupid question, but why did you code some nifty additional library and didn't extend the image_lib? You could've added resize_and_crop method to class directly and maybe even posted it to the repository. That's how it could eventually get even to the Core. And in that way, help us all.

Just my 2c, but of course I'm delighted of your contribution to the community.




Theme © iAndrew 2016 - Forum software by © MyBB