如何使用图像处理codeigniter在我的表中创建和保存创建的拇指

时间:2017-11-08 16:39:12

标签: php codeigniter

我有一个名为Manage Menu的模块,管理员将添加另一个菜单。我使用图像处理类使所有大小都相同。我不知道如何将它应用到我的系统中,我只是尝试将其作为静态并且有效。

静态

public function index()
    {
        $config = array(
            'image_library'     => 'gd2',
            'source_image'      => 'assets/img/2.jpg',
            'create_thumb'      => 'true',
            'width'             =>  575,
            'height'            =>  550,
        );
        $this->load->library('image_lib',$config);
        $this->image_lib->resize();

问题:如何调整上传图像的大小并将其保存到我的桌面?

**我的控制器**

public function new_menu_form_submit()
    {
        $image = array(
            'image_library'     => 'gd2',
            'source_image'      => 'upload/'.'$this->upload->data("file_name")',
            'create_thumb'      => 'true',
            'width'             =>  575,
            'height'            =>  550,
        );
        $this->load->library('image_lib',$image);
        $this->image_lib->resize();
        $config = array(
            'upload_path'   => 'uploads',
            'allowed_types' => 'jpg|png|PNG|JPG',
            'max_size'      => '2048', //25MB
            'encrypt_name'  => TRUE //encrypt filename
            );

        $this->load->library('upload', $config);
        if($this->form_validation->run('new-menu') == FALSE) {
            $error = [

                'menu_name_error'         => form_error('menu_name'),
                'menu_price_error'         => form_error('menu_price'),
                'menu_stocks_error'         => form_error('menu_stocks'),
                'menu_image_error'         => form_error('menu_image'),
            ];

            echo json_encode($error);
        }else{
            $uppercase_menu_name = ucwords($this->input->post('menu_name'));

            $menu_name = preg_replace('/(.)(?=[A-Z])/u', '$2', $uppercase_menu_name);
            $insert_menu = [
                'menu_name'               => clean_data($menu_name),
                'menu_price'               => $_POST['menu_price'],
                'menu_stocks'               => $_POST['menu_stocks'],
                'menu_image'               => $this->upload->data('file_name'),

                'status'                    => "Active"
            ];
            $this->Crud_model->insert('menu',$insert_menu);
            echo json_encode("success");
        }
    }

查看

<div class="content-wrapper">
    <div class="container-fluid">
      <!-- Breadcrumbs-->
      <ol class="breadcrumb">
        <li class="breadcrumb-item">
          <a href="<?= base_url().'administrator/dashboard'?>">Dashboard</a>
        </li>
        <li class="breadcrumb-item">
          <a href="<?= base_url().'administrator/manage_stocks'?>">Stocks</a>
        </li>
        <li class="breadcrumb-item active">Edit Stocks</li>
      </ol>
      <!-- Icon Cards-->
      <div class="row">

        <div class="col-md-6">
          <ol class="breadcrumb">
          <div id="success-message-new-menu"></div>
          <form id="new-menu">

            <div class="form-group">
              <label>Name:</label>
              <input type="text" class="form-control" name="menu_name" id="menu_name">
               <div class="text-message" id="menu_name_error"></div>
            </div>
            <div class="form-group">
              <label>Price:</label>
              <input type="text" class="form-control" name="menu_price" id="menu_price">
              <div class="text-message" id="menu_price_error"></div>
            </div>
            <div class="form-group">
              <label>Stocks:</label>
              <input type="number" class="form-control" min="1" name="menu_stocks" id="menu_stocks" >
              <div class="text-message" id="menu_stocks_error"></div>
            </div>
            <div class="form-group">
               <label>File</label>
              <input type="file" name="menu_image" id="menu_image" class="form-control">
               <div class="text-message" id="menu_image_error"></div>
            </div>

            <input type="submit" class="btn btn-success " value="Continue" align="center" ">

          </form>


          </ol>
        </div>






      </div>

    </div>
    <!-- /.container-fluid-->

    <!-- /.content-wrapper-->
     <!-- Logout Modal-->
        <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
                <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">×</span>
                </button>
              </div>
              <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
              <div class="modal-footer">
                <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
                <a class="btn btn-primary" href="<?= base_url(). 'administrator/logout'?>">Logout</a>
              </div>
            </div>
          </div>
        </div>
    <footer class="sticky-footer">
      <div class="container">
        <div class="text-center">
          <small>Copyright © Your Website 2017</small>
        </div>
      </div>
    </footer>
    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
      <i class="fa fa-angle-up"></i>
    </a>

JS

$(document).ready(function(){
    $("#new-menu").on('submit',function(e){
        var form = new FormData(document.getElementById("new-menu"));
        $.ajax({
            url: base_url + "administrator/new_menu_form_submit",
            data: form,
            type: "POST",
            processData: false, // tell jQuery not to process the data
            contentType: false, // tell jQuery not to set contentType
            success:function(data) {
                var result = JSON.parse(data);

                if(result === "success")
                {
                    $("h5").html("");
                    success_message("#success-message-new-menu","New Menu Added Successfully!");
                    window.setTimeout(function(){location.href=base_url+"administrator/menu"},2000);

                }else{

                    $("#menu_name_error").html(result.menu_name_error);
                    $("#menu_price_error").html(result.menu_price_error);
                    $("#menu_stocks_error").html(result.menu_stocks_error);
                    $("#menu_stocks_error").html(result.menu_stocks_error);
                    $("#menu_image_error").html(result.menu_image_error);
                }
            },
            error: function(data) {
                alert('error');
            }
        })
        e.preventDefault();
    })
})

注意:所以在这里,我使用了javascript。一旦表单被提交,它将指向我的js然后该进程将执行url中的任何内容:....

下面这行代码检查是否所有字段都填满了。

if($this->form_validation->run('new-menu') == FALSE) {....

编辑:

public function new_menu_form_submit()
{

    $config = array(
        'upload_path'   => 'uploads',
        'allowed_types' => 'jpg|png|PNG|JPG',
        'max_size'      => '2048', //25MB
        'encrypt_name'  => TRUE //encrypt filename
        );

    $this->load->library('upload', $config);
    if($this->form_validation->run('new-menu') == FALSE) {
        $error = [

            'menu_name_error'         => form_error('menu_name'),
            'menu_price_error'         => form_error('menu_price'),
            'menu_stocks_error'         => form_error('menu_stocks'),
            'menu_image_error'         => form_error('menu_image'),
        ];

        echo json_encode($error);
    }else{
        if($this->upload->do_upload('menu_image'))
        { // added
            $uppercase_menu_name = ucwords($this->input->post('menu_name'));

        $menu_name = preg_replace('/(.)(?=[A-Z])/u', '$2', $uppercase_menu_name);

             $uploaded_data = $this->upload->data(); // added

            $this->resize_uploaded_image($uploaded_data); //added

             $insert_menu = [
            'menu_name'               => clean_data($menu_name),
            'menu_price'               => $_POST['menu_price'],
            'menu_stocks'               => $_POST['menu_stocks'],
            'menu_image'               => $uploaded_data['file_name'], //edited 'menu_image'               => $this->upload->data('file_name'),

            'status'                    => "Active"
            ];
        } // added
        // $uploaded_data = $this->upload->data(); // remove
        // $this->resize_uploaded_image($uploaded_data); //remove


        // $uppercase_menu_name = ucwords($this->input->post('menu_name'));

        // $menu_name = preg_replace('/(.)(?=[A-Z])/u', '$2', $uppercase_menu_name);
        // $insert_menu = [
        //     'menu_name'               => clean_data($menu_name),
        //     'menu_price'               => $_POST['menu_price'],
        //     'menu_stocks'               => $_POST['menu_stocks'],
        //     'menu_image'               => $uploaded_data['file_name'], //edited 'menu_image'               => $this->upload->data('file_name'),

        //     'status'                    => "Active"
        // ];
        $this->Crud_model->insert('menu',$insert_menu);
        echo json_encode("success");
    }
}

 public function resize_uploaded_image($image_data)
    {
       $config = array(
            'image_library'     => 'gd2',
            'source_image'      => $image_data['uploads'], //uploaded imagepath
            'create_thumb'      => 'true',
            'width'             =>  575,
            'height'            =>  550,
        );

       $this->load->library('image_lib', $config);  
       $this->image_lib->resize(); 
    }

在这部分中将数据插入我的表格

$insert_menu = [
                'menu_name'               => clean_data($menu_name),
                'menu_price'               => $_POST['menu_price'],
                'menu_stocks'               => $_POST['menu_stocks'],
                'menu_image'               => $this->upload->data('file_name'),

                'status'                    => "Active"
            ];

1 个答案:

答案 0 :(得分:0)

您首先需要上传图片:

readyState = Complete

当然你会添加错误处理之类的东西。请不要复制/粘贴,因为我确定这里有很多错误,但这是对这个过程的粗略概念。