• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I make multiple thumbails for the same image with image_lib?

#2
[eluser]Bramme[/eluser]
Code:
function process_uploaded_image($filename){
    
    $large_path = $this->config->item('portfolio_path').$filename;
    $thumb_path = $this->config->item('thumbs_path').$filename;
    
    //get image size
    $img_size = getimagesize($large_path);
    $orig_width = $img_size[0];
    $orig_height = $img_size[1];
    
    if($orig_width > $orig_height) {
    //horizontal picture
        $res_width = 450;
        $res_height = 10;
        $set_master_dim = 'width';
    } else {
    //vertical picture
        $res_width = 10;
        $res_height = 400;
        $set_master_dim = 'height';
    }
    
    //resizing the uploaded image
    $resize_config['image_library'] = 'gd2';
    $resize_config['source_image'] = $large_path;
    $resize_config['maintain_ratio'] = TRUE;
    $resize_config['width'] = $res_width;
    $resize_config['height'] = $res_height;
    $resize_config['master_dim'] = $set_master_dim;
    
    $this->load->library('image_lib', $resize_config);
    
    if (!$this->image_lib->resize()) {
        echo "fout bij eerste resize";
        echo $this->image_lib->display_errors();
        return false;
    }
    
    //making a thumbnail
    $this->image_lib->clear();
    
    //get image size
    $img_size = getimagesize($large_path);
    $orig_width = $img_size[0];
    $orig_height = $img_size[1];
    
    //calculate offset
    if($orig_width > $orig_height) {
    //horizontal picture
        $diff = $orig_width - $orig_height;
        $cropsize = $orig_height - 1;
        $x_axis = round($diff / 2);
        $y_axis = 0;
    } else {
    //vertical picture
        $cropsize = $orig_width - 1;
        $diff = $orig_height - $orig_width;
        $x_axis = 0;
        $y_axis = round($diff / 2);
    }
    
    //actual cropping
    $crop_config['image_library'] = 'gd2';
    $crop_config['source_image'] = $large_path;
    $crop_config['maintain_ratio'] = FALSE;
    $crop_config['new_image'] = $thumb_path;
    $crop_config['x_axis'] = $x_axis;
    $crop_config['y_axis'] = $y_axis;
    $crop_config['width'] = $cropsize;
    $crop_config['height'] = $cropsize;
        
    $this->image_lib->initialize($crop_config);

    if (!$this->image_lib->crop()) {
        echo "fout bij crop";
        echo $this->image_lib->display_errors();
        return false;
    }
    
    
    //resize the cropped image to a thumbnail
    $this->image_lib->clear();
    
    $thumb_config['image_library'] = 'gd2';
    $thumb_config['source_image'] = $thumb_path;
    $thumb_config['maintain_ratio'] = TRUE;
    $thumb_config['width'] = 100;
    $thumb_config['height'] = 100;
    
    $this->image_lib->initialize($thumb_config);
    
    if (!$this->image_lib->resize()) {
        echo "fout bij resizen crop";
        echo $this->image_lib->display_errors();
        return false;
    }
    
    //watermark the resized image
    $this->image_lib->clear();
    
    $wm_config['source_image'] = $large_path;
    $wm_config['wm_type'] = 'overlay';
    $wm_config['wm_vrt_alignment'] = 'middle';
    $wm_config['wm_hor_alignment'] = 'center';
    $wm_config['wm_overlay_path'] = './images/shutter_watermark.png';
    
    $this->image_lib->initialize($wm_config);
    
    if (!$this->image_lib->watermark()) {
        echo "fout bij watermarken";
        echo $this->image_lib->display_errors();
        return false;
    }
    
    return true;    
    
}
Here's a method from a model of mine. It does several things after each other... There's resizing, cropping, more resizing and more cropping. If you have a look at it, you should understand the process.

How do you use it? Just put $this->my_model->process_uploaded_image('image.jpg'); in your controller. (ignore the dutch error messages)


Messages In This Thread
How do I make multiple thumbails for the same image with image_lib? - by El Forum - 07-20-2008, 06:50 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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