[英]php filesize() always returns same value
I am trying to upload an image via PHP. 我正在尝试通过PHP上传图像。 On upload, it should become resized that its' dimensions are as big as I defined it in my config[]-array and that its' filesize is also smaller or equal to the predefined value in my config[]-array.
在上传时,应该调整大小,使其尺寸与我在config []数组中定义的尺寸一样大,并且它的文件大小也小于或等于config []数组中的预定义值。 But somehow, the method getFileSize() always returns the same size, even after resizing the image.
但是以某种方式,即使在调整图像大小之后,方法getFileSize()始终返回相同的大小。
Here's my code. 这是我的代码。 Explanation follows.
解释如下。
$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++;
}
$image is an object of the type Picture, which is just a wrapper-class for all kind of functions I need related to modifying pictures. $ image是Picture类型的对象,它只是我与修改图片相关的所有功能的包装类。
$config is my configuration-array which includes all kind of predefined values. $ config是我的配置数组,其中包含所有类型的预定义值。
$tries holds the number of tries that are allowed. $ tries保留允许的尝试次数。 The program is allowed to resize the image no more than 10 times.
该程序允许调整图像大小不超过10倍。
getFileSize() returns the image-filesize via return filesize( path ) getFileSize()通过return filesize( path )返回图像文件大小。
resize(maxWidth,maxHeight,factor) resizes the image to the size mentioned in the parameters. resize(maxWidth,maxHeight,factor)将图像调整为参数中提到的尺寸。 After it resized the picture, it saves the result to the same path , the filesize is read from.
调整图片大小后,将结果保存到同一路径 ,从中读取文件大小。
I'll just post the resize() and getFileSize() method, since it may interest you: 我将只发布resize()和getFileSize()方法,因为它可能会让您感兴趣:
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;
}
getFileSize() getFileSize()
function getFileSize() {
return filesize($this->file_path);
}
Thanks! 谢谢!
Try http://www.php.net/manual/en/function.clearstatcache.php 尝试http://www.php.net/manual/zh/function.clearstatcache.php
function getFileSize() {
clearstatcache();
return filesize($this->file_path);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.