2014-09-10 28 views
0

试图解决从IOS设备上传图像的问题,exif方向会导致它们有时会被旋转。imagerotate使用move_uploaded_file时

我发现了很多使用imagerotate的片段来解决这个问题,但是试图实现它们。

对于我使用的是图像的保存:

$moveUploadedFile = move_uploaded_file($fileToUpload["tmp_name"], $this->uploadDir . "/" . $newFileName); 

(从防弹图像上传类拍摄)

我没事用做switch语句来检查的EXIF数据,但不能使move_uploaded_file的工作。

我已经试过(测试)e.g:

$image = $fileToUpload["tmp_name"]; 
$image = imagerotate($image, 90, 0); 
$moveUploadedFile = move_uploaded_file($image, $this->uploadDir . "/" . $newFileName); 

这是给我的move_uploaded_file请求字符串的错误,但接收的资源来代替。

任何帮助?

回答

0

在这里你去先生

public function moveUploadedFile($tmp_name, $destination) 
    { 
     if(move_uploaded_file($tmp_name, $destination)){ 
      $this->image_rotation(); 
      return true; 
     } 
} 
    public function image_rotation(){ 
     /* Check if the image is rotated, 
     * and if it's rotates. Fix it! 
     */ 
     // $filename = __DIR__ . '/'.$this->location .'/'. $this->name . '.' . $this->mime; 
     $filename = $this ->fullPath; 

     $exif = exif_read_data($filename); 
     if (isset($exif['Orientation'])) 
     { 
      switch ($exif['Orientation']) 
      { 
      case 3: 
      // Need to rotate 180 deg 
        $degrees = 180; 
        break ; 

      case 6: 
       // Need to rotate 90 deg clockwise 
        $degrees = -90; 
        break ; 

      case 8: 
       // Need to rotate 90 deg counter clockwise 
        $degrees = 90; 
        break ; 
      } 

      if (preg_match("/jpg|jpeg/", pathinfo($filename, PATHINFO_EXTENSION))) 
      { 
       $image_source = imagecreatefromjpeg($filename); 
       $rotate = imagerotate($image_source, $degrees, 0); 
       imagejpeg($rotate, $filename, 100); 

      } 
      if (preg_match("/png/", pathinfo($filename, PATHINFO_EXTENSION))) 
      { 
       $image_source = imagecreatefrompng($filename); 
       $rotate = imagerotate($image_source, $degrees, 0); 
       imagepng($rotate, $filename, 100); 
      } 
      if (preg_match("/gif/", pathinfo($filename, PATHINFO_EXTENSION))) 
      { 
       $image_source = imagecreatefromgif($filename); 
       $rotate = imagerotate($image_source, $degrees, 0); 
       imagepng($rotate, $filename, 100); 
      } 

      imagedestroy($image_source); //free up the memory 
      imagedestroy($rotate); //free up the memory 

     } 
} 
0

$图像是一种资源。但是,move_uploaded_file采用上传文件的文件名以及目标文件夹(http://php.net/manual/en/function.move-uploaded-file.php

您有一个要保存的资源,而不是特定路径上的上传文件。

使用imagejpeg($ image,$ destination)或imagepng($ image,$ destination),取决于您想要将其存储为。

希望这会有所帮助!