2015-04-14 54 views
0

我想在codeigniter中上传多个图像并减小每个图像的大小。 这里是我的看法codeingniter在上传前调整图像的大小

<?php echo form_open_multipart('main_controller/do_insert');?> 
    <div id="mainDiv"> 
    <div class='group'> 
     <div><input type="text" name="name[]"/></div> 
     <div><input type="file" name="img[]"/></div> 
    </div> 
    </div> 
    <input type="button" id="add an entry"> 
    <input type="submit" value="save all"/> 
<?php from_close();?> 

和我的JavaScript看起来像

<script> 
function add(x) 
{ 
var str1="<div><input type='text' name='name"+x+"'/></div>" 
var str2="<div><input type='file' name='img"+x+"'/></div>" 
var str3="<input type='button' value='add an entry' onClick='add(x+1)'>"; 
$("#mainDiv").append(str1+str2+str3); 
} 
</script> 

这里是我的控制器

function do_insert{ 
    while($i<=$counter) /*conter have value of total number for images just ignore*/ 
    { 
     $config['upload_path'] = './images/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 

     $this->load->library('upload', $config); 

     if (! $this->upload->do_upload($userfileName)) 
     { 
     echo "error".count;  
     } 
     else 
     { 
     $data = array('upload_data' => $this->upload->data()); 
     $img=$data['upload_data']['file_name']; /*for geting uploaded image name*/ 

     $config['image_library'] = 'gd2'; 
     $config['source_image'] = './images/'.$img; 
     $config['new_image'] = './images/'; 
     $config['maintain_ratio'] = TRUE; 
     $config['width'] = 640; 
     $config['height'] = 480; 

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

     if (!$this->image_lib->resize()) { 
      echo $this->image_lib->display_errors(); 
     } 
     else 
     { 
      echo "success"; /*and some code here*/ 
     } 
     } 
    } 
} 

我的问题是只有第一形象是越来越重定尺寸仍然保持因为它原来的大小。 图片在上传后调整大小。我认为这不是一个正确的方式现在 有没有其他的方式来调整图像大小?上传前调整大小可能会更好。

+0

我没有关于变更的图像的任何建议,但你试过'未设置($ IMG调整第一图像的问题, $ data)'while'while循环之外?这会使变量在每次处理循环时都重新分配,这应该解决只调整第一个图像的问题。 – Iain

回答

2

我已经解决了通过在我的控制器变化`

$this->load->library('image_lib'); 
while($i<=$counter) /*conter have value of total number for images just ignore*/ 
{ 
     $config['upload_path'] = './images/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $this->load->library('upload', $config); 
     if (! $this->upload->do_upload($userfileName)) 
     { 
     echo "error".count;  
     } 
     else 
     { 
      $image_data = $this->upload->data(); 

      $configer = array(
       'image_library' => 'gd2', 
       'source_image' => $image_data['full_path'], 
       'maintain_ratio' => TRUE, 
       'width'   => 250, 
       'height'   => 250, 
      ); 
      $this->image_lib->clear(); 
      $this->image_lib->initialize($configer); 
      $this->image_lib->resize(); 
     } 
} 
0
//Here is my upload controller and really works in local and server 
//load you image_lib to your config 


$config = array(
      'upload_path' => './upload/', 
      'log_threshold' => 1, 
      'allowed_types' => 'jpg|png|jpeg|gif|JPEG|JPG|PNG', 
      'max_size' => 10000, 
      'max_width'=>0, 
      'overwrite' => false 
     ); 


      for($i = 1 ; $i <=8 ; $i++) { 
       $upload = 'upload'.$i; //set var in upload 
       if(!empty($upload)){ 

        $this->load->library('upload', $config); 
        $this->upload->do_upload('upload'.$i); 
        $upload_data = $this->upload->data(); 
        $file_name = $upload_data['file_name']; 
        // process resize image before upload 
        $configer = array(
          'image_library' => 'gd2', 
          'source_image' => $upload_data['full_path'], 
          'create_thumb' => FALSE,//tell the CI do not create thumbnail on image 
          'maintain_ratio' => TRUE, 
          'quality' => '40%', //tell CI to reduce the image quality and affect the image size 
          'width' => 640,//new size of image 
          'height' => 480,//new size of image 
         ); 
        $this->image_lib->clear(); 
        $this->image_lib->initialize($configer); 
        $this->image_lib->resize(); 

       } 

     }