我想旋轉並保存圖像。旋轉是基於EXIF數據。我曾嘗試以下,而這一切給黑色邊框周圍:PHP - 用GD旋轉圖像產生黑色邊框
凡原來看起來像這樣:
$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);
建議在Black background when rotating image with 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);
但我似乎無法得到它的工作。有人能幫助我嗎?
謝謝您的測試。這隻能意味着我正在工作的應用程序會改變其他地方的圖像。我會試着找出在哪裏,但至少我知道這個代碼正在工作:-) –