简体   繁体   English

Codeigniter 图像在上传前调整大小

[英]Codeigniter image resize before upload

I am trying to upload multiple images in Codeigniter and reduce the size of each image.我正在尝试在 Codeigniter 中上传多个图像并减小每个图像的大小。 here is my view这是我的观点

 <?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();?>

and my javascript is look like我的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>

here is my controller这是我的控制器

 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*/
         }
       }
    }
 }

my problem is that only the first image is getting re-sized remains kept as its original size.我的问题是只有第一个图像被重新调整大小仍然保持其原始大小。 And the image is resized after once uploaded.并且图像在上传后调整大小。 I think this is not a proper way now Is there any alternative way to resize the image?我认为现在这不是正确的方法有没有其他方法可以调整图像大小? it may be better if resize before doing the upload.如果在上传之前调整大小可能会更好。

I have solved the problem resizing first image by making changes in my controller as `我已经解决了调整第一张图片大小的问题,将我的控制器更改为 `

$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();
      }
}

I solved image resize before upload image in codeigniter use this simple code.我使用这个简单的代码在codeigniter中上传图像之前解决了图像调整大小。

image.php图片.php

<form id="thumb_form" enctype="multipart/form-data" method="post">
    <div style="margin-bottom: 5px;">
       <label>Choose Image File</label>
       <input id="image" name="image" type="file" class="form-control"/>
    </div>
    <div>
       <input type="submit" class="btn btn-primary" name="add_video" id="add_video" value="Submit">
    </div>
</form>

//ajax call write here

ajax call or script ajax 调用或脚本

<script>
$("form#thumb_form").submit(function(event)
{
    event.preventDefault();
    var formData = new FormData($(this)[0]);
    $.ajax({
        url : "<?php echo site_url('Welcome/add_image_thumb');?>",
        type : "POST",
        data: formData,
        contentType: false,
        processData: false,
        dataType:"JSON",
        success : function(result)
        {
            alert(result);
        }
    });
});

Welcome.php欢迎.php

public  function add_image_thumb()
{
    if($_FILES['image']['name']=='')
    {
        $data['file_err']='please choose image';
    }
    else
    {
            $data = $_FILES['image']['name'];
            $config['image_library'] = 'gd2';
            $config['source_image'] = $_FILES['image']['tmp_name'];
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = FALSE;
            $config['width'] = 300;
            $config['height'] = 300;
            $config['new_image'] = 'asstes/thumb/' . $data;
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();
            $img = '<img src="' . base_url() . 'asstes/thumb/' . $data . '">';
            echo json_encode(array('img' => $img));
    }
}
//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();

                }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM