[英]PHP - move_uploaded_file after compressing image
I have an upload script in php that takes a file or multiple files and uploads them onto my server. 我在php中有一个上传脚本,该脚本接受一个或多个文件并将它们上传到我的服务器上。 I am now trying to compress the image down before uploading to save time and server space.
我现在正尝试在上传之前压缩图像,以节省时间和服务器空间。
Both my compression function and upload functions work on their own but i cannot combine the two. 我的压缩功能和上传功能都可以单独工作,但是我无法将两者结合在一起。
If i do the upload without compression, as in: 如果我没有压缩就上传,如:
if (move_uploaded_file($temp, "../images/" . $upload)){
it works fine, but it's trying to move the compressed image not the full size one like here: 它工作正常,但是它尝试移动压缩的图像而不是完整尺寸的图像,如下所示:
// compression
$compressed = compress_image($temp, $upload, 70);
//
if (move_uploaded_file($temp, "../images/" . $compressed)){
Full code is below: 完整代码如下:
function getExtension($str) {
$ext = pathinfo($str, PATHINFO_EXTENSION);
return $ext;
}
function compress_image($source_url, $destination_url, $quality) {
$info = getimagesize($source_url);
if ($info['mime'] == 'image/jpeg'){$image = imagecreatefromjpeg($source_url);}
else if ($info['mime'] == 'image/gif'){$image = imagecreatefromgif($source_url);}
else if ($info['mime'] == 'image/png'){$image = imagecreatefrompng($source_url);}
imagejpeg($image, $destination_url, $quality);
return $destination_url;
}
foreach ($_FILES["images"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$temp = $_FILES["images"]["tmp_name"][$key];
$name = $_FILES["images"]["name"][$key];
$extension = getExtension($name);
$extension = strtolower($extension);
$upload = $_POST['name'].'.'.$extension;
// compression
$compressed = compress_image($temp, $upload, 70);
//
if (move_uploaded_file($temp, "../images/" . $compressed)){
echo "OK";
}
else{
echo "ERROR";
}
}
}
the documentation says that. 该文件说。
If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.
如果filename不是有效的上传文件,则不会执行任何操作,并且move_uploaded_file()将返回FALSE。
i am guessing that it will only work on the original upload file. 我猜想它只能在原始上传文件上使用。 why don't you first
move_upload_file
and then do the compression. 为什么不先移动
move_upload_file
然后进行压缩。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.