dongpu42006096 2016-02-27 14:43
浏览 28

我们上传时如何减少codeigniter中图像的kb文件大小?

I have a page where user will drag and drop multiple images. I need to resize and store it as thumbnail and also i have to reduce the big images in kb without losing the image width and height. What would be the best way do it in codeigniter?

     if ($_FILES["file"]["name"])
        {

        $targetPath = APPPATH . 'uploads/portfolios/';

        $result = $this->do_upload("file", $targetPath);
        $data =  array();
        if (!$result['status']) {
                $data['error_msg'] ="Can not upload Image for " . $result['error'] . " ";

        }

                else {

    $this->resize_image($targetPath . $result['upload_data']['file_name'],$targetPath,'120','120');


    $file_name = $result['upload_data']['raw_name'].'_thumb'.$result['upload_data']['file_ext'];    


        }

function resize_image($sourcePath, $desPath, $width = '500', $height = '500')
{

    $this->load->library('image_lib');
    $this->image_lib->clear();
    $config['image_library'] = 'gd2';
    $config['source_image'] = $sourcePath;
    $config['new_image'] = $desPath;
    //$config['quality'] = '100%';
    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = false;
    $config['thumb_marker'] = '_thumb';
    $config['width'] = 120;
    $config['height'] = 120;
 $this->image_lib->initialize($config);

    if ($this->image_lib->resize())

        return true;
    return false;
}

  function do_upload($htmlFieldName, $path)
{
    $config['file_name'] = time();
    $config['upload_path'] = $path;
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = '20000';
    //$config['max_width'] = '2000';
    //$config['max_height'] = '2000';
    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    unset($config);
    if (!$this->upload->do_upload($htmlFieldName))
    {
        return array('error' => $this->upload->display_errors(), 'status' => 0);
    } else
    {
        return array('status' => 1, 'upload_data' => $this->upload->data());
    }
}
  • 写回答

1条回答 默认 最新

  • dounanyin3179 2016-09-09 16:40
    关注

    I had similar problem which is solved via native php function.

    Credit to pez-cuckow from his original answer. From it's question also have an improvement but it remove support for gif image.

    function compressImage($source_url, $destination_url, $quality) {
    
      $info = getimagesize($source_url);
    
      if ($info['mime'] == 'image/jpeg') 
        $image= imagecreatefromjpeg($source_url);
      elseif ($info['mime'] == 'image/gif')
        $image = imagecreatefromgif($source_url);
      elseif ($info['mime'] == 'image/png')
        $image = imagecreatefrompng($source_url);
    
      //save file
      imagejpeg($image, $destination_url, $quality);
    
      //return destination file
      return $destination_url;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上