首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用具有php的插件裁剪和上传照片

如何使用具有php的插件裁剪和上传照片
EN

Stack Overflow用户
提问于 2015-01-22 10:01:21
回答 2查看 13.3K关注 0票数 13

因此,我目前发现了一个名为cropit的照片剪切插件。演示是here。因此,我要做的是获取裁剪的照片,并将照片的名称上传到mysql数据库,并使用php保存到目录中。

到目前为止我有这样的想法:

HTML :

代码语言:javascript
复制
<form method="POST">
    <div class="image-editor">
        <div class="cropit-image-preview-container">
            <div class="cropit-image-preview"></div>
        </div>
            <div class="image-size-label">
            Resize image
        </div>
        <input type="range" class="cropit-image-zoom-input">
        <input type="hidden" name="image-data" class="hidden-image-data" />
        <button type="submit">Submit</button>
    </div>
</form>

jQUERY :

代码语言:javascript
复制
    $('form').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);

        // Prevent the form from actually submitting
        return false;
    });

我所需要的只是php设置代码,因为当我裁剪照片并选择submit时,jquery返回序列化代码,而我通常不熟悉的所有代码都会出现。以下是jquery返回的序列化代码的几个字符:

代码语言:javascript
复制
image-data=data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhE...
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-22 10:26:30

1.保存base64编码的图像

代码语言:javascript
复制
    <?php
    //save your data into a variable - last part is the base64 encoded image
    $encoded = "data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhE";

    //decode the url, because we want to use decoded characters 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);

2.获取base64编码的图像的文件名

代码语言:javascript
复制
    $encoded = "data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhE";
    $decoded = urldecode($encoded);
    $exp = explode(';', $decoded);
    $exp = explode(':', $exp[0]);
    $image = array_pop($exp);
    echo ($image);
票数 14
EN

Stack Overflow用户

发布于 2015-11-27 19:36:56

我通过不解码编码数据得到了Hosch Nok的答案。不打电话:

代码语言:javascript
复制
$decoded = urldecode($encoded);

但是直接使用$encoded变量。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28085848

复制
相关文章

相似问题

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