我正在使用以下代码来显示页面目录中的图像
$dirname = "img/data/new/";
$images = glob($dirname."*.png");
foreach($images as $image) {
echo '<img src="'.$image.'" alt="" data-size=""/><br />';
}
工作正常,但我还需要访问要在 alt=""
中显示的每个图像名称以及 data-size=""
中显示的图像大小。您能让我知道如何在这里使用 getimagesize()
或 getImageProperties()
吗?
最佳答案
要获取图像文件的尺寸,您需要提供其路径
到 getimagesize() ,要获取 alt
的文件名
,您可以使用 basename() ,即:
$dirname = "img/data/new/";
$website = "https://mysite/";
$images = glob($dirname."*.png");
foreach($images as $image) {
list($width, $height) = getimagesize($image); //get the $width, $height of the image
$filename = basename($image); // get the filename for alt
echo <<< LOL
<img src="{$website}{$dir}{$image}" alt="$filename" data-size="$width:$height"/><br />
LOL;
}
关于php - 如何在 PHP 中从文件目录获取图像属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36852959/