【发布时间】:2015-04-01 11:44:07
【问题描述】:
我正在使用一个函数来检查图像是否为灰度,文件路径和名称是否正确加载,有时运行正常。
但是它开始出现通常的内存耗尽错误,所以我想知道是什么原因造成的?
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in ... on line 51
第 51 行是$b[$i][$j] = $rgb & 0xFF;
我如何优化这个函数以使用更少的内存,可能只做一半的图像然后计算出一个平均值,或者如果平均值太高?
function checkGreyscale(){
$imgInfo = getimagesize($this->fileLocation);
$width = $imgInfo[0];
$height = $imgInfo[1];
$r = array();
$g = array();
$b = array();
$c = 0;
for ($i=0; $i<$width; $i++) {
for ($j=0; $j<$height; $j++) {
$rgb = imagecolorat($this->file, $i, $j);
$r[$i][$j] = ($rgb >> 16) & 0xFF;
$g[$i][$j] = ($rgb >> 8) & 0xFF;
$b[$i][$j] = $rgb & 0xFF;
if ($r[$i][$j] == $g[$i][$j] && $r[$i][$j] == $b[$i][$j]) {
$c++;
}
}
}
if ($c == ($width * $height))
return true;
else
return false;
}
【问题讨论】:
标签: php image-processing memory-leaks