【问题标题】:Resize image in CodeIgniter while uploading and save that resized image in directory上传时在 CodeIgniter 中调整图像大小并将调整后的图像保存在目录中
【发布时间】:2019-06-28 23:06:56
【问题描述】:

我想在上传图片时调整其大小,并使用 CodeIgniter 保存调整后的图片。我尝试了一些代码,但不起作用。我也想知道如何在多个文件上传中应用它。有人可以帮忙

public function submited()
{
  //$this->load->library('image_lib');
  $config['upload_path'] = './images';
  $config['allowed_types'] = 'gif|jpg|png|jpeg';

  $this->load->library('upload',$config);

  if($this->upload->do_upload('myfile'))
  {
    $filedata = $this->upload->data();
    $this->load->library('image_lib');
    $this->load->library('image_lib');
    $config['allowed_types']='jpg|jpeg|png|gif';
    $config['image_library'] = 'gd2';
    $config['source_image'] = './images'; 
    $config['new_image'] = './images'; 
    $config['maintain_ratio'] = FALSE; 
    $config['width'] = 200;
    $config['height'] = 150; 
    $config['quality']   = 75;
    $this->image_lib->initialize($config);
    $this->image_lib->resize();
    $this->image_lib->clear(); 

  }
}

【问题讨论】:

    标签: php image codeigniter file-upload


    【解决方案1】:

    要调整图像大小,请使用 codeigniter 中的图像库,如下所示:

        $this->load->library('image_lib');
        $config['allowed_types']='jpg|jpeg|png|gif'; //extension to allow
        $config['image_library'] = 'gd2';
        $config['source_image'] = $file;    //your path uploaded file
        $config['new_image'] = $targetPath;     //path to save the new file
        $config['maintain_ratio'] = FALSE; //to maintain ratio of image
        $config['width'] = 200; // width to resize image
        $config['height'] = 150; 
        $config['quality']   = 75;
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        $this->image_lib->clear();  `
    
    

    【讨论】:

    • 可以,先上传文件再调整大小,如果不需要原始文件,可以取消链接。
    • 消息:getimagesize(E:/xamp/htdocs/Code/images):打开流失败:权限被拒绝这是错误
    • 您的原图上传了吗?我的理解是你的路径不正确。
    • yessss..原图上传到目录
    • 您的源图像应该是带有图像名称的完整路径。还包括一次库。
    【解决方案2】:

    创建一个辅助函数。并使用上传的图像文件路径调用辅助函数。它将用新尺寸替换您的图像。

    public function submitd()
    {
             $config['upload_path'] = './images';
             $config['allowed_types'] = 'gif|jpg|png|jpeg';
             $this->load->library('upload',$config);
             if($this->upload->do_upload('myfile'))
            {
               $filedata = $this->upload->data();
               $this->resize_image($filedata['full_path']);
            }
    }
    //you can create this function in helper or in same controller.
    function resize_image($file_path) {
      $CI =& get_instance();
        // Set your config up
        $config['image_library']    = "gd2";      
        $config['source_image']     = $file_path;      
        $config['create_thumb']     = TRUE;      
        $config['maintain_ratio']   = TRUE;     
        $config['new_image'] = $file_path; 
        $config['width'] = "320";      
        $config['height'] = "240";  
        $config['thumb_marker']=FALSE;
        $CI->load->library('image_lib');
    
        $CI->image_lib->initialize($config);
        // Do your manipulation
    
        if(!$CI->image_lib->resize())
        {
           $CI->image_lib->display_errors();  
        } 
        $CI->image_lib->clear(); 
    
    }
    

    【讨论】:

    • yesssss 我试过了......它不起作用..我没有很好地理解
    • @HHHHHH 上传文件时很简单,将其上传到 resize_image() 函数的路径。你可以简单地通过 $filedata['full_path'] 获取路径。我已经在答案中提到了。你明白了吗?
    • 我也可以将其应用于多个文件上传吗?
    • 数组没问题,所有图片一张一张上传。上传单个图像时调用该函数。
    猜你喜欢
    • 2013-08-20
    • 2011-10-24
    • 2015-06-19
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多