如何从图像中获取图像高度和宽度哪个位置存储在数据库中?

时间:2023-02-05 20:54:32

I need to get the height and width of an image in order to set stretch it according to the size of the div. I could have used max-width:100% and max-height:100% but there are some images that are smaller than the div so I decided I would just manually assign the width and height.

我需要获取图像的高度和宽度,以便根据div的大小设置拉伸它。我可以使用max-width:100%和max-height:100%但是有些图像比div小,所以我决定只手动分配宽度和高度。

I prefer to get these dimensions in php imagesy() and imagesx().

我更喜欢在php imagesy()和imagesx()中获取这些维度。

   <?php $h=imagesy($list['Image1']); 
    $ w = imagesy($list['Image1'])?>

   <img src="<?php echo $list['Image1']; ?>"
    <?php if($h>$w) echo"style='height:100%;'";
    else echo"style='width:100%;'"; ?> />

2 个解决方案

#1


0  

PHP functions like getimagesize() will return the dimensions of an image. But you are first going to have the load the image into the function. In other words you're going to have to fetch the image first.

像getimagesize()这样的PHP函数将返回图像的尺寸。但是你首先要将图像加载到函数中。换句话说,你将不得不首先获取图像。

If you have allow_url_fopen enabled then you should be able to invoke it with an absolute URL:

如果启用了allow_url_fopen,那么您应该能够使用绝对URL调用它:

$size = getimagesize("http://www.example.com/gifs/logo.gif");

#2


0  

Okay, I was able to read just now that getimagesize returns an array with the width at [0] and height at [1]. So I was still able to compare them directly, hence, dynamically resizing them to fit the div containing the image. ;)

好的,我刚才能够读到getimagesize返回宽度为[0]且高度为[1]的数组。所以我仍然可以直接比较它们,因此,动态调整它们以适应包含图像的div。 ;)

    <?php $h=(getimagesize($list['Image1']));?>
    <img src="<?php echo $list['Image1']; ?>" 
    style="<?php 
    if($h[0]>$h[1])
    {
        echo'width:100%;margin:0 auto;';    
    }
    else
    {
        echo'height:100%;vertical-alignment:auto;'; 
    } ?>"
    alt="<?php echo $list['Image1']; ?>" />

#1


0  

PHP functions like getimagesize() will return the dimensions of an image. But you are first going to have the load the image into the function. In other words you're going to have to fetch the image first.

像getimagesize()这样的PHP函数将返回图像的尺寸。但是你首先要将图像加载到函数中。换句话说,你将不得不首先获取图像。

If you have allow_url_fopen enabled then you should be able to invoke it with an absolute URL:

如果启用了allow_url_fopen,那么您应该能够使用绝对URL调用它:

$size = getimagesize("http://www.example.com/gifs/logo.gif");

#2


0  

Okay, I was able to read just now that getimagesize returns an array with the width at [0] and height at [1]. So I was still able to compare them directly, hence, dynamically resizing them to fit the div containing the image. ;)

好的,我刚才能够读到getimagesize返回宽度为[0]且高度为[1]的数组。所以我仍然可以直接比较它们,因此,动态调整它们以适应包含图像的div。 ;)

    <?php $h=(getimagesize($list['Image1']));?>
    <img src="<?php echo $list['Image1']; ?>" 
    style="<?php 
    if($h[0]>$h[1])
    {
        echo'width:100%;margin:0 auto;';    
    }
    else
    {
        echo'height:100%;vertical-alignment:auto;'; 
    } ?>"
    alt="<?php echo $list['Image1']; ?>" />