如何使用PHP动态旋转帖子/页面中的缩略图?

如何使用PHP动态旋转帖子/页面中的缩略图?,php,wordpress,image,rotation,Php,Wordpress,Image,Rotation,在我的wordpress模板中,在循环中,我加载缩略图 如何在模板中使用php将其旋转90度 我希望避免手动执行此操作,因为有许多页面使用相同的模板 谢谢大家! <?php if (has_post_thumbnail( $post->ID ) ): ?> <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );

在我的wordpress模板中,在循环中,我加载缩略图

如何在模板中使用php将其旋转90度

我希望避免手动执行此操作,因为有许多页面使用相同的模板

谢谢大家!

<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
  <img class="img-fluid mx-auto d-none d-md-block" src="<?php echo $image[0]; ?>" />
<?php endif; ?>

我想出来了。我必须通过imgsrc标记调用php文件

<?php

function getImagePath(){
   return $_GET["img"];
}
// File and rotation
$filename = getImagePath();
$degrees = -90;

// Content type
header('Content-type: image/jpeg');
header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822,strtotime(" 2 day")));
// Load
$source = imagecreatefromjpeg($filename);

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

// Output
imagejpeg($rotate);

// Free the memory
imagedestroy($source);
imagedestroy($rotate);
?>

您可以对图像使用css

.your-image-class {
    transform: rotate(90deg);
}
或者,如果您确实需要在wordpress gallery中旋转图像,请使用下一个功能:

$image_editor = wp_get_image_editor( $path, $args );

我试过这样做,``但是输出是一个黑色的页面,有一个破损的图像``更多关于你具体想做什么,或者为什么需要旋转的详细信息会很有用。因为如果图像只需要旋转显示,您可能不需要php,您可以使用css转换:rotate90deg;并且只在需要旋转的帖子/区域应用这种样式。基本上,我使用页面缩略图作为酒瓶的产品图像图片。在较小的屏幕和分类页面上,我希望水平显示图像,而不是垂直显示。我知道我可以用css来做,但是这会给元素的定位带来一些困难。让源图像处于正确的方向会更干净。然而,我不确定它的效率有多高,如果可能的话