我发现了一个很棒的图像裁剪工具,可用于我的应用程序。 http://tympanus.net/codrops/2014/10/30/resizing-cropping-images-canvas/无法将图像画布保存到使用AJAX/PHP的文件
工作完美,作物就像我想要的,但我无法弄清楚如何通过PHP/AJAX保存裁剪文件。虽然有涉及很多文件,将3修改为:
index.php (Cropper Tool)
savefile.php
js/component.js (the main functions)
在上面的链接文章的评论,有人改变了JS一个AJAX调用发送到PHP文件,以“拯救”裁剪后的图像。我无法让它工作。
下面的代码,我的修改component.js
crop = function(){
//Find the part of the image that is inside the crop box
var crop_canvas,
left = $('.overlay').offset().left - $container.offset().left,
top = $('.overlay').offset().top - $container.offset().top,
width = $('.overlay').width(),
height = $('.overlay').height();
crop_canvas = document.createElement('canvas');
crop_canvas.width = width;
crop_canvas.height = height;
crop_canvas.getContext('2d').drawImage(image_target, left, top, width, height, 0, 0, width, height);
//=== set our canvas data
var canvasData = crop_canvas.toDataURL('image/png');
//=== call ajax to fire php save function
var ajax = new XMLHttpRequest();
ajax.open('POST','savefile.php',true);
ajax.setRequestHeader('Content-Type', 'application/upload');
ajax.send(canvasData);
//=== displays image in new window to prove its working
window.open(crop_canvas.toDataURL('image/png'));
}
接下来的部分是savefile.php。由于没有任何东西可以保存,所以我永远无法分辨这是否真的在开火。
<?php
$imageData = $_POST['data'];
//==== replace with dynamic names later ===
$imgFile = “test.jpg”;
if (!empty($imageData)) {
// Remove the headers (data:,) part.
// A real application should use them according to needs such as to check image type
$filteredData=substr($imageData, strpos($imageData, “,”)+1);
// Need to decode before saving since the data we received is already base64 encoded
$unencodedData=base64_decode($filteredData);
// Save file.
$fp = fopen('$imgFile', ‘wb’);
fwrite($fp, $unencodedData);
fclose($fp);
}
header (‘Location: index.php’);
?>
洞察力非常感谢 - 花了整个星期五晚上试图调试无济于事!
请检查我的答案。 –