【问题标题】:Photo upload, Crop and Save using Codeigniter使用 Codeigniter 上传、裁剪和保存照片
【发布时间】:2016-09-25 10:04:55
【问题描述】:

我们正在开发一个基于 CI 框架的社交网络项目。我在网上搜索但很困惑。如何上传照片,允许用户裁剪照片并使用数据库保存,以便在调用时检索?我用这个来做头像。

正在查看:

<?php echo form_open_multipart ('index.php/Main/do_upload'); ?>
<table class="table">
    <tr>
        <td>Title</td>
        <td><?php echo form_input('title'); ?></td>
    </tr>
    <tr>
        <td>Image</td>
        <td><?php echo form_upload('userfile'); ?></td>
    </tr>
    <tr>
        <td></td>
        <td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
        <?php echo form_close(); ?>
    </tr>       
</table>

在控制器上:

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

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

    if ( ! $this->upload->do_upload('userfile'))
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('Material_view', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }
}

【问题讨论】:

  • 到目前为止你尝试了什么。用那个更新你的问题。以便我们为您提供改进代码的步骤。
  • 首先在视图上我使用 php 表单创建了表单,然后在控制器上创建了函数 do_upload()。我用参考做了,但没有用。
  • 为什么不能提供代码。如果您提供,它将有助于纠正错误。
  • @Aryan 通过单击编辑按钮将其粘贴到您的问题中

标签: php codeigniter model-view-controller


【解决方案1】:

同时使用 Bluimp Multiple fileupload (https://blueimp.github.io/jQuery-File-Upload) 和 Croppr (https://fengyuanchen.github.io/cropperjs/) 插件。 在 Bluimp 中,您可以获得足够的代码(他们的站点中有一个 codeigniter 类)来处理 codeigniter 中的文件上传。

【讨论】:

    【解决方案2】:

    请尝试使用cropper js。我已经使用了它,它非常简单,它将完全满足您的要求

    https://github.com/fengyuanchen/cropper

    【讨论】:

      【解决方案3】:

      创建一个Image模型并粘贴代码

      <?php 
      class Image_model extends CI_Model {
       public function __construct()  {
          parent::__construct();
          $this->load->helper('url');
          $this->load->library('upload');
          $this->load->library('image_lib');
      }
      public function do_resize($filename)
      {
      
          $source_path =  'uploads/' . $filename;
          $target_path =  'uploads/thumb/thumb_'.$filename;
      
          $config_manip = array(
      
              'image_library' => 'gd2',
              'source_image' => $source_path,
              'new_image' => $target_path,
              'maintain_ratio' => TRUE,
              'width' => 150,
              'height' => 150
          );
          $this->image_lib->initialize($config_manip);
          $this->load->library('image_lib', $config_manip);
      
      
          if (!$this->image_lib->resize()) {
              echo $this->image_lib->display_errors();
              die();
          }
      }
      
      public function img_upload()
      {
          $config = array(
              'upload_path' => "uploads",
              'allowed_types' => "*",
              'overwrite' => TRUE,
              'max_size' => "5048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
              'max_height' => "3000",
              'max_width' => "3000"
          );
          $this->upload->initialize($config);
          $this->load->library('upload', $config);
      
          if($this->upload->do_upload()) {
              $response   =    array('upload_data' => $this->upload->data());
              $this->do_resize($response['upload_data']['file_name']);
      
          }
          /*else{
              $error              =   array('error'=>$this->upload->display_errors());
              //print_r($error);die(); 
      
          }*/
        }
      }
      

      在你的控制器中像这样调用这个函数

      <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
       class Post extends CI_Controller {
         public function __construct(){
          parent::__construct();
          $this->load->model('image_model');
          }
         public function add() {
             if(isset($_FILES)){
                   $config                =   $this->image_model->img_upload();
                   $file_data             =   $this->upload->data();
                   $data['feature_image'] =   $file_data['file_name'];
                  }
         $lat_id      =       $this->your_model->save($data);
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-13
        • 1970-01-01
        • 2019-02-03
        • 2015-05-31
        • 2015-03-21
        • 1970-01-01
        相关资源
        最近更新 更多