Welcome Guest, Not a member yet? Register   Sign In
Problem With Image Resizing
#1
Star 

Hello to All Members,

I have a problem in image resizing. I have a function name as do_resize(). it gets 4 parameters and return the name of the the new resized file. the code of this function is given below.
PHP Code:
public function do_resize($source_file$target_folder$height 128$width 128)
 
   {
 
       $filename $source_file;
 
       $temp_data explode('/',$filename);
 
       $new_filename end($temp_data);

 
       $temp_data explode('.'$new_filename);
 
       $ext end($temp_data);
 
       $new_filename $temp_data[0] . $width .'-'$height .'.'$ext;
 
       $source_path $filename;

 
       $folder_path '';
 
       $temp_folder explode('/',$target_folder);

 
       foreach ($temp_folder as $folder) {
 
           $folder_path .=$folder '/';
 
           if (!file_exists($folder_path)) {
 
               mkdir($folder_path);
 
           }
 
       }
 
       $target_path $target_folder;

 
       if(isset($config_manip)){
 
           unset($config_manip);
 
       }

 
       $config_manip = array(
 
           'image_library'     => 'gd2',
 
           'source_image'      => $source_path,
 
           'maintain_ratio'    => FALSE,
 
           'new_image'         => $target_path,
 
           'create_thumb'      => TRUE,
 
           'thumb_marker'      =>  $width '-'$height,
 
           'width'             => $width,
 
           'height'            => $height
        
);
 
       
        $this
->load->library('image_lib'$config_manip);
 
       if (!$this->image_lib->resize()) {
 
           echo $this->image_lib->display_errors();
 
           echo "<br>";
 
           echo $config_manip['source_image'];
 
       }
 
       // clear //
 
       $this->image_lib->clear();
 
       return $folder_path $new_filename;
 
   

This function only work for one time. I'm calling this function from a loop to resize multiple images. the code of the loop is like this.
PHP Code:
foreach ($data['distributors'] as $key => $item){
 
    $target_folder 'uploads/images/cache/distributor/profile-image';
 
    $name $this->do_resize($item['profile_image'], $target_folder128,128);
 
    $data['distributors'][$key]['profile_image'] =  base_url($name);
 
    $data['distributors'][$key]['added_date'] =  date($this->dateFormatstrtotime($item['added_date']));



I'm expecting from this code is that. When I starts the loop, at each attrition of the loop it takes the profile-image path and pass to the do_resize() function which will resize the image and store it in a target folder. But it works only for first attrition of the loop. and remaining profile-images are not resized. 

Any one can help me to solve this issue. Thanks
Reply
#2

IF you look at your code you will see that you are over writing your
foreach loop data $data['distributors']
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(10-19-2017, 03:28 AM)InsiteFX Wrote: IF you look at your code you will see that you are over writing your
foreach loop data $data['distributors']

Yes I know, I'm overwriting the original data with the modified data. There is no problem in this overwriting. In fact I found the solution for this problem and that is written below.
PHP Code:
$this->image_lib->initialize($config_manip); 

Thanks for your reply.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB