我正在创建一个需要使用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>
问题是,当图像上传时,它给了我“重新调整大小失败”的东西,所以它的imagecopyresesampled函数失败了。我还通过回声函数检查了宽度和高度的变化,它们都是正常的。gd库也运行良好。
最佳答案
gd函数的典型问题是它们很快就达到了php的内存上限。
执行phpinfo()
并检查电流限制。
我建议您将其增加到64m,甚至128m。您可以在php.in i文件中更改它,或者添加一个.htaccess文件,其中:
php_value memory_limit 64M
这也可以从.php文件获得:
ini_set('memory_limit', '64M');
phpinfo()
右侧的列应显示修改后的值。关于php - php imagecopyresampled无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16095914/