首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有php的cropit上传

具有php的cropit上传
EN

Stack Overflow用户
提问于 2015-11-09 17:36:04
回答 1查看 2.5K关注 0票数 1

我使用cropit.js来上传带有php的图像。

我只是跟着这个问题How to crop and upload photo using cropit jquery plugin with php

这是我的HTML代码:

代码语言:javascript
复制
<form action="#" class="form-horizontal form-bordered" id="formUpload">
  <div class="image-editor">
    <div class="form-group">
        <div class="col-xs-12">
            <button id="fakeUpload" class="btn btn-default col-xs-12"><i class="fa fa-upload"></i> Scegli la foto da caricare</button>
            <input type="file" class="cropit-image-input" name="bannerUpload" id="bannerUpload" style="display:none;">
        </div>
    </div>
    <div class="form-group">
        <div class="col-xs-12 text-center">
            <div class="cropit-image-preview"></div>
        </div>
    </div>
    <div class="form-group">
        <label for="zoomUpload" class="col-md-2">Zoom</label>
        <div class="col-md-10 text-center">
            <input type="range" class="cropit-image-zoom-input" id="zoomUpload">
        </div>
    </div>
    <div class="form-group">
        <div class="col-xs-12 text-center">
            <input type="hidden" name="image-data" class="hidden-image-data" />
            <button type="submit" class="btn btn-primary col-xs-12"><i class="fa fa-cloud-upload"></i> Carica</button>
        </div>
    </div>
  </div>
</form>

这是JS:

代码语言:javascript
复制
$('#formUpload').submit(function() {
    // Move cropped image data to hidden input
    var imageData = $('.image-editor').cropit('export');
    $('.hidden-image-data').val(imageData);

    // Print HTTP request params
    var formValue = $(this).serialize();
    //$('#result-data').text(formValue);

    $.ajax({
      type: "POST",
      url : "cpu/uploadBanner.php",
      data: formValue,
      success: function(msg){
          //$("#AjaxResult").html(msg);
          alert(msg);
      }
    })
    // Prevent the form from actually submitting
  return false;
});

最后,PHP代码:

代码语言:javascript
复制
<?php
include '../inc/config.php';

if($_SERVER['REQUEST_METHOD']=="POST"){
    $encoded = $_POST['image-data'];
    //decode the url, because we want to use normal charackters to use explode
    $decoded = urldecode($encoded);
    //explode at ',' - the last part should be the encoded image now
    $exp = explode(',', $decoded);
    //we just get the last element with array_pop
    $base64 = array_pop($exp);
    //decode the image and finally save it
    $data = base64_decode($base64);
    $file = 'data.png';
    //make sure you are the owner and have the rights to write content
    file_put_contents($file, $data);
    echo 'ok';
}else{
    echo "Non dovresti essere quì!";
}
?>

当表单提交时,我看到了名为“data.png”的新图像,但我看到了所有的黑色,不保存图像更正或什么?该文件夹具有777权限。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-21 01:37:59

尝试以下PHP代码:

代码语言:javascript
复制
<?php
include '../inc/config.php';

if($_SERVER['REQUEST_METHOD']=="POST"){
    $encoded = $_POST['image-data'];
    //explode at ',' - the last part should be the encoded image now
    $exp = explode(',', $encoded);
    //decode the image and finally save it
    $data = base64_decode($exp[1]);
    $file = 'data.png';
    //make sure you are the owner and have the rights to write content
    file_put_contents($file, $data);
    echo 'ok';
}else{
    echo "Non dovresti essere quì!";
}
?>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33614835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档