簡體   English   中英

PHP - 使用GD旋轉圖像會產生黑色邊框

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM