【问题标题】:Codeigniter - How to upload original image with thumbnail image?Codeigniter - 如何上传带有缩略图的原始图像?
【发布时间】:2015-09-30 10:42:35
【问题描述】:

我有这段代码可以在 codeigniter 中上传文件:

if(!empty($_FILES['userfile'])){
    $name_array = array();
    $count = count($_FILES['userfile']['size']);
    foreach($_FILES as $key => $value)
        for ($s=0; $s<=$count-1; $s++){
            $_FILES['userfile']['name'] = $value['name'][$s];
            $_FILES['userfile']['type'] = $value['type'][$s];
            $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
            $_FILES['userfile']['error'] = $value['error'][$s];
            $_FILES['userfile']['size'] = $value['size'][$s];
            $config['upload_path'] = './public/images/campaign-images/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
            $config['max_size'] = '10000';
            //$config['max_width'] = '1024';
            //$config['max_height'] = '768';
            $CI->load->library('upload', $config);
            $CI->upload->do_upload();
            $data = $CI->upload->data();
            $name_array[] = $data['file_name'];
        }
    return $name_array;
}

此代码非常适合上传单个原始图片,但如何将图片上传为缩略图(350 x 250)并在不同文件夹中调整大小。

知道如何使用 codeigniter 库吗?

谢谢

【问题讨论】:

    标签: php codeigniter codeigniter-2


    【解决方案1】:

    先保存原图,然后上传缩略图 您只需要包含 gd2 库即可生成缩略图

                if(!empty($_FILES['userfile'])){
                    $name_array = array();
                    $count = count($_FILES['userfile']['size']);
                    foreach($_FILES as $key => $value)
                        for ($s=0; $s<=$count-1; $s++)
                        {
                            //Original Image Upload - Start
                            $_FILES['userfile']['name'] = $value['name'][$s];
                            $_FILES['userfile']['type'] = $value['type'][$s];
                            $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
                            $_FILES['userfile']['error'] = $value['error'][$s];
                            $_FILES['userfile']['size'] = $value['size'][$s];
                            $config['upload_path'] = './public/images/campaign-images/';
                            $config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
                            $config['max_size'] = '10000';
                            //$config['max_width'] = '1024';
                            //$config['max_height'] = '768';
                            $CI->load->library('upload', $config);
                            $CI->upload->do_upload();
                            $data = $CI->upload->data();
                            //Original Image Upload - End
    
                            //Thumbnail Image Upload - Start
                            $config['image_library'] = 'gd2';
                            $config['source_image'] = './public/images/campaign-images/'. $value['name'][$s];
                            $config['new_image'] = './public/images/campaign-images/thumbs/'.$value['name'][$s];
                            $config['width'] = 350;
                            $config['height'] = 250;
    
                            //load resize library
                            $this->load->library('image_lib', $config);
                            $this->image_lib->resize();
                            //Thumbnail Image Upload - End
    
                            $name_array[] = $data['file_name'];
                        }
                    return $name_array;
                }
    

    【讨论】:

    • 感谢您对我的支持。我已添加您的代码,但无法在 thumbs 文件夹中看到上传的文件。
    • 修改源图片路径如下 $config['source_image'] = $value['name'][$s];
    • 对于源图像,你需要传递实际的图像路径,我在这里使用了循环变量 //Thumbnail Image Upload - Start $config['image_library'] = 'gd2'; $config['source_image'] = $value['name'][$s]; . . .
    • 抱歉,我仍然对此感到困惑 $config['source_image'] = $value['name'][$s]; 我正在上传此文件夹中的原始图像 ./public/images/campaign-images/ 和此文件夹中的拇指 ./public/images/campaign-images/thumbs/
    • 关于 $config['source_image'] = $value['name'][$s]; 的更多信息检查这个ellislab.com/codeigniter/user-guide/libraries/image_lib.html
    【解决方案2】:
    if(!empty($_FILES['userfile'])){
                $name_array = array();
                $count = count($_FILES['userfile']['size']);
                foreach($_FILES as $key => $value)
                    for ($s=0; $s<=$count-1; $s++)
                    {
                        //Original Image Upload - Start
                        $_FILES['userfile']['name'] = $value['name'][$s];
                        $_FILES['userfile']['type'] = $value['type'][$s];
                        $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
                        $_FILES['userfile']['error'] = $value['error'][$s];
                        $_FILES['userfile']['size'] = $value['size'][$s];
                        $config['upload_path'] = 'PATH_TO_UPLOAD_IMAGE';
                        $config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
                        $config['max_size'] = '10000';
                        $CI->load->library('upload', $config);
                        $CI->upload->do_upload();
                        $data = $CI->upload->data();
                        //Original Size of Image Upload - End
    
                        //Thumbnail Size of Image Upload - Start
                        $config['image_library'] = 'gd2';
                        $config['source_image'] = 'PATH_TO_IMAGE_UPLOAD'. $value['name'][$s];
                        $config['new_image'] = 'PATH_TO_UPLOAD_THUMB_IMAGE'.$value['name'][$s];
                        $config['width'] = 350;
                        $config['height'] = 250;
    
                        //CI load resize library
                        $this->load->library('image_lib', $config);
                        $this->image_lib->resize();
                        //Thumbnail SIZED Image Upload - End
    
                        $name_array[] = $data['file_name'];
                    }
                return $name_array;
            }
    

    【讨论】:

      猜你喜欢
      • 2012-12-12
      • 2013-10-28
      • 1970-01-01
      • 2016-11-02
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      • 2016-04-09
      • 2017-01-27
      相关资源
      最近更新 更多