php CodeIgniter 上传图片,然后调整它的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10027083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:10:17  来源:igfitidea点击:

CodeIgniter Upload image and then resize it

phpcodeignitercodeigniter-2

提问by user794846

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('', ''));   
              }
               }      
           }  

采纳答案by Altrim

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.

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

回答by Maitho David

i have been trying to create thumbnails for the past 20 hrs and i was not successful until i realized where i was messing up with the code, i was not INITIALIZING my config array!!!

在过去的 20 小时里,我一直在尝试创建缩略图,但直到我意识到我在哪里弄乱了代码,我才成功,我没有初始化我的配置数组!!!

Just add this line of code after loading your image_lib

只需在加载image_lib后添加这行代码

$this->image_lib->initialize($config);

Hope this works for you...........thumbs up!

希望这对你有用.........竖起大拇指!

回答by Oguz Kurtcuoglu

If you like to play with Image with CI, I highly recommend , image_moo library.

如果你喜欢用 CI 来玩 Image,我强烈推荐 image_moo 库。