douganbi7686 2011-07-25 02:48
浏览 12
已采纳

我可以调整图像大小以适应桌子的宽度吗?

I'm using a BB code function to allow members to post images and such in comments and topics. My problem is if an image is too big to fit the table, it expands everything past the width. Are there any workarounds for this? Either in the table or the td? A predefined width and height for the image won't work because I'll never know how big each image is. Is there a way to make a max width/height for the image or something along those lines?

  • 写回答

2条回答 默认 最新

  • duanqing3026 2011-07-25 02:52
    关注

    Here's a bit of code you can run to force a maximum width if the images are being stored on your server:

    $maxWidth = 450;
    $info = getimagesize("../path/to/image.jpg");
    if( $info[0] > $maxWidth )
    {
        print '<img src="../path/to/image.jpg" width="' . $maxWidth . '" />';
    }
    else
    {
        print '<img src="../path/to/image.jpg" />';
    }
    

    The height will scale to the width, so nothing gets too stretched out.

    Edit:

    1. If you want to actually resize the images on your server, I have a PHP library that might be of help to you, found on my github, here.
    2. If you want to do it all clientside, without storing the image locally, you should be able to create a new Javascript Image(), set the src to the image and then determine the width and height of the image through Javascript's built-in methods. You could then dynamically alter the width/height of the <img> element to be no larger than your pre-determined width/height.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序