【问题标题】:Creating multiple resized images in codeigniter?在codeigniter中创建多个调整大小的图像?
【发布时间】:2015-08-10 04:10:44
【问题描述】:

我试图上传一张图片并创建该图片的多个调整大小的副本,以便在网站的不同位置使用。我正在使用 CodeIgniter Image Manipulation Class,它运行良好,我能够获得调整大小的图像,但是当我尝试创建多个调整大小的图像时,它不起作用。

//controller
<?php
class Upload extends CI_Controller{
    public function upload_image(){
        $this->upload_model->do_upload(); //execute the upload function
    }
}

//model
<?php
class User_model extends CI_Model{
  var $original_path;
  var $resized_path;
  var $thumbs_path;

  //initialize the path where you want to save your images
  function __construct(){
    parent::__construct();
    //return the full path of the directory
    //make sure these directories have read and write permessions
    $this->original_path = realpath(APPPATH.'../uploads/original');
    $this->resized_path = realpath(APPPATH.'../uploads/resized');
    $this->thumbs_path = realpath(APPPATH.'../uploads/thumbs');
  }

  function do_upload(){
    $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

        // 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->image_lib->initialize($config);
    $this->image_lib->resize();

【问题讨论】:

标签: php image codeigniter


【解决方案1】:

当我使用 CI 图像处理类时,我认为调用 resize 函数两次会产生两个调整大小的图像,但实际上并没有工作,经过一些研究和教程后,我发现我必须每次初始化 image_lib 类是时候调用 resize() 函数了。

所以你的函数应该是这样的

function do_upload(){
    $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
    );


    //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();

    desires $config array 
    //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
    • 2012-07-15
    • 2018-02-14
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    相关资源
    最近更新 更多