【问题标题】:make image thumbnail before upload - php codeigniter在上传之前制作图像缩略图 - php codeigniter
【发布时间】:2017-01-27 02:14:00
【问题描述】:

我想将图像调整为 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();

此代码不起作用。它在不调整大小的情况下上传图像。请帮忙。

【问题讨论】:

  • 您的服务器是否安装了 GD/GD2、NetPBM 或 ImageMagick?
  • 我不知道。我该如何检查。我目前正在我的本地主机 AppServ 上工作
  • 您使用的是 Windows、Mac 还是 Linux?
  • 我在 Windows 8 上

标签: php image codeigniter


【解决方案1】:
$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

【讨论】:

    【解决方案2】:

    试试下面的示例代码。

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

    【讨论】:

      猜你喜欢
      • 2011-03-24
      • 2012-09-21
      • 2017-12-12
      • 1970-01-01
      • 2013-08-26
      • 2021-01-03
      • 2011-02-23
      • 2012-07-25
      • 1970-01-01
      相关资源
      最近更新 更多