Welcome Guest, Not a member yet? Register   Sign In
How to generate on the fly images with image_lib?
#1

[eluser]Sinclair[/eluser]
Hi,

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.
#2

[eluser]JoostV[/eluser]
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.
Code:
// 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.
#3

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

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

Now your image src path can look normal.
Code:
<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:
Code:
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");

    imagejpeg($im);
    imagedestroy($im);
}

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

[eluser]Sinclair[/eluser]
Hi,

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:


Code:
function get_photo($folder, $file){
        
        $path = 'c:/xampp/htdocs/acomp_www/' . $folder . '/' . $file;
        #$path = 'c:/xampp/htdocs/acomp_www/1/2.jpg';
        #$this->load->library('image_lib');                    
           #$this->image_lib->clear();
        $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';
        
        $this->image_lib->initialize($imageinit);
        $this->image_lib->watermark();
        #$this->image_lib->clear();
        $this->image_lib->resize();
        if(!$this->image_lib->resize() && !$this->image_lib->watermark()){
        echo $this->image_lib->display_errors();
        }
    }

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




Code:
function get_photo($folder, $file){
        
        $path = 'c:/xampp/htdocs/acomp_www/' . $folder . '/' . $file;
        $this->load->library('image_lib');
        
        $config['thumbnail'] = array('source_image' => $path,
                                     'width' => '200',
                                     'height' => '150',
                                     'dynamic_output' => true);
        $this->image_lib->initialize($config['thumbnail']);
        $this->image_lib->resize();
        
        $this->image_lib->clear();
        
        $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');
        $this->image_lib->initialize($config['watermark']);
        $this->image_lib->watermark();    
        
    #    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,




Theme © iAndrew 2016 - Forum software by © MyBB