Php 使用codeigniter上传图片缩略图预览
我做了一个codeigniter函数,所以当用户选择要上传的产品图片时,它必须以用户选择的形式显示为预览图片。这样用户就可以看到他选择的图片 这是一个链接,显示了它的外观: 控制器中的索引函数:Php 使用codeigniter上传图片缩略图预览,php,codeigniter,codeigniter-3,Php,Codeigniter,Codeigniter 3,我做了一个codeigniter函数,所以当用户选择要上传的产品图片时,它必须以用户选择的形式显示为预览图片。这样用户就可以看到他选择的图片 这是一个链接,显示了它的外观: 控制器中的索引函数: public function index() { $products = $this->db->get('products'); ( null !== $products->result() ) ? ( !empty( $products->result(
public function index()
{
$products = $this->db->get('products');
( null !== $products->result() ) ? ( !empty( $products->result() ) ? $products->result() : false ) : false;
$this->data = ['get_image' => $products];
//Laad kado uploaden view
$this->load->view('product_form', $data);
}
这是控制器代码中的上载功能:
public function upload() {
$this->load->library('upload');
$this->load->library('image_lib');
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['encrypt_name']= true;
$this->upload->initialize($config);
$products = $this->db->get('products');
( null !== $products->result() ) ? ( !empty( $products->result() ) ? $products->result() : false ) : false;
$this->data = ['get_image' => $products];
if(!$this->upload->do_upload('userfile')){
$error = array('error'=>$this->upload->display_errors());
$this->load->view('product_form', $error);
}else{
//Main image
$data = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = './upload/'.$data["raw_name"].$data['file_ext'];
$config['new_image'] = './upload/'.'new_'.$data["raw_name"].$data['file_ext'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 547;
$config['height'] = 430;
//Thumb image
$dataThumb = $this->upload->data();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = './upload/'.$dataThumb["raw_name"].$dataThumb['file_ext'];
$configThumb['new_image'] = './upload/'.'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'];
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = FALSE;
$configThumb['width'] = 120;
$configThumb['height'] = 112;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
$this-> db-> insert('products', array(
'user_id' => $_SESSION['user_id'],
'product_foto' => 'new_'.$data["raw_name"].$data['file_ext'],
'product_foto_thumb' => 'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'],
'product_naam' => $this->input->post('product_naam'),
'product_beschrijving' => $this->input->post('product_beschrijving'),
'ophaal_plaats' => $this->input->post('ophaal_plaats'),
'category_id' => !empty($this->input->post('category_id')) ? $this->input->post('category_id') : 0,
'date_created' => date('Y-m-d'),
'date_updated' => date('Y-m-d')
));
$data['img'] = base_url().
'/upload/new_'.$data["raw_name"].$data['file_ext'];
$dataThumb['img'] = base_url().
'/upload/thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'];
header('location:https://kadokado-ferran10.c9users.io//Product/cadeaupagina_aangeboden');
}
}
这是product_foto行的视图表单文件:
<div class="image-upload">
<input id="file-input" type="file" name="userfile"/>
<h3>Upload foto </h3>
<label for="file-input">
<img src="../images/PLUS.jpg"/>
</label>
</div>
</td>
<?php if ($get_image) {
echo '<tr>';
echo '<td>'.img(array('width' => '120', 'height' => '80', 'src' => 'upload/'.$data->product_foto)).'</td>';
echo '<td> Bekijk | Verwijder </td>';
echo '</tr>';
}
else {
echo '<tr>';
echo '<td> </td>';
echo '</tr>';
} ?>
</tr>
第二个错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: get_image
Filename: views/product_form.php
Line Number: 67
Backtrace:
File: /home/ubuntu/workspace/application/views/product_form.php
Line: 67
Function: _error_handler
File: /home/ubuntu/workspace/application/controllers/Product.php
Line: 21
Function: view
File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once
数据库信息和其他信息:
The map where the picture is stored: upload
The database table name: products
The product picture row is named: product_foto
排除上载代码中可能存在的任何问题,您之所以会出现这些错误,是因为您正在将
$data
分配给类作用域$this->data
,而不是在视图调用中访问$this->data
修订守则:
public function index()
{
$products = $this->db->get('products');
( null !== $products->result() ) ? (!empty($products->result()) ? $products->result() : false ) : false;
$data = array('get_image' => $products);
//Laad kado uploaden view
$this->load->view('product_form', $data);
}
作为补充说明,我认为您不必访问
$this->upload->data()代码>相同数据的两倍。只需将其分配给类似的变量,如$image
这不是一个问题,而是我的代码和一些错误。请提出一个特定的问题,并详细说明脚本到底发生了什么-图像是否正在上载和调整大小?是的,我问为什么我不能在视图中使用get_image变量那么我应该将$this->upload->data()放在哪里?您正在访问它两次。我不是说你应该移除它,只是重复两次是多余的。这是相同的数据。在您的视图中,您正在访问$data->product\u foto,如果您使用我的方法$get_image->product_foto,那么应该更改这些参数……我能建议的最好的事情是了解类变量有一个类作用域,因为类中的所有函数都可以访问它,并且没有“this”的局部变量只能访问分配给它的函数中的变量。
public function index()
{
$products = $this->db->get('products');
( null !== $products->result() ) ? (!empty($products->result()) ? $products->result() : false ) : false;
$data = array('get_image' => $products);
//Laad kado uploaden view
$this->load->view('product_form', $data);
}