當然可以。做這樣的事情。
$upload = $_FILES['uploaded_img'];
$uploadPath = 'new/path/for/upload/';
$uploadName = pathinfo($upload['name'], PATHINFO_FILENAME);
$restrainedQuality = 75; //0 = lowest, 100 = highest. ~75 = default
$sizeLimit = 2000;
if($upload['size'] > $sizeLimit) {
//open a stream for the uploaded image
$streamHandle = @fopen($upload['tmp_name'], 'r');
//create a image resource from the contents of the uploaded image
$resource = imagecreatefromstring(stream_get_contents($streamHandle));
if(!$resource)
die('Something wrong with the upload!');
//close our file stream
@fclose($streamHandle);
//move the uploaded file with a lesser quality
imagejpeg($resource, $uploadPath . $uploadName . '.jpg', $restrainedQuality);
//delete the temporary upload
@unlink($upload['tmp_name']);
} else {
//the file size is less than the limit, just move the temp file into its appropriate directory
move_uploaded_file($upload['tmp_name'], $uploadPath . $upload['name']);
}
這將接受PHP GD支持的任何圖像格式(假設它安裝在您的服務器上,很可能是)。如果圖片小於限制,它只會將原始圖片上傳到您指定的路徑。
閱讀圖片功能:http://php.net/manual/en/ref.image.php您可以使用'imagejpeg'以另一種質量保存圖像,但您不知道原始質量雖然。 – Cyclonecode
是的,我想我不能告訴圖像質量。這就是爲什麼當文件大小超過一定限制時我只能運行這些代碼。 – user1091856