我正在创建一个需要使用 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/