私は画像をトリミングし、それをtoDataURL()iOSデバイスから白紙イメージを返す
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'),
};
};
を保存するには、次のコードを使用しますが、私は画像に変換して、サーバーにアップロードするとき。アップロードされた画像は空白です。このため
私は細かい作業PCのすべてのものから、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";
}
?>
からコードを以下の使用しますが、私は任意のモバイル機器から画像をアップロードする際の画像は空白です。私はそれを最大サイズにズームするときにうまく動作します。
私はどこが間違っているか教えていただけますか?
ソースの矩形の計算が正しくないか、またはiOSがこの問題とdrawImageに問題があるように思われるため、追加のクランプが必要になると思います。問題がなくなるかどうかをテストするために、既知の有効なサイズを使用してください。 – K3N
はい、「モバイルデバイス」がiosだけの場合、[this](http://stackoverflow.com/questions/35500999/cropping-with-drawimage-not-working-in-safari)が役立ちます。問題は 'toDataURL'ではなくdrawImageから来るので、キャンバスをアップロードするのではなく、ドキュメントに追加することもできます。 – Kaiido
はい、iOSとdrawImageの問題です。 –