【问题标题】:Automatically rotate image when is width bigger than height in PHP当PHP中的宽度大于高度时自动旋转图像
【发布时间】:2014-01-26 10:30:17
【问题描述】:

我过去问过,但我不确定我们是否理解并且我仍然没有解决方案。 我需要优雅的解决方案; 我有 600x800 的照片,我需要在我的网站上显示它旋转 90 度,所以结果是,当我打印 php 页面时,所有照片都会自动垂直。

例如 我有很多照片,两种:800x600 和 600x800。 我需要在我的 php 页面上显示所有 800x600 的原始图像,并且所有 600x800 旋转 90 度。

我需要一些非常简单的解决方案,我完全疯了。一些可以旋转宽度大于高度的图像的功能。

非常感谢。

【问题讨论】:

  • 所以每张图片都有 2 个; 800x600 和 600x800?你现在的输出是多少?你能发布你的php逻辑吗?
  • 您可以在服务器上使用 GD 或 ImageMagick 或在浏览器中使用 CSS 转换来旋转图像。

标签: php css image image-rotation autorotate


【解决方案1】:

使用PHP函数getimagesize()你可以得到你的图片的widthheight

  list($width, $height) = getimagesize($imageUrl);

然后,在您的模板中:

  <?php if($width > $height): ?>
        //put css here as you want
  <?php else: ?>
        //put css here as you want
  <?php endif; ?>

【讨论】:

    【解决方案2】:

    使用getimagesize 检测图像宽度,如果宽度为 800px,则添加这样的内联 css

    style="-moz-transform: rotate(90deg); -webkit-transform: rotate(90deg); -o-transform: rotate(90deg); -ms-transform: rotate(90deg); transform: rotate(90deg);"
    

    【讨论】:

      【解决方案3】:

      getimagesize() 函数将为您提供宽度和高度,通过使用imagerotate(),您可以将该图像旋转到任何所需的角度。

      试试这个:-

      <?php
      // File and rotation
      $filename = 'php.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);
      ?>
      

      【讨论】:

      • 这不是正确的形式:--- echo(''); ---? :/ 不工作
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 2021-02-02
      • 2023-03-22
      • 1970-01-01
      • 2013-04-06
      • 2023-03-25
      相关资源
      最近更新 更多