使用PHP、HTML或CSS。
我有一个图像,假设是“1642x351”,我想让它自动缩放宽度但最大高度为100,
所以最终的图像是: 467 x 100;
我正在做的主要代码是部分PHP和部分CSS。
PHP:
<?php
$size = getimagesize($tmpfilename);
$imgwidth = $size[0];
$imgheight = $size[1];
$imgwidthhalf = $imgwidth/2;
?>CSS:
<style>
.widget-4 .card-profile-img {
position: absolute;
text-align: center;
pointer-events: none;
left: 50%;
top: 25px;
margin-left: <?= $imgwidthhalf; ?>px;
}
</style>(注意:是的,我知道$imgwidthhalf = $imgwidth/2;不会给出正确的比例。)
发布于 2020-06-25 04:35:46
找到了解决方案,也可以回答我自己的帖子。
<?php
$size = getimagesize($tmpfilename);
$imgWidth = $size[0]; // Get current Width of image.
$imgHeight = $size[1]; // Get current Height of image.
$aspect = ($imgWidth / $imgHeight); // Get Aspect Ratio, Width / Height.
// Optional set new Height or New Width.
$setNewHeight = 100;
$setNewWidth = 200;
// To get Width of image to be auto aspect ratio of a new Height:
$newWidth = ($setNewHeight * $aspectRatio);
// To get Height of image to be auto aspect ratio of a new Width:
$newHeight = ($setNewWidth / $aspectRatio);
?>https://stackoverflow.com/questions/62562858
复制相似问题