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 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误