输出高度>时无法获取已调整大小的图像的文件大小999px

时间:2018-02-05 15:52:32

标签: php

为了让工作场所中“技术娴熟”的人们更轻松,我们整理了一个网页,为他们在公司网站或发布商文档等工作时调整图片大小。

如果输出图像大小(本例中的高度)大于999px,则PHP会引发错误:PHP Warning: filesize() expects parameter 1 to be a valid path, resource given in /[...]/squish.php on line 39

我认为实际问题可能是我的imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);或我如何计算图像的'新宽度': $new_height = $_POST["size"]; $new_width = floor($width * ($new_height / $height));

在索引页面上,我只有一个选择框,其中有一些预定义的高度供用户选择,其中一个是1000 ..这已经存在了几个月,我刚才意识到该选项有问题。

以下示例代码:

'的index.html'

<html>
<head>
<title>Squish - Image resizing.</title>
<script>
    function submitTheSquisher() {
        document.getElementById("squisher").submit()
        hideStuff();
    }
    function hideStuff() {
        document.getElementById("stufftohide").style.display = "none";
        document.getElementById("resetFormBtn").value = "Squish Again!";
    }
    function resetForm() {
        document.getElementById("squisher").reset();
        document.getElementById("stufftohide").style.display = "block";
        document.getElementById("resetFormBtn").value = "Reset From";
    }
</script>
</head>
<body>
<div class='headerWrapper'>
<div class='screenWidth'><img src='logo.png' style='padding:5px 10px 5px 10px;' /></div>
</div>
<div class='screenWidth' style='background:white;'>
<div style='padding:10px;'>
<h2>Image Squisher</h2>
<p>This tool resizes image for use on the Website or on Publisher. It supports .png .jpg and .gif formats. All images are exported as .jpg.</p>
<form action="squish.php" id='squisher' name='squisher' method="post" target='result' enctype="multipart/form-data">
    <div id='stufftohide'>
    Select a size preset:
    <select name='size'>
        <option value='200'>Tiny (Max height 200)</option>
        <option value='400'>Small (Max height 400)</option>
        <option value='600'>Medium (Max height 600)</option>
        <option value='800'>Large (Max height 800)</option>
        <option value='1000'>Extra Large (Max height 1000)</option> <!-- This is the option that causes PHP to throw the error -->
    </select><br/>
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload" onchange='submitTheSquisher'><br/><br/>
    <input type="submit" value="Squish it!" name="submitBtn" id="submitBtn" onclick='hideStuff()'>
    </div><br/>
    <input type='button' name='resetFormBtn' id='resetFormBtn' value='Reset Form' onclick='resetForm()' />
</form>
<iframe name='result' id='result' frameborder='0' height='20px'></iframe>
</div>
</div>
</body>
</html>

'squish.php'

<?php
if(isset($_POST["submitBtn"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        //echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
          // load image and get image size
  switch ($check["mime"]) {
    case "image/jpeg":
        $img = imagecreatefromjpeg($_FILES["fileToUpload"]["tmp_name"]);
        break;
    case "image/gif":
        $img = imagecreatefromgif($_FILES["fileToUpload"]["tmp_name"]);
        break;
    case "image/png":
        $img = imagecreatefrompng($_FILES["fileToUpload"]["tmp_name"]);
        break;
}

  $width = imagesx($img);
  $height = imagesy($img);

  // calculate image size
  $new_height = $_POST["size"];
  $new_width = floor($width * ($new_height / $height));

  // create a new temporary image
  $tmp_img = imagecreatetruecolor($new_width, $new_height);

  // copy and resize old image into new image 
  imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

  // save image into a file

  $theFileName = explode('.', $_FILES["fileToUpload"]["name"]);

  $quoted = sprintf('"%s"', addcslashes(basename($theFileName[0]) . $_POST["size"] . '.jpg', '"\\'));
$size   = filesize($tmp_img);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted); 
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);


  imagejpeg($tmp_img,NULL,100);
  imagedestroy($tmp_img);

    } else {
        echo "File is not an image - " . $check["mime"] . ".";
        $uploadOk = 0;
    }
}
?>

我应该注意,当$_POST["size"]为1000时,图像会开始下载,在发生“网络错误”之前我得到1308kb;该日志仅显示如上所述的一个错误。

Failed - Network error

0 个答案:

没有答案