在 PHP 中,当宽度大于高度时自动旋转图像



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

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

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

多谢。

使用 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; ?>

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

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

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);
?>

最新更新