[英]PHP - Rotate image with GD gives black borders
我正在嘗試旋轉並保存圖像。 輪換基於EXIF數據。 我嘗試了以下內容,它們都圍繞着它呈現黑色邊框:
原件看起來像這樣:
$orientation = array_values([0, 0, 0, 180, 0, 0, -90, 0, 90])[@exif_read_data($imagePath)['Orientation'] ?: 0];
$source = imagecreatefromjpeg($imagePath);
$resource = imagerotate($source, $orientation, 0);
imagejpeg($resource, $image, 100);
我也試過添加imagealphablending($resource, true);
和imagesavealpha($resource, true);
在使用PHP旋轉圖像時在黑色背景中提出,但無濟於事; 邊界仍然存在。
然后我嘗試使用imagecreatetruecolor()
創建圖像:
$imageSizes = getimagesize($image);
$oldWidth = $imageSizes[0];
$oldHeight = $imageSizes[1];
$orientation = array_values([0, 0, 0, 180, 0, 0, -90, 0, 90])[@exif_read_data($image)['Orientation'] ?: 0];
$source = imagecreatefromjpeg($imagePath);
$resource = imagerotate($source, $orientation, 0);
$newWidth = $oldWidth;
$newHeight = $oldHeight;
if ($orientation !== 180 && $orientation !== 0) {
$newWidth = $oldHeight;
$newHeight = $oldWidth;
}
$imageResized = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled ($imageResized, $resource, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight);
imagejpeg($imageResized, $image, 100);
但我似乎無法讓它發揮作用。 有人能幫我這個嗎?
我今天在PHP for Windows中發現了這個問題。 當你進行0度或360度旋轉時,邊框似乎只會被添加。 我沒有180度旋轉的邊框。 因此,只需檢查方向是否為非零,並且僅在必要時旋轉。
... if ($orientation !== 0) $resource = imagerotate($source, $orientation, 0); else $resource = $source; end ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.