編輯爲什麼這個基本的imagejpeg()調整器會返回黑色圖像?
感謝所有的答案,尤其是@Mailerdaimon誰發現我不是在imagecopyresampled
功能使用的計算值。
我不再得到黑色圖像,但我仍然得到一些黑色部分,所以我想我的比例公式應該更新:如果我上傳風景圖像,新圖像的高度小於170px,並且然後有一些黑色的表現。
我怎樣才能確保圖像的高度一路走來?
下面是一個簡單的腳本允許用戶上傳圖片。上傳完成後,圖片將顯示爲170px(h)x 150px(w)的縮略圖。
容量調整部分不工作因爲輸出圖像是170x150px,但我仍然得到,如果
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$maxWidth = 150;
$maxHeight = 170;
$name = $_FILES ['image'] ['name'];
$type = $_FILES ["image"] ["type"];
$size = $_FILES ["image"] ["size"];
$tmp_name = $_FILES ['image'] ['tmp_name'];
list($originalWidth, $originalHeight) = getimagesize($tmp_name);
if ($originalWidth > $originalHeight)
{
$thumbnail_height = floor(($originalHeight/$originalWidth)*$maxWidth);
$thumbnail_width = $maxWidth;
} else {
$thumbnail_width = floor(($originalWidth/$originalHeight)*$maxHeight);
$thumbnail_height = $maxHeight;
}
// Resample
$image_p = imagecreatetruecolor($maxWidth, $maxHeight);
imagecreatefrompng($tmp_name);
imagecopyresampled($image_p, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height,
$originalWidth, $originalHeight);
//start upload process
$RandomNumber = uniqid();
$location = "uploads/$RandomNumber";
imagejpeg($image_p, $location, 100);
$sql=query("UPDATE users SET image = '".$location."' WHERE id = '$id'");
}
}
任何想法,我做錯了一些黑色的區域?
你確定上傳的圖片是PNG文件嗎? (mime image/png)? –
@ mly0是的,我只上傳PNG –
您是否找到了解決方案? – Mailerdaimon