Welcome Guest, Not a member yet? Register   Sign In
CI Image Manipulation
#1

[eluser]phusiondesign[/eluser]
I have been trying to utilize the image manipulation class which is included in code igniter. I keep running into a variety of problems and hope someone can help. The objective is to have a said image and resize / crop it on the fly. I want the image to be resized so that the width is 200px and height doesn't matter at that point... but then I want it chopped off so I only see the top 120px... then displayed. It would be best if a thumb was generated and used every time if it was available instead of running the whole resize / crop script.

Code I am using now... I know it is not all there, but I was trying to get something to work.

Code:
$config['image_library'] = 'GD';
$config['source_image'] = '/public/images/item_images/'.$item->item_data['image'].'';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 200;

$this->load->library('image_lib', $config);
if ( ! $this->image_lib->resize()) {
  echo $this->image_lib->display_errors();
}

Oh ya, how do I show what I created?

Thanks in advance.
#2

[eluser]Référencement Google[/eluser]
I don't specially answer your question, I only put below a function I am using in one application that may help you. It resize an upload picture, then crop it in the middle height at specified dimensions:

Code:
function resize_crop($files, $path = 'public/gallery')
    {
        $errors = FALSE;

        foreach($files as $photo)
        {
            $config['image_library']  = 'GD2';
            $config['maintain_ratio'] = TRUE;
            $config['quality']        = 95;

            // Resize the image
            $config['width']        = 550;
            $config['height']       = 550;
            $config['source_image'] = realpath($path).'/'.$photo['file_name'];
            $this->initialize($config);

            if( ! $this->resize())
            {
                $errors = TRUE;
            }

            // Create the thumbnail
            $config['width']        = 260;
            $config['height']       = 100;
            $config['create_thumb'] = TRUE;
            $config['thumb_marker'] = 'th_';
            $config['master_dim']   = 'width';
            $this->initialize($config);

            if( ! $this->resize())
            {
                $errors = TRUE;
            }

            // Crop the thumbnail
            $config['source_image']   = realpath($path).'/th_'.$photo['file_name'];
            $config['create_thumb']   = FALSE;
            $config['maintain_ratio'] = FALSE;
            $config['quality']        = 85;
            $config['x_axis']         = 0;

            $imageSize = $this->get_image_properties($config['source_image'], TRUE);

            // Crop in the middle of height
            $config['y_axis'] = (($imageSize['height'] / 2) - ($config['height'] / 2));

            $this->initialize($config);

            if( ! $this->crop())
            {
                $errors = TRUE;
            }

            // Clear settings for next loop
            $this->clear();
            unset($config);
         }

         return ! $errors;
    }
#3

[eluser]phusiondesign[/eluser]
Thanks for the post... I liked your method, and attempted to adapt it to my quest.

Code:
$config['image_library'] = 'GD2';
$config['maintain_ratio'] = TRUE;

// create a thumbnail
$config['source_image'] ='/public/images/item_images/'.$item->item_data['image'].'';
$config['width'] = 200
$config['height'] = 120;
$config['create_thumb'] = TRUE;
$config['thumb_marker'] = 'th_';
$config['master_dim']   = 'width';
                                    
$this->image_lib->initialize($config);
                                    
// Crop the thumbnail
$config['source_image'] = '/public/images/item_images/'.'th_'.$item->item_data['image'].'';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['x_axis'] = 0;
$config['y_axis'] = 120;
                                    
$this->image_lib->initialize($config);
                                    
if( ! $this->image_lib->crop())
{
  echo $this->image_lib->display_errors();
}

So now I am using this code, and it doesn't generate any errors... but, it doesn't generate a thumb either. I am more confused now.

Still seeking help. Thanks!
#4

[eluser]Pygon[/eluser]
Code:
$this->image_lib->initialize($config);

should be

Code:
$this->initialize($config);

as in the snippet provided by elitemedia
#5

[eluser]phusiondesign[/eluser]
[quote author="Pygon" date="1205283068"]
Code:
$this->initialize($config);
[/quote]

I tried that and received an error (Fatal error: Call to undefined method CI_Loader::initialize()) so I tried the example in the user guide which didn't give an error (http://ellislab.com/codeigniter/user-gui...e_lib.html)... neither work. Any thoughts?
#6

[eluser]Pygon[/eluser]
Bah -- you're right.

Quote:$config['image_library'] = 'GD2';

Did you mean 'GD'?
#7

[eluser]Référencement Google[/eluser]
Quote:as in the snippet provided by elitemedia

Yes yes, he is right, sorry I just copy and past from my code, but I am using this piece of code in a MY_Image_lib that extend the original one, this is why I don't call $this->image_lib->initialize() functions...
#8

[eluser]Référencement Google[/eluser]
Phusiondesign, you forgot to resize the image before calling the crop:
$this->image_lib->resize()

If you don't want resize, then in your code you call 2 times the initialize fonction, should be called only once per image work function.
#9

[eluser]phusiondesign[/eluser]
Ok, here is where I am at now. The resize portion is working correctly by itself with the crop part removed, as it creates a thumbnail 200px wide regardless of the height. Check!

The new problem is that it is not capturing the new thumbnail. It seems to be cropping the original image, but I have tried to eliminate that possibility.

Code:
$config['image_library'] = 'GD2';
$config['maintain_ratio'] = TRUE;

// create a thumbnail
$config['source_image'] = 'public/images/item_images/'.$item->item_data['image'];
$config['quality'] = 100;
$config['width'] = 200;
$config['height'] = 120;
$config['create_thumb'] = TRUE;
$config['thumb_marker'] = '_th';
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'width';

$this->image_lib->initialize($config);
                                    
if( ! $this->image_lib->resize()) {
  echo $this->image_lib->display_errors();
  echo 'resize?';
}
                                    
// crop the thumbnail
$imageName = $this->image_lib->explode_name('public/images/item_images/'.$item->item_data['image']);
$config['source_image'] = 'public/images/item_images/'.$imageName['name'].'_th'.$imageName['ext'];
                                    
$config['quality'] = 100;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['x_axis'] = 0;
$config['y_axis'] = 120;
                                    
$this->image_lib->initialize($config);
                                    
if( ! $this->image_lib->crop()){
  echo $this->image_lib->display_errors();
  echo 'crop?';
}
#10

[eluser]Référencement Google[/eluser]
Quote:It seems to be cropping the original image, but I have tried to eliminate that possibility.

This is why I call 2 times in my code the resize function... I know, this is not very intuitive to understand why the Image Lib work like that but It's the only way I have found to make what I wanted.




Theme © iAndrew 2016 - Forum software by © MyBB