Javascript 如何使用带有php的cropit jquery插件裁剪和上传照片

Javascript 如何使用带有php的cropit jquery插件裁剪和上传照片,javascript,php,jquery,mysql,Javascript,Php,Jquery,Mysql,所以我现在发现了这个名为的照片裁剪插件。演示正在进行中。所以我想做的是抓取裁剪后的照片,将照片的名称上传到mysql数据库,并使用php将其保存到一个目录中 到目前为止,我有: HTML: 我所需要的帮助是php设置代码,因为当我裁剪照片并选择submit时,jquery返回序列化代码,所有这些我通常不熟悉的代码都会出现。以下是jquery返回的序列化代码的几个字符: image-data=data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhE...

所以我现在发现了这个名为的照片裁剪插件。演示正在进行中。所以我想做的是抓取裁剪后的照片,将照片的名称上传到mysql数据库,并使用php将其保存到一个目录中

到目前为止,我有:

HTML:

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

image-data=data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhE...

1。保存base64编码图像

    <?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);
    $encoded = "data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhE";
    $decoded = urldecode($encoded);
    $exp = explode(';', $decoded);
    $exp = explode(':', $exp[0]);
    $image = array_pop($exp);
    echo ($image);

我得到了Hosch Nok的工作答案,没有解码编码数据。 不打电话:

$decoded = urldecode($encoded);

但是直接使用
$encoded
变量。

回答得很好。我有一个问题,我如何才能得到照片扩展,因为它不会总是一个png?我问这个问题的原因是因为我要将它添加到文件名的末尾。图像名包含它。您可以再次使用explode(),如我的示例所示。请注意,给定的文件扩展名不必与文件的MIME类型匹配。我们如何将jquery数据保存在php变量中?@JaskaransinghRajal您必须使用ajax并设置POST或GET参数。请注意,即使我上载“jpg”,我仍然会收到编码为“data:image/png;base64”。如果是“jpg”,它不应该显示“data:image/jpg;base64”吗?看看这个很棒的库,你不会后悔的。它可以作为独立库使用,也可以作为Laravel包使用。完全可以推荐干预图像。下面答案中的代码不起作用(空白图像),但我一使用干预,它就工作得很好。接受的答案对我不起作用,但这个干预图像做得很好
    $encoded = "data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhE";
    $decoded = urldecode($encoded);
    $exp = explode(';', $decoded);
    $exp = explode(':', $exp[0]);
    $image = array_pop($exp);
    echo ($image);
$decoded = urldecode($encoded);