php – Codeigniter缩略图图像路径无法存储在MySql中
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – Codeigniter缩略图图像路径无法存储在MySql中,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3655字,纯文字阅读大概需要6分钟。
内容图文
我正在尝试使用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')
)
但它只在我的表格的thumbnail_path列中存储“./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_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’]. “_拇指”.看看你的文件系统,文件应该在那里供你查看.
所以要解决你的问题,这应该可以解决问题:
$pathinfo = pathinfo($data['upload_data']['file_name']);
$thumbnail_path = './uploads/'. $data['upload_data']['file_name'] . "_thumb" . $pathinfo['extension'];
内容总结
以上是互联网集市为您收集整理的php – Codeigniter缩略图图像路径无法存储在MySql中全部内容,希望文章能够帮你解决php – Codeigniter缩略图图像路径无法存储在MySql中所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。