Welcome Guest, Not a member yet? Register   Sign In
Am I using the image lib library properly?
#1

[eluser]JasonS[/eluser]
Compared to other parts of CI, this seems to be a terrible amount of code to do what I am trying to do.

1 - Measure image, determine whether width or height or is larger.
2 - Resize image by smallest attribute
3 - Crop image

Code:
// Make Thumb
$config['image_library'] = 'gd2';
$config['source_image']    = getcwd().'/uploads/' . $upload['file_name'];
$config['new_image']    = getcwd().'/uploads/_40thumb_' . $upload['file_name'];
$config['maintain_ratio'] = TRUE;
if ($upload['image_width'] > $upload['image_height']) {
    $width = (40 / $upload['image_height']) * $upload['image_width'];
    $height = 40;
} else {
    $width = (40 / $upload['image_width']) * $upload['image_height'];
    $height = 40;
}
$config['width'] = $width;
$config['height'] = $height;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
            
$this->image_lib->clear();
                
// Crop
unset($config['new_image']);
$config['source_image'] = getcwd().'/uploads/_40thumb_' . $upload['file_name'];
$config['width'] = 40;
$config['height'] = 40;
$config['maintain_ratio'] = FALSE;
$this->image_lib->initialize($config);
$this->image_lib->crop();
#2

[eluser]d1a8lo24[/eluser]
When it comes to image manipulation just a simple upload and re-size can become a large block of code.

In one of my apps i have over 50 lines of code so i can upload, optimize(re-size, crop, quality), store in a database and save it in its own folder.

That's just to get the image uploaded, to manipulate the image over 100 lines of code and more than 6 functions this is to focus, rotate, re-crop, re-size, watermark, update the database with all this parameter and finally output to the browser.

But writing this much code helps minimize different functions kind of like a library or model you write a lot of code so you can then use small pieces to manipulate data/files/images.

In this case your benefit is that the image is uploaded and re-size and all a user had to do is click upload.




Theme © iAndrew 2016 - Forum software by © MyBB