duansaoguan7955 2018-07-13 02:50
浏览 45
已采纳

如何在上传时调整多个图像文件的大小?

I have the following php code for uploading:

$data = array();
$filesCount = count($_FILES['img']['name']);
for($i = 0; $i < $filesCount; $i++)
{
    $_FILES['userFile']['name'] = $_FILES['img']['name'][$i];
    $_FILES['userFile']['type'] = $_FILES['img']['type'][$i];
    $_FILES['userFile']['tmp_name'] = $_FILES['img']['tmp_name'][$i];
    $_FILES['userFile']['error'] = $_FILES['img']['error'][$i];
    $_FILES['userFile']['size'] = $_FILES['img']['size'][$i];


    $id = $this->session->userdata('ses_id');
    $tabel = $this->session->userdata('ses_tabel');
    if (!file_exists('uploads/'.$tabel.'/kategori'))
    {
        mkdir('uploads/'.$tabel.'/kategori', 0777, true);
    }else{  
        $uploadPath = 'uploads/'.$tabel.'/kategori';
        $newname = $idkategori.'_'.$nama;
        $config['upload_path'] = $uploadPath;
        $config['allowed_types'] = 'jpg|png|jpeg';
        $config['file_name'] = $newname;

        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if($this->upload->do_upload('userFile')){
            $fileData = $this->upload->data();
            $cfg['image_library'] = 'gd2';
            $cfg['quality'] = '50%';
            $this->load->library('image_lib');
            $this->image_lib->initialize($cfg);
            $this->image_lib->resize();
            $uploadData[$i]['nama'] = $this->input->post('nama_kategori');
            $uploadData[$i]['file_name'] = $fileData['file_name'];
            $uploadData[$i]['created'] = date("Y-m-d H:i:s");
            $uploadData[$i]['modified'] = date("Y-m-d H:i:s");
            $uploadData[$i]['id_resto'] = $id;
            $uploadData[$i]['id_kategori'] = $idkategori;
        }
    }
}

My file gets uploaded but is not resized, I want to resize image so that when I request it on an Android device, the image file is not too big. If possible please help me resize width and height too.

sorry for bad english, thank you.

  • 写回答

3条回答 默认 最新

  • dtt78245 2018-07-13 03:25
    关注

    You need to set the path of the newly uploaded image for the image lib config.

    Read more here.

        $this->upload->initialize($config, true);  // important 2nd param
        if ($this->upload->do_upload('userFile')) {
            $fileData = $this->upload->data();
            $cfg['source_image'] = $fileData['full_path'];
            list($width, $height) = getimagesize($fileData['full_path']);
            $cfg['width'] = $width - 1;
            $cfg['height'] = $height - 1;
            $cfg['image_library'] = 'gd2';
            $cfg['quality'] = '50%';
            $this->load->library('image_lib');
            $this->image_lib->initialize($cfg);
            if (!$this->image_lib->resize()) {
                log_message('error', 'resize failed: ' . $this->image_lib->display_errors());
            }
            $this->image_lib->clear();
            $uploadData[$i]['nama'] = $this->input->post('nama_kategori');
            $uploadData[$i]['file_name'] = $fileData['file_name'];
            $uploadData[$i]['created'] = date("Y-m-d H:i:s");
            $uploadData[$i]['modified'] = date("Y-m-d H:i:s");
            $uploadData[$i]['id_resto'] = $id;
            $uploadData[$i]['id_kategori'] = $idkategori;
        }
    

    UPDATE:

    If you only want to change quality:

    So I forgot I already answered this type of question before. If you dig into the image library code you will notice that the quality variable is only used when something actually needs to be resized! CI checks to see if the image size matches the designated output size and if it is a match or if the image width and height aren't specified in the config it just moves on... Thus the quality will never be affected. To hack around this you can get the width and height and subtract 1px. I wouldn't call it a bug but more of an oversight.

    I personally don't like CI's upload or image lib and use https://github.com/verot/class.upload.php (this issue is not a problem there).

    Here is the answer where I figured this all out: Image compress in Codeigniter3

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)