2

目前我正在学习和尝试 CodeIgniter 能够做到的事情。但我坚持同时制作多个缩略图。可能,我使用 Wordpress 搞砸了我的脑袋,并试图在 Codeigniter 中做这样的事情,但无论如何,这是我的代码

<?php

class Gallery_model extends CI_Model {

var $gallery_path;

function __construct() {
    parent::__construct();
    $this->load->helper('functions');
    $this->gallery_path = realpath(APPPATH . '../uploads');
}

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

    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->load->library('image_lib', $config);
        $this->image_lib->resize();
    }
}
}

目前,我只能自己创建一个图像,但我不能用它制作缩略图。也许有人可以增强代码,并让它工作:)

PS - 我尝试在 $image_sizes 之后获取所有内容,将其放入其他独立的 php 文件中,运行它,然后 var 将 $config 转储到 foreach 中,它似乎可以正常工作。

4

2 回答 2

13

像这样使用它:

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

initialize比使用$config.

于 2012-07-22T19:11:41.087 回答
-1

我解决了。

我创建这个函数来创建多个缩略图:

function _create_thumbs($file_name){
    // Image resizing config
    $config = array(
        // Large Image
        array(
            'image_library' => 'GD2',
            'source_image'  => './assets/images/'.$file_name,
            'maintain_ratio'=> FALSE,
            'width'         => 700,
            'height'        => 467,
            'new_image'     => './assets/images/large/'.$file_name
            ),
        // Medium Image
        array(
            'image_library' => 'GD2',
            'source_image'  => './assets/images/'.$file_name,
            'maintain_ratio'=> FALSE,
            'width'         => 600,
            'height'        => 400,
            'new_image'     => './assets/images/medium/'.$file_name
            ),
        // Small Image
        array(
            'image_library' => 'GD2',
            'source_image'  => './assets/images/'.$file_name,
            'maintain_ratio'=> FALSE,
            'width'         => 100,
            'height'        => 67,
            'new_image'     => './assets/images/small/'.$file_name
        ));

    $this->load->library('image_lib', $config[0]);
    foreach ($config as $item){
        $this->image_lib->initialize($item);
        if(!$this->image_lib->resize()){
            return false;
        }
        $this->image_lib->clear();
    }
}

然后,当我像这样上传图像时调用该函数:

function do_upload(){
  $config['upload_path'] = './assets/images/'; //path folder
    $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
    $config['encrypt_name'] = TRUE;

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

  if(!empty($_FILES['filefoto']['name'])){
        if ($this->upload->do_upload('filefoto')){
        $img = $this->upload->data();
          //Compress Image
            $this->_create_thumbs($img['file_name']);

            $title = $this->input->post('title',TRUE);
            $image_large = $img['file_name'];
            $image_medium = $img['file_name'];
            $image_small = $img['file_name'];

            $this->upload_model->insert_images($title,$image_large,$image_medium,$image_small);
                }else{
                echo $this->upload->display_errors();
              }

        }else{
                echo "image is empty or type of image not allowed";
        }

}

于 2019-03-05T14:13:51.627 回答