Welcome Guest, Not a member yet? Register   Sign In
resizing multiple images: what goes wrong?
#1

[eluser]JoostV[/eluser]
Hi,

I'm trying to resize several instances of a single image. I placed 3 different width, height and thumbnail_marker settings in an array $this->data['config']['image_upload']['art']

Then, I loop through this array. The idea is that 3 different thmbnails will be created in the same folder, each with a different size and name (such as '19_640.jpg, 19_320.jpg' and '19_120.jpg').

image_lib appears to execute the different resizings, but fails to name the thumbs differently. I.e. the same image gets rewritten 3 times, instead of 3 different files being created. So I just end up with a single image 19.jpg in my images folder. Anybody any idea how I can fix this?

This is the code:
Code:
<?php

//Set general config for image resize
$config['image_library'] = 'ImageMagick';
$config['library_path'] = '/usr/bin/';
$config['source_image'] = $filepath;
$config['maintain_ratio'] = true;
$config['create_thumb'] = false;

//Instantiate image library
$this->load->library('image_lib', $config);

//Set thumb and resize options in a loop
foreach ($this->data['config']['image_upload']['art'] as $img) {
    
    //If a prefix is specified, set appropriate thumb marker
    if ($img['prefix']) {
        $this->image_lib->create_thumb = true;
        $this->image_lib->thumb_marker = $img['prefix'];
    }
    //Else, set thumb marker to empty string
    else {
        $this->image_lib->create_thumb = false;
        $this->image_lib->thumb_marker = '';
    }
    
    //Set appropriate width & height
    $this->image_lib->width = $img['maxwidth'];
    $this->image_lib->height = $img['maxheight'];

    //Resize the image
    if ( ! $this->image_lib->resize()) {
        echo $this->image_lib->display_errors();
    }
}

?>
#2

[eluser]cdevroe[/eluser]
Sad to say, I am having this issue nearly 3 years later.
#3

[eluser]cdevroe[/eluser]
For the sake of keeping this thread up-to-date I wanted to say that I found the solution to this issue.

http://makdotgnu.wordpress.com/2009/03/0...deigniter/

Essentially, you use the initialize() and clear() methods rather than loading the library the way the documentation says to do it.
#4

[eluser]JoostV[/eluser]
There's a link to a image resizing lib in my footer that handles multiple image resizing and cropping, if you're interested.
#5

[eluser]ChrisBrad51[/eluser]
A lot of thanks goes to JoostV for his quality decent answer. this really a good post having some important code used for resizing image. Nice sharing.
#6

[eluser]JoostV[/eluser]
Thanks, Chris, my pleasure Smile
#7

[eluser]Unknown[/eluser]
Thanks Joost, I was having the same issue with resizing multiple images and this library works perfectly!
#8

[eluser]predat0r[/eluser]
My code works with multi image resize. You don't need extra library..
It's a little triky though..

1. You have to initialize the $config with initialize()
2. before init you have to use clear() in the loop

Code:
$this->load->library('image_lib');                
                
                foreach($files as $file) {
                    list($width, $height, $type, $attr) = getimagesize('./galeriak/'.$folder_name.'/orig/'.$file);
                    
                    if($width < $height) {
                        $config = array(
                            'source_image' => './galeriak/'.$folder_name.'/orig/'.$file,
                            'new_image' => './galeriak/'.$folder_name.'/thumb/'.$file,
                            'maintain_ratio' => TRUE,
                            'width' => 92,
                            'height' => 123
                        );
                    } else {
                        $config = array(
                            'source_image' => './galeriak/'.$folder_name.'/orig/'.$file,
                            'new_image' => './galeriak/'.$folder_name.'/thumb/'.$file,
                            'maintain_ratio' => TRUE,
                            'width' => 123,
                            'height' => 92
                        );
                    }
                    $this->image_lib->clear();
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                }
#9

[eluser]behnampmdg3[/eluser]
[quote author="predat0r" date="1305986196"]My code works with multi image resize. You don't need extra library..
It's a little triky though..

1. You have to initialize the $config with initialize()
2. before init you have to use clear() in the loop

Code:
$this->load->library('image_lib');                
                
                foreach($files as $file) {
                    list($width, $height, $type, $attr) = getimagesize('./galeriak/'.$folder_name.'/orig/'.$file);
                    
                    if($width < $height) {                         $c
                            'source_image' => './galeriak/'.$folder_name.'/orig/'.$file,
                            'new_image' => './galeriak/'.$folder_name.'/thumb/'.$file,
                            'maintain_ratio' => TRUE,
                            'width' => 92,
                            'height' => 123
                        );
                    } else {
                        $config = array(
                            'source_image' => './galeriak/'.$folder_name.'/orig/'.$file,
                            'new_image' => './galeriak/'.$folder_name.'/thumb/'.$file,
                            'maintain_ratio' => TRUE,
                            'width' => 123,
                            'height' => 92
                        );
                    }
                    $this->image_lib->clear();
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                }
[/quote]predat0r is right, worked for me too




Theme © iAndrew 2016 - Forum software by © MyBB