CodeIgniter上传图片,然后重新调整大小 [英] CodeIgniter Upload image and then resize it

查看:287
本文介绍了CodeIgniter上传图片,然后重新调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想上传图片并调整其大小,到目前为止,上传的工作,以及缩略图的创建。但我不能得到原始的图像调整大小。所有这一切发生的是文件是直接上传到文件夹的原始大小。



这是我的代码我缺少什么:

  //初始化上传文件类
$ config ['upload_path'] ='./image_uploads/';
$ config ['allowed_types'] ='jpg | jpeg | gif | png';
$ config ['max_size'] ='6048';
$ this-> load-> library('upload',$ config);

$ userfile =_upload_image;
//检查文件是否正在上传
if(strlen($ _ FILES [_ upload_image] [name])> 0){

if $ this-> upload-> do_upload($ userfile))//检查上传是否失败
{
$ upload_errors = $ this-> upload-> display_errors('','' );
$ this-> session-> set_flashdata('errors','Error:'。$ upload_errors);
redirect('admin / view_food /'.$ food-> course_id);
}
else
{
// $ image_data = $ this-> upload-> data();

//// [THUMB IMAGE]
$ config2 ['image_library'] ='gd2';
$ config2 ['source_image'] = $ this-> upload-> upload_path。$ this-> upload-> file_name;
$ config2 ['new_image'] ='./image_uploads/thumbs';
$ config2 ['maintain_ratio'] = TRUE;
$ config2 ['create_thumb'] = TRUE;
$ config2 ['thumb_marker'] ='_thumb';
$ config2 ['width'] = 75;
$ config2 ['height'] = 50;
$ this-> load-> library('image_lib',$ config2);

if(!$ this-> image_lib-> resize()){
$ this-> session-> set_flashdata('errors',$ this-> image_lib - > display_errors('',''));
}

// [MAIN IMAGE]
$ config ['image_library'] ='gd2';
$ config ['source_image'] = $ this-> upload-> upload_path。$ this-> upload-> file_name;
// $ config ['new_image'] ='./image_uploads/';
$ config ['maintain_ratio'] = TRUE;
$ config ['width'] = 250;
$ config ['height'] = 250;
$ this-> load-> library('image_lib',$ config);

if(!$ this-> image_lib-> resize()){
$ this-> session-> set_flashdata('errors',$ this-> image_lib - > display_errors('',''));
}
}
}


解决方案>

您尝试添加到配置选项

  $ config ['overwrite'] = TRUE; 

这应该可以做。



根据文档


如果设置为true,如果存在与您正在上传的文件同名的文件,则该文件将被覆盖。如果设置为false,则会有一个数字附加到文件名,如果另一个具有相同的名称。



I am trying to upload images and resize them, so far the upload is working and also the creation of thumb nail images. But I cant get the original image to be resized. All that is happening is the file is directly uploaded to the folder in its original size. I guess I am trying to take this image resize it then overwrite the originally uploaded file.

Here is my code what am I missing:

//Initialise the upload file class
    $config['upload_path'] = './image_uploads/';
    $config['allowed_types'] = 'jpg|jpeg|gif|png';
    $config['max_size'] = '6048';
    $this->load->library('upload', $config);

    $userfile = "_upload_image";
            //check if a file is being uploaded
            if(strlen($_FILES["_upload_image"]["name"])>0){

                if ( !$this->upload->do_upload($userfile))//Check if upload is unsuccessful
                {
                    $upload_errors = $this->upload->display_errors('', '');
                    $this->session->set_flashdata('errors', 'Error: '.$upload_errors);   
                    redirect('admin/view_food/'.$food->course_id);  
                }
                else
                {
                    //$image_data = $this->upload->data();

                     ////[ THUMB IMAGE ]
                    $config2['image_library'] = 'gd2';
                    $config2['source_image'] = $this->upload->upload_path.$this->upload->file_name;
                    $config2['new_image'] = './image_uploads/thumbs';
                    $config2['maintain_ratio'] = TRUE;
                    $config2['create_thumb'] = TRUE;
                    $config2['thumb_marker'] = '_thumb';
                    $config2['width'] = 75;
                    $config2['height'] = 50;
                    $this->load->library('image_lib',$config2); 

                    if ( !$this->image_lib->resize()){
                $this->session->set_flashdata('errors', $this->image_lib->display_errors('', ''));   
              }

                    //[ MAIN IMAGE ]
                    $config['image_library'] = 'gd2';
                    $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
                    //$config['new_image'] = './image_uploads/';
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 250;
                    $config['height'] = 250;
                    $this->load->library('image_lib',$config); 

                    if ( !$this->image_lib->resize()){
                $this->session->set_flashdata('errors', $this->image_lib->display_errors('', ''));   
              }
               }      
           }  

解决方案

Did you tried to add this to the config options

$config['overwrite'] = TRUE;

This should do the trick.

According to the documentation

If set to true, if a file with the same name as the one you are uploading exists, it will be overwritten. If set to false, a number will be appended to the filename if another with the>same name exists.

这篇关于CodeIgniter上传图片,然后重新调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆