2014-05-02 45 views
1

所以我創建了一個可以將圖片上傳到頁面的響應式網站。 PHP腳本基本上調整了圖像的大小,並將一個拇指文件路徑存儲在數據庫中。原始圖像和拇指圖像也存儲在網站文件夾中。我正在使用GD庫。這是新的。無法從智能手機上傳照片?

無論如何,我只是在做一個從我的iphone上傳照片到網站的測試。它確實上傳圖像。但是它有兩個問題。

  1. 上傳照片需要太長的時間。
  2. 該照片在完成上傳後側向定位。如果照片是縱向照片,則會上傳爲橫向照片。奇怪的。

任何人都可以幫我解決這兩個問題嗎?

更新代碼

if (isset($_FILES['image'])) { 

    if (empty($_FILES['image']['name'])) { 

      ?><div class="add-errors">Please choose an image!</div><?php 

    } else { 


     function getOrientedImage($imagePath){ 
      $image = imagecreatefromstring(file_get_contents($imagePath)); 
      $exif = exif_read_data($imagePath); 
      if(!empty($exif['Orientation'])) { 
       switch($exif['Orientation']) { 
        case 8: 
         $image = imagerotate($image,90,0); 
         break; 
        case 3: 
         $image = imagerotate($image,180,0); 
         break; 
        case 6: 
         $image = imagerotate($image,-90,0); 
         break; 
       } 
      } 
      return $image; 
     } 

     $name  = $_FILES['image']['name']; 
     $temp  = $_FILES['image']['tmp_name']; 
     $type  = $_FILES['image']['type']; 
     $size  = $_FILES['image']['size']; 
     $ext  = strtolower(end(explode('.', $name))); 
     $size2  = getimagesize($temp); 
     $width  = $size2[0]; 
     $height  = $size2[1]; 
     $upload  = md5(rand(0, 1000) . rand(0, 1000) . rand(0, 1000) . rand(0, 1000)); 

     // Restrictions for uploading 
     $maxwidth = 6000; 
     $maxheight = 6000; 
     $allowed = array('image/jpeg', 'image/jpg', 'image/png', 'image/gif'); 

     // Recognizing the extension 
     switch($type){ 

      // Image/Jpeg 
      case 'image/jpeg': 
        $ext= '.jpeg'; 
      break; 

      // Image/Jpg 
      case 'image/jpg': 
        $ext= '.jpg'; 
      break; 

      // Image/png 
      case 'image/png': 
        $ext= '.png'; 
      break; 

      // Image/gif 
      case 'image/gif': 
        $ext= '.gif'; 
      break; 
     } 

     // upload variables 
     $path   = $userDir . $upload . $ext; 
     $thumb_path  = $userDir . 'thumb_' . $upload . $ext; 

     // check if extension is allowed. 
     if (in_array($type, $allowed)) { 

      // Checking if the resolution is FULLHD or under this resolution. 
      if ($width <= $maxwidth && $height <= $maxheight) { 
       if ($size <= 5242880) { 

        // check the shape of the image 
        if ($width == $height) {$shape = 1;} 
        if ($width > $height) {$shape = 2;} 
        if ($width < $height) {$shape = 2;} 

        //Adjusting the resize script on shape. 
        switch($shape) { 

         // Code to resize a square image. 
         case 1: 
          $newwidth =  690; 
          $newheight = 690; 
         break; 

         // Code to resize a tall image 
         case 2: 
          $newwidth = 690; 
          $ratio  = $newwidth/$width; 
          $newheight = round($height * $ratio); 

         break; 

        } 

        // Resizing according to extension. 
        switch ($type) { 

         // Image/Jpeg 
         case 'image/jpeg'; 
          $img =  getOrientedImage($temp); 
          $thumb = imagecreatetruecolor($newwidth, $newheight); 
             imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
             imagejpeg($thumb, $thumb_path); 
         break; 

         // Image/Jpg  
         case 'image/jpg'; 
          $img =  getOrientedImage($temp); 
          $thumb = imagecreatetruecolor($newwidth, $newheight); 
             imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
             imagejpeg($thumb, $thumb_path); 
         break; 

         // Image/png  
         case 'image/png'; 
          $img =  getOrientedImage($temp); 
          $thumb = imagecreatetruecolor($newwidth, $newheight); 
             imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
             imagepng($thumb, $thumb_path); 
         break; 

         // Image/gif  
         case 'image/gif'; 
          $img =  getOrientedImage($temp); 
          $thumb = imagecreatetruecolor($newwidth, $newheight); 
             imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
             imagegif($thumb, $thumb_path); 
         break; 
        } 


         // Move the original file aswell. 
         move_uploaded_file($temp, $path); 


       } else { 
        ?><div class="add-errors">Your image size is too big!</div><?php 
       } 
      } else { 
       ?><div class="add-errors">Your image resolution exceeds the limit!</div><?php 
      } 

     } else { 
      ?><div class="add-errors">Your have uploaded a forbidden extension!</div><?php 

     } 

    } 

} 
+0

這可能已經超出了原始簡單問題的範圍。我建議把問題帶到論壇。 – SuperJer

回答

4

您可能需要查看exif_read_data()函數,然後查看數組中返回的['Orientation']值。通常情況下,具有方向傳感器的手機或相機以一個方向或另一個方向存儲圖像,然後向圖片中的exif數據添加適當的方向標誌。然後由圖像查看器或圖像處理器決定在顯示或處理圖像之前是否旋轉原始圖像。

該頁面的評論中有一些很棒的例子。

功能從該網頁上的一個例子建:

<?php 
    function getOrientedImage($imagePath){ 
     $image = imagecreatefromstring(file_get_contents($imagePath)); 
     $exif = exif_read_data($imagePath); 
     if(!empty($exif['Orientation'])) { 
      switch($exif['Orientation']) { 
       case 8: 
        $image = imagerotate($image,90,0); 
        break; 
       case 3: 
        $image = imagerotate($image,180,0); 
        break; 
       case 6: 
        $image = imagerotate($image,-90,0); 
        break; 
      } 
     } 
     return $image; 
    } 
?> 

此外,關於上傳時,如果該設備是使用手機信號塔傳輸數據,上傳速度大概的分數你下載速度。爲了進行比較,大多數社交網絡應用程序在上傳前調整圖像大小,而您的網頁可能不會。由於手機需要800萬像素或更高的照片,因此需要大量的數據。

+0

太好了。我現在正在看這些例子。 – utooall

+0

由於我的網站不是應用程序,因此我認爲無法事先調整它的大小。 – utooall

+0

我認爲這可能與一個JavaScript庫,雖然我不能肯定地說。 – SuperJer

0

1)不知道是否有什麼可以做的是,你上傳的圖片中選擇的用戶。在應用程序中,我可能會在傳輸之前縮小圖像尺寸。

2)無論您正在查看照片(或生成縮略圖),您可能都不會考慮照片中的方向標誌。

+0

典型的iphone圖像在3mb到4mb之間。難怪上傳需要一段時間。不幸的是,在上傳之前無法縮小它的尺寸。如果只有它可以選擇一個較小的版本,比如發送照片時的方式。 – utooall