我有以下代码从$_POST
获取HTML <form>
个ed图像并上传它们。然后将它们全部重新调整大小(最多两次),并将这些较小的图像保存到同一目录中。
但是,在处理较大的文件时,我的内存不足。
我的问题不是“如何防止X错误发生?”,但是“如何释放此脚本正在使用的内存以便它可以继续?”
<?php
if (!isset($_POST['SiteID']) || !filter_var($_POST['SiteID'], FILTER_VALIDATE_INT)) {
header("Location: ./");
exit();
}
$SiteID = $_POST['SiteID'];
if (!isset($_POST['GalleryID']) || !filter_var($_POST['GalleryID'], FILTER_VALIDATE_INT)) {
header("Location: ./?SiteID=" . $SiteID . "&error=upload");
exit();
}
$GalleryID = $_POST['GalleryID'];
$ImageFile = $_FILES['ImageFile'];
$Extensions = array("jpg", "jpeg");
$Timestamp = date("U");
require_once("db.php");
for ($X = 0; $X < count($_FILES['ImageFile']['name']); $X++) {
$Extension = strtolower(pathinfo($ImageFile['name'][$X], PATHINFO_EXTENSION));
if (in_array($Extension, $Extensions)) {
if ($ImageFile['error'][$X] === UPLOAD_ERR_OK) {
if ($ImageFile['size'][$X] <= 5 * 1024 * 1024) {
$Filename = $GalleryID . "-" . $Timestamp . "-" . $X . "." . $Extension;
move_uploaded_file($ImageFile['tmp_name'][$X], "uploads/" . $Filename);
mysql_query("INSERT INTO NewGalleryImages SET GalleryID = $GalleryID, ImageFile = '$Filename', ImageCaption = ''");
$Dimensions = getimagesize("uploads/" . $Filename);
$Width = ($Dimensions[0] > 768 ? 768 : $Dimensions[0]);
$Height = ($Dimensions[1] / $Dimensions[0]) * $Width;
$Expl = explode(".", $Filename);
$LargeFilename = $Expl[0] . "_gallery." . $Expl[1];
$Source = "uploads/" . $Filename;
$Canvas = imagecreatetruecolor($Width, $Height);
$LargeImage = imagecreatefromjpeg($Source);
imagecopyresampled($Canvas, $LargeImage, 0, 0, 0, 0, $Width, $Height, $Dimensions[0], $Dimensions[1]);
imagejpeg($Canvas, "uploads/" . $LargeFilename, 100);
imagedestroy($Canvas);
$Width = 200;
$Height = ($Dimensions[1] / $Dimensions[0]) * $Width;
$Expl = explode(".", $Filename);
$ThumbFilename = $Expl[0] . "_thumb." . $Expl[1];
$Source = "uploads/" . $Filename;
$Canvas = imagecreatetruecolor($Width, $Height);
$ThumbImage = imagecreatefromjpeg($Source);
imagecopyresampled($Canvas, $ThumbImage, 0, 0, 0, 0, $Width, $Height, $Dimensions[0], $Dimensions[1]);
imagejpeg($Canvas, "uploads/" . $ThumbFilename, 100);
imagedestroy($Canvas);
} else {
mysql_close();
header("Location: ./?SiteID=" . $SiteID . "&error=size");
exit();
}
} else {
mysql_close();
header("Location: ./?SiteID=" . $SiteID . "&error=unknown");
exit();
}
} else {
mysql_close();
header("Location: ./?SiteID=" . $SiteID . "&error=type");
exit();
}
}
mysql_close();
header("Location: ./?SiteID=" . $SiteID . "&uploaded");
exit();
// var_dump($_POST);
// var_dump($_FILES);
// var_dump($ImageFile);
?>
我在PHP文档中阅读了imagedestroy()
,并尝试在几个地方将其添加到我的脚本中,但是1)我不确定我是否在破坏正确的资源,并且2)我不确定它是否真的在清理记忆。
我目前在服务器上有以下设置:
post_max_size = 64M
upload_max_filesize = 10M
max_input_time = 60
max_input_nesting_level = 64
max_execution_time = 120
memory_limit = 512M
max_input_vars = 1000
这是我收到的错误消息:
Fatal error: Allowed memory size of 536870912 bytes exhausted
(tried to allocate 29440 bytes) in
/var/www/vhosts/example.com/httpsdocs/gallery-upload.php
on line 36
我觉得奇怪的是,无论我将memory_limit
增加到多少,它需要分配的“额外”总是相同的:29440 bytes
。
如果我单独上传图片,脚本运行正常,但一次上传两个以上会引发错误。调整两个图像的512MB以上的内存似乎过多,不是吗?还是内存还不够?我不确定这台服务器可以节省多少......! 如何减少总内存使用量?
答案 0 :(得分:1)
经过一段时间的游戏,我设法让我的脚本工作。我需要做的是在新图像上使用imagedestroy()
,和每次调整大小后的原始图像,即:
imagedestroy($LargeImage);
imagedestroy($Canvas);
我不确定这是否是可以用来释放内存的一切,但它正在运行(现在)。我相信有人会很快用更大的图像文件测试我的内存限制......