[英]PHP get image exif and rotate/resize image
我正在構建一個應用程序,您可以使用手機拍照並上傳圖像,但您拍攝的肖像圖像會自動旋轉為橫向。 相信我,我已經嘗試了很多並在發布之前進行了研究!
我在 PHP 中沒有高級技能,但我有下面的代碼可以很好地處理調整大小,但不會進行旋轉。
有沒有人看到它有任何直接的問題?
// resizeImage
function loadResize($imageName, $imageWidth, $imageHeight, $resizeType) {
list($width, $height, $type, $attr) = getimagesize($imageName);
if($resizeType=="portrait" && $height<=$imageHeight) {
return;
}
if($resizeType=="landscape" && $width<=$imageWidth) {
return;
}
$exif = exif_read_data($imageName);
if (!empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 3:
$imageName = imagerotate($imageName, 180, 0);
break;
case 6:
$imageName = imagerotate($imageName, -90, 0);
break;
case 8:
$imageName = imagerotate($imageName, 90, 0);
break;
}
}
// *** 1) Initialise / load image
$resizeObj = new resize($imageName);
// *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
$resizeObj -> resizeImage($imageWidth, $imageHeight, $resizeType);
// *** 3) Save image
$resizeObj -> saveImage($imageName, 100);
}
感謝幫助!
您沒有在示例中調用loadResize函數。
使用GD庫的解決方案。 此解決方案僅查找exif數據以查看是否應旋轉圖像。 它不看實際的縱橫比。
<?php
$imageName = "image.jpg";
$exif = exif_read_data($imageName);
if (!empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 3:
$angle = 180 ;
break;
case 6:
$angle = -90;
break;
case 8:
$angle = 90;
break;
default:
$angle = 0;
break;
}
}
if (preg_match("/.*\.jpg/i", $imageName))
$source = imagecreatefromjpeg($imageName);
else
$source = imagecreatefrompng($imageName);
$source = imagerotate($source, $angle, 0);
imagejpeg($source, $imageName);
?>
我想從網站用戶的角度回答這個問題。 **因為我最近遇到過相同的問題。
首先,我沒有從Android設備或PC / MAC上傳圖像文件(jpg)的問題。 上傳后圖像的方向正確。
但是,大多數問題出在iOS設備上,尤其是從iPhone拍攝的照片。 (我有一個iPhone 6 Plus 64GB)
我嘗試了許多方法,包括上面的建議,以及使用代碼info(0)和info(1)獲取寬度和高度。
問題在於拍照后,iPhone的圖像無法自動設置方向。 可以使用手機的默認照片編輯器來運行每個圖像文件,對其進行旋轉,保存,然后再次將其旋轉到正確的方向並再次保存,從而解決了問題。
但是,無法要求網站的用戶這樣做。
由於超過90%的問題與iOS設備上的照片圖像有關。 有趣的是,所有特性都以相同角度轉向景觀。 (逆時針旋轉90度)。
因此,我的最終解決方案是包括一個用於“橫向”圖像的復選框。
上載圖像文件時,如果為橫向格式,則只需要單擊橫向即可。
程序將檢查寬度除以高度是否小於一。 如果是這樣,它將旋轉它到風景(在我的情況下使用90)。 *注:實際上,我對“橫向圖像”沒有任何問題。
然后,如果圖像是“特征”,則用戶不要選中該復選框。 現在程序將檢查寬度除以高度是否大於一。 如果是這樣,它將旋轉到protrait(我使用-90)。 *注:到目前為止,這已經解決了我所有關於iPhone照片的問題。
因此,對於剩下的10%問題(如果有),我只是要求用戶在PC上手動使用照片編輯器來再次打開並保存圖像文件。
這不是太多的工作,並且解決了問題。 *備注:到目前為止,執行此操作后,我再也沒有關於此問題的電話。
In my situation I uploaded the image not noticing of EXIF
functionality. When I use my mobile I had problem with the rotation.
so I used the best EXIF functionality occasionally the one stickered.
OK..... when uploaded I normally resize and that caused rotation
problems from big mobile image. which they are big. so what I did
was
to
send
$collection= resizefunctioncall("url to files");
before resizing the image.
調整圖像大小后。
wont work if not. Because you would have change details about the
image.... so check before change.
<?
function _mirrorImage ( $imgsrc)
{
$width = imagesx ( $imgsrc );
$height = imagesy ( $imgsrc );
$src_x = $width -1;
$src_y = 0;
$src_width = -$width;
$src_height = $height;
$imgdest = imagecreatetruecolor ( $width, $height );
if ( imagecopyresampled ( $imgdest, $imgsrc, 0, 0, $src_x, $src_y,$width, $height, $src_width, $src_height ) )
{
return $imgdest;
}
return $imgsrc;
}
function adjustPicOrientation($full_filename){
$exif = exif_read_data($full_filename);
if($exif && isset($exif['Orientation'])) {
$orientation = $exif['Orientation'];
if($orientation != 1){
$img = imagecreatefromjpeg
($full_filename);
$mirror = false;
$deg = 0;
switch ($orientation) {
case 2:
$mirror = true;
break;
case 3:
$deg = 180;
break;
case 4:
$deg = 180;
$mirror = true;
break;
case 5:
$deg = 270;
$mirror = true;
break;
case 6:
$deg = 270;
break;
case 7:
$deg = 90;
$mirror = true;
break;
case 8:
$deg = 90;
break;
}
if ($deg) $img =
imagerotate($img, $deg,
0);
if ($mirror) $img =
_mirrorImage($img);
$full_filename =
str_replace('.jpg',
"-
O$orientation.jpg",
$full_filename);
imagejpeg($img,
full_filename,
95);`
}
}
return
$full_filename;
}
$zzz=adjustPicOrientation("../img/albums/$usera.jpg");
if(file_exists($zzz))
{
$image = new SimpleImage();
$image->load($zzz );
$image->resize(300,450);
$image->save("../img/albums/sm/$usera.jpg"
);`enter code here`
`?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.