首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP或HTML或CSS图像缩放从原始最大高度100,但缩放宽度为自动;

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

Stack Overflow用户
提问于 2020-06-25 03:39:30
回答 1查看 23关注 0票数 0

使用PHP、HTML或CSS。

我有一个图像,假设是“1642x351”,我想让它自动缩放宽度但最大高度为100,

所以最终的图像是: 467 x 100;

我正在做的主要代码是部分PHP和部分CSS。

PHP:

代码语言:javascript
复制
<?php
$size = getimagesize($tmpfilename);
$imgwidth = $size[0];
$imgheight = $size[1];
$imgwidthhalf = $imgwidth/2;
?>

CSS:

代码语言:javascript
复制
<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;不会给出正确的比例。)

EN

回答 1

Stack Overflow用户

发布于 2020-06-25 04:35:46

找到了解决方案,也可以回答我自己的帖子。

代码语言:javascript
复制
<?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);
    
    ?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62562858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档