Question

I'd like to use phpThumb ( http://phpthumb.sourceforge.net/ ) on my view layer to automatically size some images.

What's the recommended way to integrate phpThumb into the CodeIgniter architecture? Has anyone done this already and found that you prefer one method of integration over another?

I'm basically looking for opinions on using phpThumb as a helper/library/as-is code, etc.

Was it helpful?

Solution

You can do this with the codeigniter image manipulation class.

Just create a helper with your image resize code, here is a basic example:

In the Helper

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');


if ( ! function_exists('generate_image'))
{
    function generate_image($img, $width, $height){

        $obj =& get_instance();

        $obj->load->library('image_lib');
        $obj->load->helper('url');

        $config['image_library'] = 'gd2';

        $config['source_image'] = $img;
        $config['new_image'] = './resources/images/img_tp/tn_img.png' ;
        $config['width'] = $width;
        $config['height'] = $height;

        $obj->image_lib->initialize($config);

        $obj->image_lib->resize();


        return $config['new_image'];
    }
}

Name as you wish and ensure to load the helper in your controller constructer.

Then in your view:

<img src="<?php echo generate_image($img, $width, $height); ?>" />

You can create a set of functions in the helper to perform different manipulation tasks and use them directly in your view using this method.

Here is the image manipulation documentation. http://codeigniter.com/user_guide/libraries/image_lib.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top