douruduan8812
2012-03-13 21:25 阅读 17
已采纳

如何在PHP中修复图像大小?

How do I get images in certain fixed size, example I've attached the image and codes for your review

Please click on this link (image) to understand my question

 <img src="<?php echo $picfile; ?>" border="0" width="<?php echo $imgsize[0]; ?>" height="<?php echo $imgsize[1]; ?>" align="left" style="border:1px solid black;margin-right:5px;">
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    dongshi6529 dongshi6529 2012-03-13 21:28

    You can get image details with

    $details = get_image_size($filename). 
    

    $details[3] will contain the width and height in html format ready for you to use.

    点赞 评论 复制链接分享
  • dqmq0654 dqmq0654 2012-03-13 21:32

    Could possibly try this

    img src='$filename' style='width:250px; height: 250px'
    

    allows you to strech image to specific size

    to scale you could use

    img src='$filename' style='width:auto; height: 250px'
    
    点赞 评论 复制链接分享
  • douran9707 douran9707 2012-03-13 21:36

    If you don't want to do any work in PHP then just wrap the image in a div and set overflow:hidden. This assumes that you know the height you want all of your images to be.

    <div style="width:<?php echo $imgsize[0]; ?>; height:100px; overflow:hidden; border:1px solid black; margin-right:5px">
        <img src="<?php echo $picfile; ?>" width="<?php echo $imgsize[0]; ?>" height="<?php echo $imgsize[1]; ?>">
    </div>
    
    点赞 评论 复制链接分享
  • dongna9185 dongna9185 2012-03-13 21:41

    There are a few ways to do this. As you are looping through your images, you want to keep track of the maximum width.

    $newImageArray = array();
    $maxWidth = 0;
    $maxHeight = 0;
    $i = 0;
    forEach ( $imageArray as $picfile ) {
        $newImageArray[$i]['picFile'] = $picfile;
        $imgsize = get_image_size($picfile);
        $newImageArray[$i]['width'] = $imgsize[0];
        if ( $imgsize[0] > $maxWidth ) {
            $maxWidth = $imgsize[0];
        }
        $newImageArray[$i]['height'] = $imgsize[1];
        if ( $imgsize[1] > $maxHeight ) {
            $maxHeight = $imgsize[1];
        }
        $1++;
    }
    forEach ( $newImageArray as $i) { ?>
        <img src="<?php echo $picfile; ?>" border="0" width="<?php echo $maxWidth; ?>" height="<?php echo $maxHeight; ?>" align="left" style="border:1px solid black;margin-right:5px;">
    <?php
    }
    

    Now you shouldn't really be doing this. A CSS based option would work better. You can add a wrapper container with a width on it and set the images to display:block;width:100%; and the images will always fill the space. I will edit with that solution shortly.

    This will keep the image within a fixed width, and scale the image to fit inside of the box when it is too large.

    HTML:

    <div class="imgWrap">
       <img src="http://www.captainangry.com/img/1236647133510anonib.jpg">
    </div>
    

    CSS:

    .imgWrap {
        width:100px;
        height:100px;
        float:left;
    }
    .imgWrap img {
        display:block;
        max-width:100%;
    }
    
    点赞 评论 复制链接分享

相关推荐