PHP 或 HTML 或 CSS 图像从原始最大高度 100 缩放,但将宽度缩放为自动;



Ether Using, PHP, HTML or CSS.

我有一个图像,比如说"1642 x 351",我希望它自动缩放宽度,但最大高度为 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;不会给出正确的比例。

找到解决方案不妨回答我自己的帖子。

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

?>