目前有一个简单的功能,但它允许用户添加大图像。我想允许所有图片上传,但应该有一个功能可以调整图像大小,直到满足某个文件大小(例如200kb以下)
if($_FILES['file']['size'] != 0){
$uploadOk = 0;
//image object
$image = $_FILES['file'];
//create unique name for file
$imageRandomName = substr(md5(time()), 0, 5) . "-" . $image['name'];
//upload dir
$target_dir = "rest/user" . $_SESSION["username"] . "/events/" . $_SESSION["event_id"] . "/people/";
//make folder for user if non exists
if(!file_exists($target_dir)){
mkdir($target_dir, 0777, true);
}
//file to be upload
$target_file = $target_dir . base($imageRandomName);
//get file type
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
$check = getimagesize($image["tmp_name"]);
if($check !== false){
$uploadOk = 1;
} else {
$uploadOk = 0;
}
if($uploadOk == 0){
echo "noupload";
} else {
if ( move_uploaded_file($image["tmp_name"], $target_file ) ) {
//get file size on server
$filesizeonserver = filesize($target_file);
echo "file: ".$target_file." size: ".$filesizeonserver;
$times = 0;
if($filesizeonserver > 100000){
do{
$resized = resizeImage($target_file, "0.1");
$filesizeonserver = filesize($target_file);
$times++;
} while ($filesizeonserver > 100000);
echo "resized ".$times." times";
} else {
}
//mysql query
} else {
echo "Sorry, there was an error uploading your file.";
}
}
} else {
//do mysql query
}
resizeImage()
function resizeImage($file, $percent){
list($width, $height) = getimagesize($file);
$newwidth = $width-($width*$percent);
$newheight = $height-($height*$percent);
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($file);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$newImage = imagejpeg($thumb, $file, 100);
return $newImage;
}
现在我到了某个地方,我想我真的很接近它,但是出于一些奇怪的原因,我得到的图像尺寸为宽1px,高4px,尺寸为4kb。完全黑色的图像。
如果您正在考虑添加到此功能的图像: 1,232,055字节,1220×1829,72dpi,JPEG
编辑:好的,现在我认为resizeImage函数有问题。因为它只是第一次工作。由于某种原因,它无法获得图像并使其变得黑而小。EDIT2:现在我正在获得图像大小调整功能,它没有任何问题。我注意到这个函数不断调整图像大小直到它结束。我可以在发现它如何收缩时看到它。
EDIT3:我找到了问题所在。现在看来$ filesizeonserver始终是第一个值,并且在做...虽然它没有更新,即使你我将它设置为更改。
答案 0 :(得分:0)
$allowedFileTypes = ['image/png', 'image/gif', 'image/jpeg', 'image/jpg'];
if (in_array($_FILES['image']['type'], $allowedFileTypes)) {
$imageType = $_FILES['image']['type'];
//allow file size under 10mb
if($_FILES['file']['size'] < 10000000){
//image object
$image = $_FILES['file'];
//so there is no same name files
$imageRandomName = substr( md5( time() ), 0, 5 ) . "-" . $image['name'];
//place where image is uploaded
$target_dir = 'rest/user/' . $_SESSION['username'] . '/events/' . $_SESSION['event_id'] . '/people/';
//make folder for users
if ( !file_exists( $target_dir ) ) {
mkdir( $target_dir, 0777, true );
}
//file to be uploded
$target_file = $target_dir . basename( $imageRandomName );
//get file type
//$imageFileType = pathinfo( $target_file, PATHINFO_EXTENSION );
$check = getimagesize($image["tmp_name"]);
if ($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
//check if upload is ok
if ( $uploadOk == 0 ) {
echo "Sorry cant upload";
} else {
if ( move_uploaded_file($image["tmp_name"], $target_file ) ) {
//get file size on server
$filesizeonserver = filesize($target_file);
$times = 0;
if($filesizeonserver > 300000){
do{
clearstatcache();
$resized = resizeImage($target_file, 0.05, $imageType);
$filesizeonserver = filesize($target_file);
$times++;
} while ($filesizeonserver > 300000);
echo "resized ".$times." times";
} else {
}
//sql query
} else {
echo "Sorry, there was an error uploading your file.";
}
}
} else {
//sql query
}
} else {
echo "wrongimagetype";
}
resizeImage()
function resizeImage($file, $percent, $imageType){
list($width, $height) = getimagesize($file);
$newwidth = $width-($width*$percent);
$newheight = $height-($height*$percent);
$thumb = imagecreatetruecolor($newwidth, $newheight);
switch($imageType){
case 'image/png':
$background = imagecolorallocate($thumb, 0, 0, 0);
imagecolortransparent($thumb, $background);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
$source = imagecreatefrompng($file);
break;
case 'image/gif':
$background = imagecolorallocate($thumb, 0, 0, 0);
imagecolortransparent($thumb, $background);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
$source = imagecreatefromgif($file);
break;
case 'image/jpeg':
case 'image/jpg':
$source = imagecreatefromjpeg($file);
break;
}
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
switch($imageType){
case 'image/png':
$image = imagepng($thumb, $file, 0);
break;
case 'image/gif':
$image = imagegif($file);
break;
case 'image/jpeg':
case 'image/jpg':
$image = imagejpeg($thumb, $file, 100);
break;
}
return $image;
}
输出:
File is an image - image/jpeg.resized 17 times
免责声明:整件事情使您的图像质量受到一点影响。需要努力。