2016-09-16 2 views
-1

다음과 같은 PHP 코드를 사용하여 다양한 장치에서 이미지를 저장하고 있습니다. iPhone 이미지가 옆으로 나타나는 것을 제외하면 모두 잘 작동합니다. 이미지를 저장하기 전에 이미지를 회전하여 문제를 해결할 수있는 방법을 찾았습니다. 그러나 이미지를 업로드하면 내 웹 페이지와 파일 관리자에 표시되지 않고 옆으로 나타납니다. 회전 할 파일을 잘못 타겟팅합니까? 아니면 다른 것을 잘못 사용하고 있습니까?PHP 회전 이미지가 비어 있습니다.

$file = $_FILES["newsnap"]; 
    $id = $_SESSION['id']; 
    $username = $_SESSION['username']; 
    $aboutitems = nl2br(mysqli_real_escape_string($database, $_POST['about-snap'])); 
    $uploadloc = mkdir("../$username/"); 
    $image_temp = $_FILES["newsnap"]['tmp_name'];//Temporary location 
    $filename = mysqli_real_escape_string($database, htmlentities($file["name"])); 


      $sourcePath = $image_temp; // source path of the file 


      $exif = exif_read_data($sourcePath); 
      $orientation = $exif['Orientation']; 

      switch($orientation) 
       { 
        case 3: 
         $sourcePath = imagerotate($sourcePath, 180, 0); 
         break; 
        case 6: 
         $sourcePath = imagerotate($sourcePath, -90, 0); 
         break; 
        case 8: 
         $sourcePath = imagerotate($sourcePath, 90, 0); 
         break; 
       } 

      $targetPath = "../$username/$filename"; // Target path where file is to be stored 

      move_uploaded_file($sourcePath, $targetPath) ; // Moving Uploaded file 

      $added = date("y.m.d"); 

      mysqli_query($database, "INSERT INTO piqs(userid, chicpiq, aboutpic, added) VALUES('$id', '$targetPath', '$aboutitems', '$added')"); 

코드는 아래의 코드없이 완벽하게 잘 작동 : 다음은 내 코드입니다. 옆의 이미지를 회전시키기 위해 아래 코드를 추가했습니다.

  $exif = exif_read_data($sourcePath); 
      $orientation = $exif['Orientation']; 

      switch($orientation) 
       { 
        case 3: 
         $sourcePath = imagerotate($sourcePath, 180, 0); 
         break; 
        case 6: 
         $sourcePath = imagerotate($sourcePath, -90, 0); 
         break; 
        case 8: 
         $sourcePath = imagerotate($sourcePath, 90, 0); 
         break; 
       } 

감사합니다.

답변

1

내가 $sourcePath 파일 경로, 회전 할 수 없습니다에 대한 변수를 올바르게 볼 ...

http://php.net/manual/en/function.imagerotate.php를 참조하십시오, 당신은 사진의 열린 resourcere을 통과해야합니다. 그래서 이런 식으로해야만합니다.

$oldImage = ImageCreateFromJPEG($sourcePath); 
switch($orientation){ 
    case 3: 
     $newImage = imagerotate($oldImage, 180, 0); 
     break; 
    case 6: 
     $newImage = imagerotate($oldImage, -90, 0); 
     break; 
    case 8: 
     $newImage = imagerotate($oldImage, 90, 0); 
     break; 
    default: 
     $newImage = $oldImage; 
} 
imagejpeg($newImage, $targetPath, 90); 
+0

안녕하세요 Fabian. 이것을 지적 해 주셔서 감사합니다. 나는 이것을 지금 시험하고있다. 지금까지 나는 작은 변화를 볼 수 있지만 이미지는 iPhone과 데스크톱에서도 옆으로 표시됩니다 (이전에는 데스크톱에서는 옆으로 있지만 iPhone에서는 괜찮 았음). 몇 가지 시도를하고 코드를 업데이트 할 것입니다. 나중에 다시 볼 수 있을까요? 건배 남자 – davidb

+0

아니, 먼저'$ source = imagecreatefromjpeg ($ image_temp)'(var 이름에서 그 경로를 잊어 버리면 혼동 될 뿐이다)를 호출하면 "자원"을 반환 할 것이다. 그런 다음이 리소스를 순환 시키므로'$ imageRotated = imagerotate ($ source, -90, 0); '를 호출한다. 이 변수에는 회전 된 이미지 리소스가 있습니다. 'imagejpeg ($ imageRotated, $ targetPath, 90);'를 사용하면 새로운 이미지를'$ targetPath'에 쓸 것입니다 (이 값을 계산할 때 상관 없습니다). 이후에는 그래픽 처리를 통해 파일을 이미 복사했기 때문에 파일을 이동할 필요가 없습니다. –

관련 문제