php图像旋转不起作用

时间:2014-02-16 15:33:54

标签: php image rotation gd

我有一个基于图像创建缩略图的脚本(使用crop_image),它正常工作。但是,脚本还应该根据EXIF信息正确旋转图像,并且由于某种原因,当我添加该部分时它总是失败,我不知道为什么。代码和文件路径与调整大小部分完全相同。

//THE FUNCTION USED BELOW
function correctImageOrientation($rotatedfile) {
if (function_exists('exif_read_data')) {
$exif = exif_read_data($rotatedfile);
if($exif && isset($exif['Orientation'])) {
  $orientation = $exif['Orientation'];
  if($orientation != 1){
    $img = imagecreatefromjpeg($rotatedfile);
    $deg = 0;
    switch ($orientation) {
      case 3:
        $deg = 180;
        break;
      case 6:
        $deg = 270;
        break;
      case 8:
        $deg = 90;
        break;
    }
    if ($deg) {
      $img = imagerotate($img, $deg, 0);        
    }
    // then rewrite the rotated image back to the disk as $rotatedfile
    imagejpeg($img, $rotatedfile, 100);
      }
    }
   }
  }

//THIS PART DOES -NOT- WORK
$adirname = session::value('userid');
$rotatedfile = '/home/path/www/uploads/'.$adirname.'/'.$file_name;  
correctImageOrientation($rotatedfile);
    copy($rotatedfile, '/home/path/www/uploads/'.$adirname.'/'.$file_name);

//THIS PART WORKS -OK-
if(!file_exists('/home/path/www/uplimg/'.$adirname.'/500_resized_'.$file_name)){

$adirname = session::value('userid');
list($width, $height, $type, $attr) = getimagesize('/home/path/www/uploads/'.$adirname.'/'.$file_name);
$prop = $width / $height;
$newheight = round(500 / $prop);
crop_image('/home/path/www/uploads/'.$adirname.'/'.$file_name,'/home/path/www/uploads/'.$adirname.'/500_resized'.$file_name,500,$newheight);

}

1 个答案:

答案 0 :(得分:0)

<link rel="stylesheet" type="text/css" href="bootstrap-3.2.0-dist/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="bootstrap-3.2.0-dist/css/bootstrap.min.css">
<script src="js/bootstrap.js"></script>

<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="rotate" value="rotate" />

</form>
<?php

if(isset($_POST['rotate']))
{
    $image=$_FILES['image']['name'];
    $tmp_name=$_FILES["image"]["tmp_name"];
    $location='image/';

    echo $image;
    move_uploaded_file($tmp_name,$location.$image);
    $imagename = "image/".$image;
    $source= imagecreatefromjpeg($imagename);
    $rotate=imagerotate($source,75,0);
    imagejpeg($rotate,"rotated/$image");

}

if(isset($_FILES['image']['name']))
{
?>
<html>
<table>
<tr><td>UploadImage</td>
<td height="500px" width="500px" align="center">
<img src="image/<?php echo $image;?>" width="300px" height="300px" />

</td>
<td>RotateImage</td>
<td headers="500px" width="500px" align="center"><img src="rotated/<?php echo $image?>" width="300px" height="300px" /></td></tr>
</table>
</html>
<?php
}
?>