在不降低代码生成器高度和宽度的情况下缩小图像大小的方法是什么?

时间:2017-11-22 07:54:06

标签: php image codeigniter upload

我尝试在Codeigniter中创建图片上传功能。在上传文件时我想保持高度和宽度但是尺寸应该减小,例如从10kb减少到5kb。

以下代码工作正常,文件上传但大小相同。 可以做些什么改变才能使其正确

        if($_FILES['logo']['name'] != '')
        {

            $getimagesize  = getimagesize($_FILES['logo']['tmp_name']);
            $w = $getimagesize[0];
            $h = $getimagesize[1];
            #NEW CODE
            $config['upload_path'] = './upload/'; 
            $config['allowed_types'] = 'jpg|jpeg|png';
            $this->load->library('upload',$config);
            if(!$this->upload->do_upload('logo'))
            {
                echo $this->upload->display_errors();
            }#EOF if(!$this->upload->do_upload('logo'))
            else
            {
                $data = $this->upload->data();
                $config = [
                    'image_library' => 'gd',
                    'source_image' => './upload/'.$data['file_name'],
                    'create_thumb' => FALSE,
                    'maintain_ratio' => FALSE,
                    'quality' => '60',
                    'height' => $h,
                    'width' => $w,
                    'new_image' => './upload/'.$data['file_name']
                ];
                $this->load->library('image_lib',$config);
                if($this->image_lib->resize())
                {
                    $this->session->set_flashdata('success','image saved');
                    redirect('welcome/school_details');
                }
                #Adding data into databse


            }

1 个答案:

答案 0 :(得分:1)

更改配置上传:

'quality' => '60'

'quality' => '60%'