【问题标题】:Create multiple size images from one image upload in codeigniter在 codeigniter 中从一张图片上传创建多个尺寸的图片
【发布时间】:2018-06-25 22:26:07
【问题描述】:

我遇到了使用 codeigniter image_lib 创建多个图像的问题。这是我的代码。问题是只有原始图像正在上传,而不是其他图像,如数组中定义的拇指、中、大。

我的控制器代码是:

function do_upload() {

    $config = array(
        'allowed_types' => 'jpg|jpeg|gif|png',
        'upload_path' => $this->gallery_path,
        'max_size' => 2000
    );

    $this->load->library('upload', $config);
    $this->upload->do_upload();
    $image_data = $this->upload->data();


    $image_sizes = array(
        'thumb' => array(150, 100),
        'medium' => array(300, 300),
        'large' => array(800, 600)
    );

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

foreach ($image_sizes as $resize) {

    $config = array(
        'source_image' => $image_data['full_path'],
        'new_image' => $this->gallery_path . '-' . $resize[0] . 'x' . 
    $resize[1],
        'maintain_ration' => true,
        'width' => $resize[0],
        'height' => $resize[1]
    );

    $this->image_lib->initialize($config);
    $this->image_lib->resize();
    $this->image_lib->clear();
    $this->load->library('upload', $config);
    $this->upload->do_upload();
    $image_data = $this->upload->data();

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

【问题讨论】:

    标签: php codeigniter image-uploading


    【解决方案1】:

    试试这个方法,别忘了清除缓存...

    $this->load->library('image_lib');
    $config = array(
    'allowed_types'     => 'jpg|jpeg|gif|png', //only accept these file types
    'max_size'          => 2048, //2MB max
    'upload_path'       => $this->original_path //upload directory
    

    );

    $this->load->library('upload', $config);
    $image_data = $this->upload->data(); //upload the image
    
    //your desired config for the resize() function
    $config = array(
    'source_image'      => $image_data['full_path'], //path to the uploaded image
    'new_image'         => $this->resized_path, //path to
    'maintain_ratio'    => true,
    'width'             => 128,
    'height'            => 128
    );
    
    //this is the magic line that enables you generate multiple thumbnails
    //you have to call the initialize() function each time you call the resize()
    //otherwise it will not work and only generate one thumbnail
    $this->image_lib->initialize($config);
    $this->image_lib->resize();
    
    $config = array(
    'source_image'      => $image_data['full_path'],
    'new_image'         => $this->thumbs_path,
    'maintain_ratio'    => true,
    'width'             => 36,
    'height'            => 36
    );
    //here is the second thumbnail, notice the call for the initialize() function again
    $this->image_lib->initialize($config);
    $this->image_lib->resize();
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-31
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-10
      相关资源
      最近更新 更多