簡體   English   中英

用PHP獲取圖像的高度和寬度

[英]get image height and width with php

我有一個小的腳本,它掃描目錄,然后回顯該目錄中所有文件(jpg的)的列表,並將其列出為html圖像鏈接。 它運作良好。

有沒有一種方法可以獲取每個jpg的圖像尺寸?

我需要我的輸出看起來像這樣。

<img src="albm/1.jpg" width="333" height="460" />

<img src="albm/2.jpg" width="256" height="560" />

<img src="albm/3.jpg" width="327" height="580" />

這是我當前沒有圖像尺寸的腳本。

<?php
    $albm = $_REQUEST['albm'];
            $dir = 'albums/'.$albm.'/';
            $files = scandir($dir);
            arsort($files);
            foreach ($files as $file) {
                    if ($file != '.' && $file != '..') {
                            echo '<img src="' . $dir . $file . '"/>';
                    }
            }
?>
<?php
    $albm = $_REQUEST['albm'];
            $dir = 'albums/'.$albm.'/';
            $files = scandir($dir);
            arsort($files);
            foreach ($files as $file) {
                    if ($file != '.' && $file != '..') {
                            $info = getimagesize($dir . $file);
                            $width = $info[0];
                            $height = $info[1];
                            echo '<img src="' . $dir . $file . '"/>';
                    }
            }
?>

這里如何使用它:

        $size = getimagesize($path);
        $width = $size[0];
        $height = $size[1]; 

您可以簡單地使用getimagesize(image location)函數,它返回寬度,高度,類型和屬性。 這是例子

<?php    
    list($width, $height, $type, $attr) = getimagesize("image_name.jpg");
    echo "Image width " .$width;
    echo "<BR>";
    echo "Image height " .$height;
    echo "<BR>";
    echo "Image type " .$type;
    echo "<BR>";
    echo "Attribute " .$attr;
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM