【发布时间】:2014-04-17 13:14:07
【问题描述】:
嘿,我有一个页面,用户可以在其中上传图片(一次上传一张,第二次上传会覆盖第一张)。
当有人选择超过 5 MB 的图片时,脚本会上传图片,但随后会运行另一个脚本来调整图片大小以使其更小。
所以,我得到了图像大小,并像这样设置最大大小:
$size = $_FILES["userImage"]["size"]
$maxSize = 5000000 //this should be 5 MB, right?
然后,我将图像移动到具有此行的文件夹
move_uploaded_file( $tmpName, "../user_img/".$sessionId."/".$sessionId."_img.".$type);
最后,如果图像大于 5 MB,我想调整其大小
if($size > $maxSize){
include('resizeImage2.php');
//indicate which file to resize (can be any type jpg/png/gif/etc...)
$file = "../user_img/".$sessionId."/".$sessionId."_img.".$type;
//indicate the path and name for the new resized file
$resizedFile = $file;
//call the function (when passing path to pic)
//smart_resize_image($file, null, 1200, 0, true, $resizedFile, true, false, 100 );
//call the function (when passing pic as string)
smart_resize_image(null , file_get_contents($file), 1200 , 0 , true , $resizedFile , true , false ,100 );
}
resizeImage2.php 是我在网上找到的 address
当我尝试从我的 PC 上传 7MB、分辨率为 10004 x 10967 的图像时,就会出现问题。我收到一个错误
允许的内存大小为 62914560 字节已用尽(尝试分配 40016 字节)
我知道我可以更改 php.ini 中的内存限制,但我想知道是否有其他方法可以使这项工作?我的脚本是否写得不好?
提前致谢!
【问题讨论】:
标签: php image image-uploading image-resizing memory-limit