2012-09-22 48 views
3

我希望用户能够顺时针或逆时针旋转图像文件。我已经尝试imagejpeg($ rotate),但似乎无法使其正确工作。PHP永久旋转图像文件

  <form method="GET" action="rotate.php"> 
       Rotate:<input type="radio" name="rotate" value="clockwise">Clockwise 
       <input type="radio" name="rotate" value="counterclockwise">Counter clockwise 
       <input type="Submit" name="Submit1"/> 
      </form> 

我想让用户能够选择单选按钮的方向,然后单击“提交”。然后显示的图像将更新旋转到他们选择的任何方向,并且每当再次使用时永久保持该方式。任何帮助或方向?

<img src=\"uploads/$user/$folder/$image\"/></a>"; 
+0

这个'“;'不列入使所提供的代码任何意义,你..please提供如何阅读PHP的更多相关信息 –

+1

手册... [链接](http://php.net/manual/en/function.imagerotate.php) – markz

+1

这看起来像一个PHP字符串,@Registered User。 – Ohgodwhy

回答

3

使用imagerotate()这将永久旋转图像。

<?php 
// File and rotation 
$filename = 'test.jpg'; 
$degrees = 180; 

// Content type 
header('Content-type: image/jpeg'); 

// Load 
$source = imagecreatefromjpeg($filename); 

// Rotate 
$rotate = imagerotate($source, $degrees, 0); 

// Output 
imagejpeg($rotate); 
?> 

http://php.net/manual/en/function.imagerotate.php

,或者如果你想旋转你的图片永久,HTML和CSS是不是要走的路,你可以使用这个使用jQuery

http://www.linein.org/examples/jquery_rotate/

+0

永久性的,我想他的意思是用旧的图像覆盖旧图像这个代码没有做,但它开始:) –

0

。您将需要一些服务器端脚本来存储旋转的图片。你可以看看GD library。对于这种图像处理,我一直在使用Imagine library,这是一个基于GD的库。

0

如果有其他人需要这个。这里是将替换原有的旋转图像的功能:

public function autoRatateImage($src, $exifCode = ''){ 

     if($exifCode == ''){ 
      $exif = exif_read_data($src); 
     }else{ 
      $exif['Orientation'] = $exifCode; 
     } 
     $source = imagecreatefromjpeg($src); 
     if (!empty($exif['Orientation'])) { 
      switch ($exif['Orientation']) { 
       case 3: 
        $image = imagerotate($source, 180, 0); 
        break; 

       case 6: 
        $image = imagerotate($source, -90, 0); 
        break; 

       case 8: 
        $image = imagerotate($source, 90, 0); 
        break; 
      } 
      if (file_exists($src)) { 
       unlink($src); 
      } 
      imagejpeg($image, $src , 100); 
     } 

    }