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

Php Codeigniter缩略图图像路径无法存储在MySql中,php,mysql,codeigniter,image-uploading,gd2,Php,Mysql,Codeigniter,Image Uploading,Gd2,我正在尝试使用Codeigniter制作一个简单的应用程序,我需要在MySql数据库中存储缩略图路径。图像以及缩略图版本正确上载到上载文件夹中,我可以在上载时将图像路径存储在数据库中,但无法将缩略图路径存储在数据库中。我在控制器中尝试了以下操作: $thumbnail= $this->resize($data['upload_data']['full_path'], $data['upload_data'] ['file_name']); $thumbnail_path = './upl

我正在尝试使用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')
                    )
  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();

    }
但它只在我的表的缩略图路径列中存储“/uploads/”,而不是实际的缩略图路径。我希望它能存储像“/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();

    }
}

因此
thumbnail\u path
仅包含“/uploads/”:这告诉我们
$thumnail
为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\u path
。那是你的问题

您似乎只是从中复制了代码。根据上述文件(我的重点):

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

好了,给你!如果您的文件名是
$data['upload\u data']['file\u name']
,您的缩略图将是
$data['upload\u data']['file\u name']。“_thumb”
。查看一下您的文件系统,文件应该在那里供您查看

因此,要解决您的问题,这应该做到以下几点:

 $pathinfo = pathinfo($data['upload_data']['file_name']);
 $thumbnail_path = './uploads/'. $data['upload_data']['file_name'] . "_thumb" . $pathinfo['extension'];

如何从resize函数中获取新创建的图像的thumb版本的路径。我怎样才能让它返回路径?有办法吗?我试过你的方法。它试图在数据库中插入类似这样的内容。[插入
post
title
img\u path
thumbody
post\u body
)值('Hello world','./uploads/Tulips.jpg','./uploads/Tulips.jpg\u thumbjpg','I love php')。我已经检查过了。查看thumb版本图像路径是否错误。因为resize函数将thumb创建为“Tulips_thumb.jpg”,但它存储了其他内容。可能是通过一些实验,我可以存储thumb版本的路径,不是通过调整大小功能,而是通过pathinfo功能。不过我得到了一些提示。我正在努力。如果您能提供进一步帮助,我们将不胜感激。谢谢,是的,我已经能够在数据库中存储缩略图的路径,只需稍微修改一下您的方式。我用过:$thumbnail_path='./uploads/'$数据['upload_data']['raw_name']。“拇指”。"." . $路径信息['extension'];现在它可以成功地存储缩略图的实际路径。再次感谢你。保持幸福!很高兴我能帮助你!