簡體   English   中英

PHP使用getimagesize()獲取圖像的高度和寬度

[英]PHP using getimagesize() for image height and width

有人可以解決此錯誤。 我想獲取具有形式的圖像x和y(width,height)。 我覺得我過得很好。 但是出了點問題。

的HTML

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="img">
    <input type="submit" name="submit" value="Get Resolution">
</form>

的PHP

if (isset($_POST["submit"])) {
    $img = $_FILES["img"]["name"];
    $dir = "x/";
    $getRes = getimagesize("x/".$img);
    echo "This is resolution of the image : ";
    echo "<br>";
    echo "Image size : ".$getRes[4]."<br>";
    echo "Image width : ".$getRes[1]."<br>";
    echo "Image height : ".$getRes[0];
}

它的輸出你可以在這里查看

您需要像這樣使getimagesize:

$ getRes = getimagesize($ _ FILES [“ img”] [“ tmp_name”]);

需要使用臨時文件路徑來檢查它是一種更好的方法。

請更改您的代碼

$img = $_FILES["img"]["name"];

$img = $_FILES["img"]["tmp_name"];

這就是它的工作方式-

if (isset($_POST["submit"])) {
// change name to tmp_name because every file uploaded is saved in temporary location before saving it to actual location
    $img = $_FILES["img"]["tmp_name"];
// take base name of the uploaded file
    $name = basename($_FILES["img"]["name"];
    $dir = "x/";
// move it from temporary to $dir location 
    move_uploaded_file($img, "$dir/$name");
    $getRes = getimagesize("x/".$img);
    echo "This is resolution of the image : ";
    echo "<br>";
    echo "Image size : ".$getRes[4]."<br>";
    echo "Image width : ".$getRes[1]."<br>";
    echo "Image height : ".$getRes[0];
}

暫無
暫無

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

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