How to generate image on the fly with codeIgniter

It's only fair to share...Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on StumbleUponEmail this to someone

This post is talking about how to generate image on the fly with codeigniter. This is the basic code and sure you can improve it as you far as you need, All we need is only 1 controller and 1 view

1. Controller

Create controller and named it “preview”, we also need function in it to call the image class. See the script below

[sourcecode language=’php’]

load->helper(‘url’);
}

function index($height=’50’,$width=’50’)
{

// we will need to pass the height and with of the image
$arr_data[‘height’] = $height;
$arr_data[‘width’] = $width;
$this->load->view(‘preview’, $arr_data);
}

function get_photo($height,$width){
$path = [ absolute path to your image ex : /public-html/dock/image.jpg ];
$this->load->library(‘image_lib’);
$imageinit[‘image_library’] = ‘gd2’;
$imageinit[‘quality’] = ‘90%’; // set quality
$imageinit[‘dynamic_output’] = true; // set to true to generate it dynamically
$imageinit[‘source_image’] = $path;
$imageinit[‘maintain_ratio’] = false;
$imageinit[‘width’] = $width;
$imageinit[‘height’] = $height;
$this->image_lib->initialize($imageinit);
if(!$this->image_lib->resize()){
echo $this->image_lib->display_errors(); // print error if it fails
}
}
}

?>

[/sourcecode]

“get_photo” function is where we will generate the image dynamically. We call this function in <img> html tag in “view” file.

2. View

Here is the view code where we will call “get_photo” function

[sourcecode language=’php’]





Code Igniter : Image on the fly


View demo

  • Download source (only controller and view) : Image on the fly with codeigniter (1656)
  • It's only fair to share...Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on StumbleUponEmail this to someone
    • Patelmit69

      I will want to save my page div to jpg file and i will create div dynamic  

    • u got error at your demo!

    • Yoga

      ur demo was error dude ,

    • Yoga2

      Error

    • anonymous

      error

    • nikhilravindran

      error

    • FRANS

      eRROR