[英]Cropping with Jcrop and PHP GD creates black JPEG
我正在使用Jcrop進行圖像上載/裁剪功能。 第一部分工作正常,但是實施Crop會引起頭痛。 我查看了 以下 問題 ,但似乎沒有一個問題與我的問題完全相關。
創建上載的表單:
<img src="uploads/<?=$image_name?>" id="crop-me">
<form action="complete.php" method="post">
<input type="hidden" name="image_name" value="<?=$image_name?>">
<input type="hidden" name="x" id="x">
<input type="hidden" name="y" id="y">
<input type="hidden" name="x2" id="x2">
<input type="hidden" name="y2" id="y2">
<input type="hidden" name="w" id="w">
<input type="hidden" name="h" id="h">
<div class="form-group text-right">
<button type="submit" class="btn btn-primary">Crop</button>
</div>
</form>
我的Jcrop JavaScript:
$(document).ready(function() {
$('#crop-me').Jcrop({
aspectRatio: 130 / 170, // 0.7647
minSize: [130, 170],
setSelect: [260, 340, 0, 0],
onChange: showCoords,
onSelect: showCoords
});
function showCoords(c) {
$("#x").val(c.x);
$("#y").val(c.y);
$("#x2").val(c.x2);
$("#y2").val(c.y2);
$("#w").val(c.w);
$("#h").val(c.h);
}
});
最后, complete.php
會生成圖像:
<?php
$targ_w = 130;
$targ_h = 170;
$jpeg_quality = 100;
$src = 'uploads' . $_POST['image_name'];
$img_r = imagecreatefromjpeg($src);
$dst_r = imagecreatetruecolor($targ_w, $targ_h);
imagecopyresampled($dst_r, $img_r, 0, 0, $_POST['x'], $_POST['y'], $targ_w, $targ_h, $_POST['w'], $_POST['h']);
header('Content-type: image/jpeg');
imagejpeg($dst_r, null, $jpeg_quality);
?>
我確實在complete.php
上獲得了130x170尺寸的圖像,但是它是完全黑色的。 我認為這可能與縱橫比的計算/未對准有關(我使用的代碼是Jcrop的教程/手冊的直接內容,但寬度和高度不同),但是我不確定。
有人可以看到我要去哪里了嗎?
注意:我已經檢查過,並且服務器肯定正在運行GD擴展v2.x。
正如@BloodyKnuckles
指出的@BloodyKnuckles
,此行中有一個錯字:
$src = 'uploads' . $_POST['image_name'];
在uploads
文件夾名稱的末尾缺少/
,應顯示為:
$src = 'uploads/' . $_POST['image_name'];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.