• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to generate on the fly images with image_lib?


I have been testing image_lib, but I can't generate on the fly images with them.

There are some examples on how to generate on the fly images with image_lib?

Best Regards.

Create a controller that outputs the image, image_lib setting 'dynamic_output' set to true, see http://ellislab.com/codeigniter/user-gui...e_lib.html.
Set this controller as your source.
// Assuming your controller name is 'image', method 'generate'
<img src="/image/generate/some_image" alt="foo" />

NB Creating images on-the-fly puts a massive strain on your server.

Another tip. You can easily use routes in CI to fake an image path like so (config/routes.php)

$route['d_images/some_image.jpg'] = "image/generate/some_image";

Now your image src path can look normal.
<img src="/d_images/some_image.jpg" alt="foo" />

Of course your generate method needs to dynamically produce the image. Here's a small snippet to demonstrate:
function generate()
    //try to force no caching of the image
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
    header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache"); // HTTP/1.0

    // set header to content type for jpeg.
    header("Content-type: image/jpeg");

    //set path to image.
    $imgname = '/path/to/images/background.jpg';
    $font = '/path/to/fonts/arial.ttf';

    $im = @imagecreatefromjpeg($imgname)
        or die("Cannot Initialize new GD image from file: $imgname");        

    $text_color = imagecolorallocate($im, 239, 255, 253);

    imagettftext($im, 16, 0, 100, 100, $text_color, $font, "text to superimpose");


There are many image functions in PHP. See the PHP Manual for details.


First of all, thanks for the replys.

I have found something that works, but I need to resize and watermark on the fly, whitout changing the original image file.

I have to functions that I'am testing:

function get_photo($folder, $file){
        $path = 'c:/xampp/htdocs/acomp_www/' . $folder . '/' . $file;
        #$path = 'c:/xampp/htdocs/acomp_www/1/2.jpg';
        $imageinit['image_library']     = 'GD2';
        $imageinit['quality']            = '100%';
        $imageinit['dynamic_output']    = TRUE;
        #$imageinit['create_thumb']    = FALSE;
        $imageinit['source_image']         = $path;
        #$imageinit['maintain_ratio']     = false;
        $imageinit['width']             = '200';
        $imageinit['height']             = '150';
        $imageinit['wm_type'] = 'overlay';
        $imageinit['wm_overlay_path'] = 'c:/xampp/htdocs/acomp_www/1/w.png';
        $imageinit['wm_padding'] = '0';
        $imageinit['wm_hor_alignment'] = 'bottom';
        $imageinit['wm_vrt_alignment'] = 'center';
        if(!$this->image_lib->resize() && !$this->image_lib->watermark()){
        echo $this->image_lib->display_errors();

This function, Watermarks On The Fly, but not resize.

function get_photo($folder, $file){
        $path = 'c:/xampp/htdocs/acomp_www/' . $folder . '/' . $file;
        $config['thumbnail'] = array('source_image' => $path,
                                     'width' => '200',
                                     'height' => '150',
                                     'dynamic_output' => true);
        $config['watermark'] = array('wm_type' => 'overlay',
                                     'source_image' => $path,
                                     'wm_overlay_path' => 'c:/xampp/htdocs/acomp_www/1/w.png',
                                     'wm_padding' => '20',
                                     'dynamic_output' => true,
                                     'create_thumb' => 'false',
                                     'wm_hor_alignment' => 'bottom',
                                     'wm_vrt_alignment' => 'center');
    #    if(!$this->image_lib->resize() && !$this->image_lib->watermark()){
        #echo $this->image_lib->display_errors();

This function resize and watermarks, but when watermarking, change the image, add the watermark to the image withthe original image size.

My question. How can I get real Resize and Watermarking On The Fly without changing the original image files? Can you give me some clues?

Best Regards,

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.