【发布时间】:2015-09-04 20:51:20
【问题描述】:
我正在尝试添加一个功能以在上传图像时减小文件大小,我有上传图像的代码,它工作正常,但对这一切都是新手。我会为此使用 imagejpeg 还是有更好(更简单)的方式?我没有包括我的尝试,因为它们失败了,所以这里是使用基本提交文件输入的工作标准上传脚本..
$file_formats = array("jpg","jpeg","JPEG",'PNG', "png", "gif", "bmp");
$filepath = "upload_images/";
if ($_POST['submitbtn']=="Submit") {
$name = $_FILES['imagefile']['name'];
$size = $_FILES['imagefile']['size'];
if (strlen($name)) {
$extension = substr($name, strrpos($name, '.')+1);
if (in_array($extension, $file_formats)) {
if ($size < (2048 * 1024)) {
$imagename = md5(uniqid() . time()) . "." . $extension;
$tmp = $_FILES['imagefile']['tmp_name'];
if (move_uploaded_file($tmp, $filepath . $imagename)) {
//execute function once uploaded
} else {
echo "Could not move the file.";
}
} else {
echo "Your image size is bigger than 2MB.";
}
} else {
echo "Invalid file format.";
}
} else {
echo "Please select image..!";
}
}
【问题讨论】:
-
如果你想压缩图像并损失一些质量,你可以查看apptha.com/blog/…
-
如果要先在客户端压缩图片,再看stackoverflow.com/questions/2303690/…
标签: php image upload reduce image-size