【问题标题】:PHP using getimagesize() for image height and widthPHP 使用 getimagesize() 获取图像高度和宽度
【发布时间】:2019-01-28 12:04:04
【问题描述】:

谁能解决这个错误。我想用表单获取图像 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];
}

它的输出是You can check out here

【问题讨论】:

  • 尝试改变 $getRes = getimagesize("x/".$img);到 $getRes = getimagesize("/x/".$img);你也有变量 $dir = "x/"; - 你不会在任何地方使用它。
  • 感谢问题已解决。

标签: php image height width


【解决方案1】:

这就是它的工作方式-

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];
}

【讨论】:

    【解决方案2】:

    请更改您的代码 来自

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

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

    【讨论】:

      【解决方案3】:

      你需要让你的 getimagesize 像这样:

      $getRes = getimagesize($_FILES["img"]["tmp_name"]);

      使用临时文件路径来检查它是一种更好的方法。

      【讨论】:

        猜你喜欢
        • 2011-11-26
        • 1970-01-01
        • 1970-01-01
        • 2016-07-04
        • 2012-10-17
        • 2010-11-18
        • 1970-01-01
        • 2017-05-23
        • 1970-01-01
        相关资源
        最近更新 更多