2012-07-12 35 views
3

可能重複:
php resizing image on upload rotates the image when i don't want it to爲什麼我的圖像在PHP中調整上傳圖像大小時會旋轉?

我已經創建了我的第一次上傳代碼,並測試它,圖像大小調整和上傳的罰款。我唯一的問題是它旋轉。

下面是代碼:

$errors = array(); 
$message = ""; 

if(isset($_POST["test"])){ 

$name    = rand(1,999999) . $_FILES["uploadfile"]["name"]; 
$temp_name   = $_FILES["uploadfile"]["tmp_name"]; 
$size    = $_FILES["uploadfile"]["size"]; 
$extension   = strtolower(end(explode('.', $_FILES['uploadfile']['name']))); 
$path    = "testupload/" . $name; 

$info    = getimagesize($temp_name); 
$originalwidth  = $info[0]; 
$originalheight  = $info[1]; 
$mime    = $info["mime"]; 

$acceptedHeight  = 750; 
$acceptedWidth  = 0; 

$acceptedMimes = array('image/jpeg','image/png','image/gif'); 
$acceptedfileSize = 4102314; 
$acceptedExtensions = array('jpg','jpeg','gif','png'); 
echo $size; 
// check mimetype 
if(!in_array($mime, $acceptedMimes)){$errors[] = "mime type not allowed - The file you have just uploaded was a: " . $extension . " file!";} 
if(!$errors){if($size > $acceptedfileSize){$errors[] = "filesize is to big - Your file size of this file is: " . $size;}} 
if(!$errors){if(!in_array($extension, $acceptedExtensions)){$errors[] = "File extension not allowed - The file you have just uploaded was a: " . $extension . " file!";}} 

if(!$errors){ 

    // create the image from the temp file. 
    if ($extension === 'png'){ 
     $orig = imagecreatefrompng($temp_name); 
    }elseif ($extension === 'jpeg'){ 
     $orig = imagecreatefromjpeg($temp_name); 
    }elseif ($extension === 'jpg'){ 
     $orig = imagecreatefromjpeg($temp_name); 
    }elseif ($extension === 'gif'){ 
     $orig = imagecreatefromgif($temp_name); 
    } 

    // work out the new dimensions. 
    if ($acceptedHeight === 0){ 
     $newWidth = $acceptedWidth; 
     $newHeight = ($originalheight/$originalwidth) * $acceptedWidth; 
    }else if ($acceptedWidth === 0){ 
     $newWidth = ($originalwidth/$originalheight) * $acceptedHeight; 
     $newHeight = $acceptedHeight; 
    }else{ 
     $newWidth = $acceptedWidth; 
     $newHeight = $acceptedHeight; 
    } 

    $originalwidth = imagesx($orig); 
    $originalheight = imagesy($orig); 


    // make ssure they are valid. 
    if ($newWidth < 1){ $newWidth = 1; }else{ $newWidth = round($newWidth); } 
    if ($newHeight < 1){ $newHeight = 1; }else{ $newHeight = round($newHeight); } 

    // don't bother copying the image if its alreay the right size. 
    if ($originalwidth!== $newWidth || $originalheight !== $newHeight){ 

     // create a new image and copy the origional on to it at the new size. 
     $new = imagecreatetruecolor($newWidth, $newHeight); 
     imagecopyresampled($new, $orig, 0,0,0,0, $newWidth, $newHeight, $originalwidth, $originalheight); 

    }else{ 

     // phps copy on write means this won't cause any harm. 
     $new = $orig; 
    } 

    // save the image. 
    if ($extension === 'jpeg' || $extension === 'jpg'){ 
     imagejpeg($new, $path, 100); 
    }else if ($save_ext === 'gif'){ 
     imagegif($new, $path); 
    }else{ 
     imagepng($new, $path, round(9 - (100/(100/9)))); 
    } 

    $message = $path; 

可有人請讓我知道是怎麼回事?

+0

它接縫是所有高度大於寬度的圖像,它旋轉圖像,我如何防止這種情況發生? – Robert 2012-07-12 15:01:49

+0

我在代碼中看不到任何明顯的東西。在每一步'var_dump(getimagesize($ new_image))'之後,找出它正在改變的位置。 – tavocado 2012-07-12 15:20:29

回答

1

檢查原始圖像實際上是您期望的方向。我整天使用圖像,大部分時間是Windows照片瀏覽器以某種方向顯示圖像(讀取EXIF中的方向更改),但如果在Photoshop中打開它,則會有所不同。

+0

上傳的圖片原本是另一種方式,現在我測試了幾張圖片,所有高度大於寬度的圖片都旋轉。 – Robert 2012-07-12 14:57:38

0

我用兩個圖像測試了你的代碼:一個是橫向(寬度>高度),另一個是縱向(高度>寬度)。代碼起作用。

這個問題似乎是@Tavocado所說的:較新的相機有一個傳感器來檢測拍照時相機的方向,並將這些信息存儲在照片中。此外,較新的照片查看軟件從照片中讀取該信息並在顯示圖像之前將其旋轉。所以你總是看到正確的方向(天空,地球下)的形象。但是PHP函數(以及世界其他地方)不會使用這些信息並按照拍攝的內容顯示圖像。這意味着你將不得不旋轉自己的肖像圖像。

只需在瀏覽器中加載圖像(拖動地址欄上的文件)即可看到圖像如何真正存儲在文件中,無需任何自動旋轉。

0

問題是圖像嵌入了EXIF數據,可能來自拍攝照片的設備。

調查您的圖像是否已嵌入旋轉信息,使用exif_read_data,然後使用function like this自動糾正旋轉。