不会上传更大的图片 PHP

Wont uploading bigger images PHP

我写了一个上传和调整图片大小的脚本,它曾经工作得很好 但最近 2 天我收到内部服务器错误。这发生在较大的图像“7 mb”上,较小的图像如 1 mb 就可以正常工作。我试图删除缩放因子,这样图像就不会调整大小,但仍然无法正常工作。 我在 iPage 上托管,该脚本过去适用于所有图像尺寸。是服务器大小错误还是脚本错误?优化代码的任何提示。

谢谢大家

<?php
$id = $_POST["id"];
$page = $_POST["page"];

$target_dir = "";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$uploadedfile = $_FILES['fileToUpload']['tmp_name'];

$src = imagecreatefromjpeg($uploadedfile);

list($width,$height)=getimagesize($uploadedfile);

$divide = 1010/$width;
$newwidth=$width*$divide;
$newheight=$height*$divide;
$tmp=imagecreatetruecolor($newwidth,$newheight);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

$filename = $_FILES['fileToUpload']['name'];
imagejpeg($tmp,'../presentation/'.$target_file,100);

imagedestroy($src);
imagedestroy($tmp);
?>

错误代码:

20150218T075818: mpooutlet.com/scripts/uploadimg.php PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 22464 bytes) in /hermes/bosnaweb04a/b2659/ipg.mpooutletcom/scripts/uploadimg.php on line 13

首先,打开错误报告,以便php告诉你问题是什么:

error_reporting(E_ALL);
ini_set("display_errors", 1); 

我假设上传有效。但是可能存在内存限制。图像分辨率越大,imagecreatetruecolorimagecopyresampled消耗的内存越多。 尝试以下操作:

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

注意:255M 只是一个大胆的猜测,根据源图像和目标图像的大小,消耗的内存可能会高得多。

为了获得更持久的解决方案,我在我的项目中使用了发布在 php.net 上的此函数的变体: http://php.net/manual/en/function.imagecreatefromjpeg.php#64155