php filesize()始终返回相同的值
我正在尝试通过PHP上传图像。在上传时,应该调整其大小,使其“维度”与我在配置[]-数组中定义的维度一样大,并且其“文件大小”也小于或等于配置[]-数组中的预定义值。但是无论如何,getFileSize方法总是返回相同的大小,即使在调整图像大小之后也是如此 这是我的密码。解释如下php filesize()始终返回相同的值,php,image,gd,Php,Image,Gd,我正在尝试通过PHP上传图像。在上传时,应该调整其大小,使其“维度”与我在配置[]-数组中定义的维度一样大,并且其“文件大小”也小于或等于配置[]-数组中的预定义值。但是无论如何,getFileSize方法总是返回相同的大小,即使在调整图像大小之后也是如此 这是我的密码。解释如下 $tries = 0; while ( $image->getFileSize() > $config['image_max_file_size'] && $tries < 10 )
$tries = 0;
while ( $image->getFileSize() > $config['image_max_file_size'] && $tries < 10 ) {
$factor = 1 - (0.1 * $tries);
echo $image->getFileSize().PHP_EOL;
if ( !$image->resize($config['image_max_width'], $config['image_max_height'], $factor) ) {
return false;
}
$tries++;
}
谢谢 试试看
试一试
clearstatcache-谢谢:P实际上你是第一个,但不能接受这个答案:Sclearstatcache-谢谢:P实际上你是第一个,但不能接受这个答案:S
function resize($neededwidth, $neededheight, $factor) {
$oldwidth = $this->getWidth($this->file_path);
$oldheight = $this->getHeight($this->file_path);
$neededwidth = $neededwidth * $factor;
$neededheight = $neededheight * $factor;
$fext = $this->getInnerExtension();
$img = null;
if ($fext == ".jpeg" ) {
$img = imagecreatefromjpeg($this->file_path);
} elseif ($fext == ".png") {
$img = imagecreatefrompng($this->file_path);
} elseif ($fext == ".gif") {
$img = imagecreatefromgif($this->file_path);
} else {
return false;
}
$newwidth = 0;
$newheight = 0;
if ($oldwidth > $oldheight && $oldwidth > $neededwidth) { // Landscape Picture
$newwidth = $neededwidth;
$newheight = ($oldheight / $oldwidth) * $newwidth;
} elseif ($oldwidth < $oldheight && $oldheight > $neededheight) { // Portrait Picture
$newheight = $neededheight;
$newwidth = ($oldwidth / $oldheight) * $newheight;
}
$finalimg = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($finalimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight);
if ($fext == ".jpeg" ) {
if ( !imagejpeg($finalimg, $this->file_path, 100) ) return false;
} elseif ($fext == ".png") {
if ( !imagepng($finalimg, $this->file_path, 9) ) return false;
} elseif ($fext == ".gif") {
if ( !imagegif($finalimg, $this->file_path) ) return false;
} else {
return false;
}
imagedestroy($img);
return true;
}
function getFileSize() {
return filesize($this->file_path);
}
function getFileSize() {
clearstatcache();
return filesize($this->file_path);
}