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



在我的WordPress模板中,在LOOP中,我正在加载缩略图。

如何在模板中使用 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; ?>

你可以对你的图像使用 css

.your-image-class {
transform: rotate(90deg);
}

或者,如果您真的需要在wordpress库中旋转图像,请使用下一个功能:

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

我想通了。我不得不通过 img src 标签调用 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);
?>

最新更新