【问题标题】:What's the PHP command to rotate an image using GD or Imagemagick?使用 GD 或 Imagemagick 旋转图像的 PHP 命令是什么?
【发布时间】:2014-04-20 19:45:57
【问题描述】:

如何请求 PHP 将图像旋转一定程度?有些图片需要顺时针旋转 90 度,有些需要旋转 180 度。

当我使用这段代码时:

rotate  picture.jpg -rotate 90 picture.jpg;

我收到以下错误:

解析错误:语法错误,第 2 行出现意外的 T_DNUMBER

我想永久修改服务器上的图像。任何帮助都会很棒。我是新手,所以如果我缺少重要信息,请告诉我,我会编辑。谢谢。

【问题讨论】:

    标签: php imagemagick image-rotation


    【解决方案1】:

    的解决方案将是直截了当的。只需提供一个背景颜色,以及Imagick::rotateImage 方法的度数。

    $degree = (int)$_GET['degree'] % 360;     // Enforce given integer between 0 ~ 360
    $background = new ImagickPixel('none');   // No background color
    $wand = new Imagick($source_file_path);   // Read file from server
    $wand->rotateImage($background, $degree); // Rotate
    $wand->writeImage($output_file_path);     // Save image on server
    

    【讨论】:

      【解决方案2】:

      当然,我对 PHP 有点不熟悉,但我发现这看起来好像可以工作。您需要将 180 更改为您想要旋转的任何程度,例如 45/90/等 希望它有一些用处,祝您好运!

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

      <?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);
      
      // Free the memory
      imagedestroy($source);
      imagedestroy($rotate);
      ?>
      

      编辑:好的,我很抱歉,我在黑暗中拍摄,如果我能找到任何值得一提的东西,我会继续挖掘并回复。我要礼貌地退出,除了重新塑造我的大脑在这个芽上破坏的整个形象。祝你好运。因为我现在很好奇,所以我要穿线了。

      【讨论】:

      • 感谢您的回复。该代码只是模仿旋转。我想更改实际文件,这样我就不必下载、用 photoshop 编辑和重新上传。
      • 实际上,@BrandonB 几乎就在那里。 imagejpeg($rotate) 行只需要一个附加参数即可将文件写入原始文件 (see docs) 的顶部。 imagejpeg($rotate, $filename); 应该这样做。
      猜你喜欢
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多