Welcome Guest, Not a member yet? Register   Sign In
Need help for resize uploaded picture.
#1

[eluser]lamhuy300890[/eluser]
I have write code to multiple field uploading and resize thumbnail picture.
The code can upload picture from all field but it only resize the first picture.
Anyone can help me ?

Sorry for my bad English.

Thanks.

Code:
//set config for upload image
    $config['upload_path'] = './images/product/';
    $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
    $config['encrypt_name'] = TRUE;
    //$config['quality'] = '80%';
    //$config['max_size'] = '100';
    //$config['max_width']  = '1024';
    //$config['max_height']  = '768';
    $this->load->library('upload', $config);
    //end set config
    
    for($i = 1; $i <= 10; $i ++)
    {
     if ( $_POST['checkP' . $i] != "" )
     {
      $field_name = "pPicture" . $i;
      if ( ! $this->upload->do_upload($field_name) )
      {
       $message = "Hình ảnh không đúng định dạng hoặc quá lớn";
      }
      else
      {
       $uploadData = $this->upload->data();
      
       //resize image
       $config['image_library'] = 'gd2';
       $config['source_image'] = $uploadData['full_path'];
       $config['width'] = 60;
       $config['height'] = 90;
       //$config['create_thumb'] = TRUE;
       //$config['maintain_ratio'] = TRUE;
       $config['new_image'] = './images/product_thumb/' . $uploadData['file_name'] ;
       $this->load->library('image_lib', $config);
       //end set thumbs config
       $this->image_lib->resize();
      
        
       $this->product_model->update_productpicture($idProduct, $i, $uploadData['file_name']);
      }
     }
    }
#2

[eluser]LuckyFella73[/eluser]
Hi Lamhuy,

try it this way - should work:
Code:
&lt;?php
function yourmethod(){

// ... more of your code before
$uploadData = $this->upload->data();

// from here change code to
$this->_resize_mood($uploadData);

$this->product_model->update_productpicture($idProduct, $i, $uploadData['file_name']);
}



function _resize_mood($uploadData)
{
$config['image_library'] = 'gd2';
$config['source_image'] = $uploadData['full_path'].$uploadData['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 60;
$config['height'] = 90;

$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
}


?&gt;

The important part is where you fire the clear function. I like tp put
such code into an extra function - you could copy that lines into your
upload method directly of course.
#3

[eluser]lamhuy300890[/eluser]
Hi LuckyFella73,

It work great. Thank you so much.
#4

[eluser]LuckyFella73[/eluser]
You are welcome!
#5

[eluser]the_unforgiven[/eluser]
im need to know solution to my code too if anyone is willing help.

this is my code: which works inserts into the database and uploads to the specific folder in upload path config, but what i want to do is resize it before it does any of that. how and example would be appreciated based on my code.

Code:
function _do_upload_file()
{
     //upload path and config
  $config['upload_path']   = './assets/uploads/userfiles/';
     $config['allowed_types'] = 'png|jpg|gif|jpeg';
     $config['max_size'] = '3000';

     $this->load->library('upload', $config);
  $this->upload->initialize($config);
  
     if (!$this->upload->do_upload())
     {
         $this->form_validation->set_message('_do_upload_file', $this->upload->display_errors());
         return FALSE;
     }
     else
     {
   $filedata = $this->upload->data();
   $this->filename = $filedata['file_name'];
  
         return TRUE;
  }
  

}
#6

[eluser]LuckyFella73[/eluser]
Quote:but what i want to do is resize it before it does any of that
On PHP base you can't resize the image before uploading
(if its that what you meant)

What you can do is something like that:
Code:
// return "$filedata" like you have it now instead of "TRUE"
// or set a class property like $this->filedata in your upload function and pass it
$filedata = $this->_do_upload_file();
when calling the resize function.

if ($filedata !== FALSE){
$this->_resize_mood($filedata); // see provided code a few posts ago and edit format
}

In case you have your "_do_upload_file" function as a callback
as a form-validation rule just set up a new callback function
and fire the upload and resize function inside the callback.
#7

[eluser]the_unforgiven[/eluser]
Hi,

Im not sure i understand what you mean,yes sorry i realised you can't resize until after upload, so i need to upload the image then resize it and place in a thumbs folder how can this be done?

Sorry to be a pain just trying to get a better understanding....




Theme © iAndrew 2016 - Forum software by © MyBB