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

时间:2014-01-08 11:01:11

标签: php css image image-rotation autorotate

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

e.g。 我有很多照片,有两种:800x600& 600×800。 我需要在我的php页面上显示所有这些都是原始的800x600,并且所有600x800都旋转了90度。

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

非常感谢。

3 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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);
?>
相关问题