php imagecopyresampled 不工作

标签 php image gd

我正在创建一个需要使用 jQuery 缩放脚本的网站。问题是我需要管理员上传的原始文件的 4 倍大图像。我写的代码是:

$allowedExts = array("jpeg", "jpg");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (in_array($extension, $allowedExts))
{
    if ($_FILES["file"]["error"] > 0)
    {
        $out = "Error: " . $_FILES["file"]["error"] ."<br>";
    }
    else
    {
        $fname =  "img/" . $_POST["map"] . "." . $extension;
        move_uploaded_file($_FILES["file"]["tmp_name"], $fname);

        //crear imagen grande
        if(file_exists($fname)){
            list( $width, $height ) = getimagesize($fname);
            $nwidth = $width * 4;
            $nheight = $height * 4;
            $nimage = imagecreatetruecolor( $nwidth, $nheight );
            $image = imagecreatefromjpeg( $fname );
            if(imagecopyresampled( $nimage, $image, 0, 0, 0, 0, $nwidth, $nheight, $width, $height)){
                $nfname = "img/" . $_POST["map"] . "_big." . $extension;
                imagejpeg( $nimage, $nfname, 100 );
            }
            else{
                echo "Failed At re-sizing the image";
            }
            imagedestroy($image);
            imagedestroy($nimage);
        }
        else{
            echo "Can't find the file";
        }

        $out = $_FILES["file"]["name"] . " has been uploaded sucessfully <br>";
    }
}
else
{
    $out = "Invalid file (JPG-JPEG Only)<br>";
}

发送文件的形式是:

<form action="handler.php" method="post"
                enctype="multipart/form-data">
                <label for="file">Surface:</label>
                <input type="file" name="file" id="file"><br>
                <input type="hidden" name="id" value="2">
                <input type="hidden" name="map" value="surf">
                <input type="submit" class="buttons" name="submit" value="Submit" onmouseover="butOn(this,true)" onmouseout="butOn(this,false)">
              </form> <br><br>

问题是,当上传图片时,它会提示“调整大小失败”,所以它的 imagecopyresampled 函数失败了。我还通过 echo 函数检查了宽度和高度变量,它们没问题。 GD 库也工作正常。

最佳答案

GD 函数的典型问题是它们很快达到 PHP 内存上限。

执行 phpinfo() 并检查当前限制。

我建议您将其增加到 64M 甚至 128M。您可以在 PHP.ini 文件中更改它或添加一个 .htaccess 文件:

php_value memory_limit 64M

这也可以从 .php 文件中实现:

ini_set('memory_limit', '64M');

phpinfo() 右侧的列应该显示修改后的值。

关于php imagecopyresampled 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16095914/

相关文章:

linux - 转换-追加*网格

android - Drawables 在被文件夹中的其他人替换后没有改变

PHP - 将背景颜色更改为透明

php - 如何加载带有 php gd 库的动态图像,不将其保存在服务器上或具有 src ="script.php"?

php - 我如何用 GD 和 PHP 重复水印图像?

php - 准备好的 MySQLi 中的通配符返回错误值

php - print_r($ _ FILES);输出为空Array()**。这意味着没有文件上传,但是我已经上传了文件

php - XDebug 已安装,但未在 phpinfo() 中显示且不起作用

javascript - 如何使 child 书籍插图的某些区域可点击?

php - 如何将多行数据转换为数组并从另一个数组中减去它?