gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-29 00:12:11 24 4
gpt4 key购买 nike

所以我目前找到了这个名为cropit的照片裁剪插件。 .演示是 here .所以我想做的是抓取裁剪后的照片并将照片的名称上传到 mysql 数据库并使用 php 将其保存到目录中。

到目前为止我有这个:

HTML:

<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 :

    $('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 设置代码,因为当我裁剪照片并选择提交时,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);

<强>2。获取base64编码图片的文件名

    $encoded = "data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhE";
$decoded = urldecode($encoded);
$exp = explode(';', $decoded);
$exp = explode(':', $exp[0]);
$image = array_pop($exp);
echo ($image);

关于javascript - 如何使用带有 php 的 cropit jquery 插件裁剪和上传照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28085848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com