【发布时间】:2016-08-07 20:39:54
【问题描述】:
我使用以下代码裁剪图像并保存
this.crop = function(width, height, type) {
var imgInfo = this.imgInfo,
c = this.eles.container,
img = this.eles.img,
original = new Image(),
canvas = document.createElement('canvas');
original.src = img.attr('src');
canvas.width = width;
canvas.height = height;
var w = Math.round(c.width() * (imgInfo.aw / (imgInfo.w * imgInfo.s))),
h = Math.round(c.height() * (imgInfo.ah / (imgInfo.h * imgInfo.s))),
x = Math.round(-parseInt(img.css('left')) * (imgInfo.aw / (imgInfo.w * imgInfo.s))),
y = Math.round(-parseInt(img.css('top')) * (imgInfo.ah / (imgInfo.h * imgInfo.s)));
canvas.getContext('2d').drawImage(original, x, y, w, h, 0, 0, width, height);
return {
width: width,
height: height,
type: type || 'jpeg',
string: canvas.toDataURL("image/" + type || 'jpeg'),
};
};
但是当我将其转换为图像然后上传到服务器时。上传的图片是空白的。
为此,我使用 save.php 文件中的以下代码
<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
define('UPLOAD_DIR', 'img/catalog/');
$base64 = base64_decode(preg_replace('#^data:image/[^;]+;base64,#', '', $_POST['string']));
$stamp = imagecreatefrompng('img/water.png');
$im = imagecreatefromstring($base64);
ob_start();
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
$file = UPLOAD_DIR . uniqid() . '.jpg';
header('Content-type: image/');
$success = imagejpeg($im, $file, 100);
imagedestroy($im);
echo "$file";
}
?>
从 PC 一切正常,但是当我从任何移动设备上传图像时,图像是空白的。当我将它放大到最大尺寸时,一切正常。
你能告诉我哪里错了吗?
【问题讨论】:
-
我猜源矩形计算不正确或需要额外的钳位,因为 iOS 似乎对此和 drawImage 有问题。尝试使用固定的已知有效尺寸进行测试,看看问题是否消失。
-
是的,如果“任何移动设备”只是 ios 设备,this 可能会有所帮助。您也可以尝试将画布附加到文档中而不是上传它,因为问题来自
drawImage,而不是来自toDataURL。 -
是的,iOS 和 drawImage 的问题。
标签: php jquery ios html5-canvas