上传前制作图片缩略图 - php codeigniter

时间:2016-09-19 11:40:40

标签: php image codeigniter

我想将图片大小调整为160 x 160并制作缩略图,然后将该缩略图存储在文件夹中。我不想存储真实图像,只能存储它的缩略图。以下是我的代码:

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

    $this->load->library('image_lib');    

    $blog_image = $_FILES['blog_image']['name'];

    $config = array ('upload_path' => './blogs/',
                     'allowed_types' => "jpeg|jpg|png",
                     'overwrite' => TRUE,
                     'image_library' => 'gd2',
                     'source_image' => $blog_image,
                     'create_thumb' => TRUE,
                     'maintain_ratio' => TRUE,
                     'width' => 160,
                     'height' => 160
                     );

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

$this->upload->do_upload('blog_image');

$this->image_lib->resize();

此代码不起作用。它上传图像而不调整大小。请帮助。

2 个答案:

答案 0 :(得分:0)

$config['image_library'] = 'gd2';
$config['source_image'] = $_FILES['image']['tmp_name'];
$config['new_image'] = $target_path
$config['maintain_ratio'] = TRUE;
$config['width']    = 160;
$config['height']   = 160;

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

if (!$this->image_lib->resize()) {
    echo $this->image_lib->display_errors();
}

查看此链接以获取更多信息... https://stackoverflow.com/a/13154916/6588826

答案 1 :(得分:0)

尝试以下示例代码。

    $config = array(
        'upload_path' => './blogs/', 
        'allowed_types' => 'jpg|png|gif',
        'max_filename' => '255',
        'encrypt_name' => TRUE,

    );


    $this->load->library('upload', $config);
    //check file successfully uploaded. 'blog_image' is the name of the input
    if ($this->upload->do_upload('blog_image')) {
        //Now go to resize
        $image_data = $this->upload->data();
        $config_resize = array(
                'image_library' => 'gd2',
                'source_image' => $image_data['full_path'], //get original image
                'maintain_ratio' => TRUE,
                'width' => 160,
                'height' => 160
            );

        $this->load->library('image_lib', $config_resize);
        if (!$this->image_lib->resize()) {

            print_r($this->image_lib->display_errors());
        }
    }else{
        print_r($this->upload->display_errors());
    }