php - Codeigniter 缩略图图像路径无法存储在 MySql 中

标签 php mysql codeigniter image-uploading gd2

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

$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')
                    )

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

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

    }

最佳答案

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

所以 thumbnail_path 只包含“./uploads/”:这告诉我们 $thumbnail 是 false(或 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。那是你的问题。

您似乎只是复制了 the CodeIgniter docs 中的代码.来自所述文档(强调我的):

The above code tells the image_resize function to look for an image called mypic.jpg located in the source_image folder, then create a thumbnail that is 75 X 50 pixels using the GD2 image_library. Since the maintain_ratio option is enabled, the thumb will be as close to the target width and height as possible while preserving the original aspect ratio. The thumbnail will be called 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'];

关于php - Codeigniter 缩略图图像路径无法存储在 MySql 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27548000/

相关文章:

php - swift 3.0 中的播放器 ID 格式问题

php - 如何从 PHP 脚本运行 linux 命令

PHP PDO SQLITE - 连接

php - AES_DECRYPT() 返回空字段 codeigniter

php - SQL 代码点火器 : Create savepoint and roll-back to the savepoint/Rollback multiple transaction from controller

php - 同义词查找算法

php - 如何将LONGTEXT存储到MySql数据库

mysql加入: Where to put the condition that goes on the right table?

php - 如何将标题 View 包含到 codeigniter 中的所有其他 View 中

java - 如何在 hibernate 中映射自动增量字段?