我有一個上傳腳本在PHP中,需要一個文件或多個文件,並將它們上傳到我的服務器上。我現在試圖在上傳之前壓縮圖像以節省時間和服務器空間。PHP - 壓縮圖像後的move_uploaded_file
我的壓縮功能和上傳功能都可以自己工作,但我無法將兩者結合起來。
如果我上傳不壓縮,如:
if (move_uploaded_file($temp, "../images/" . $upload)){
它工作正常,但它試圖移動壓縮圖像不是全尺寸的一個喜歡這裏:
// compression
$compressed = compress_image($temp, $upload, 70);
//
if (move_uploaded_file($temp, "../images/" . $compressed)){
全部代碼如下:
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";
}
}
}
您正在返回錯誤的變量。你也可以在不使用'move_uploaded_file'的情況下安全地保存你的壓縮文件 – putvande
我應該使用哪個變量?我剛剛刪除了move_uploaded_file,並在服務器上保存了壓縮文件,我剛剛閱讀並聽說在php中上傳文件時使用它總是比較安全的。 –