4

我正在尝试使用 Codeigniter 制作一个简单的应用程序,我需要在 MySql 数据库中存储缩略图图像路径。图像连同缩略图版本正确上传到上传文件夹中,我可以在上传时将图像路径存储在我的数据库中,但我无法将缩略图图像路径存储在数据库中。我在控制器中试过这个:

$thumbnail= $this->resize($data['upload_data']['full_path'], $data['upload_data']  ['file_name']);
$thumbnail_path = './uploads/'. $thumbnail;
$newRow = array(
                    "title" => $this->input->post('title'),
                    "img_path" => $imgpath,
                    "thumbnail_path" => $thumbnail_path,
                    "post_body" => $this->input->post('post_body')
                    )

但它只将“./uploads/”存储在我表的 thumbnail_path 列中,而不是实际的缩略图路径。我希望它在哪里存储类似“./uploads/Lighthouse_thumb.jpg”的东西。我不知道如何获取缩略图图像的路径以将它们存储在数据库中。这是我的完整上传控制器:

  class Upload extends CI_Controller
  {

function __construct()
{
    parent::__construct();
    $this->load->helper('form');    
}
function index() {

        $this->load->view('upload_form', array('error'=>''));
    }

function do_upload() {
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '1000';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

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

    if (!$this->upload->do_upload()) {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('upload_form', $error);
    } else {
        $image_data = $this->upload->data();
        $imgpath = './uploads/' . $image_data['file_name'];
        $data = array('upload_data'=>$this->upload->data());
        $thumbnail= $this->resize($data['upload_data']['full_path'], $data['upload_data']['file_name']);
        $thumbnail_path = './uploads/'. $thumbnail;
        $newRow = array(
                    "title" => $this->input->post('title'),
                    "img_path" => $imgpath,
                    "thumbnail_path" => $thumbnail_path,
                    "post_body" => $this->input->post('post_body')
                    );
            $this->load->model('postimage_path');
            $this->postimage_path->insert($newRow);
            $this->load->view('upload_success', $data);
    }

    }
function resize($path, $file) {
        $config['image_library'] = 'gd2';
        $config['source_image'] =  $path;
        $config['create_thumb'] = TRUE;
        $config['maintian_ratio'] = TRUE;
        $config['width'] = 100;
        $config['height'] = 100;
        $config['new_image'] = './uploads/' . $file;

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

    }

}

4

1 回答 1

1
$thumbnail_path = './uploads/'. $thumbnail;

所以thumbnail_path只包含 "./uploads/" :这告诉我们这$thumbnail是错误的(或 null 或空字符串等),这意味着调用 resize 的返回值是错误的 - 似乎您希望它返回文件名。

function resize($path, $file) {
    $config['image_library'] = 'gd2';
    $config['source_image'] =  $path;
    $config['create_thumb'] = TRUE;
    $config['maintian_ratio'] = TRUE;
    $config['width'] = 100;
    $config['height'] = 100;
    $config['new_image'] = './uploads/' . $file;

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

}

resize什么都不返回!而你没有分配给$thumbnail_path. 那是你的问题。

您似乎只是从CodeIgniter 文档中复制了代码。从所述文档(强调我的):

上面的代码告诉 image_resize 函数在 source_image 文件夹中查找名为 mypic.jpg 的图像,然后使用 GD2 image_library 创建一个 75 X 50 像素的缩略图。由于启用了maintain_ratio 选项,拇指将尽可能接近目标宽度和高度,同时保持原始纵横比。缩略图将被称为 mypic_thumb.jpg

所以你有它!如果您的文件名是$data['upload_data']['file_name'],您的缩略图将是$data['upload_data']['file_name'] . "_thumb". 查看您的文件系统,文件应该在那里供您查看。

因此,要解决您的问题,这应该可以解决问题:

 $pathinfo = pathinfo($data['upload_data']['file_name']);
 $thumbnail_path = './uploads/'. $data['upload_data']['file_name'] . "_thumb" . $pathinfo['extension'];
于 2014-12-18T13:59:09.090 回答