灰度图像会导致图像损坏+段错误 [英] Grayscaling images causes mangled image + segfault
问题描述
[php]
/ **
* Grayscaling图像类
*
* @author Phil Powell
* @version 1.2.1
* @package IMAGE_CATALOG :: IMAGE
* /
class ImageGrayscaleGenerator扩展了ImageResizeComponents {
/
* ------------------------ -------------------------------------------------- -------------------------------------------------- ------------------------------
由于<的性能相当差,这个类存在br />
imagecopymergegray()命令
从 http://us3.php.net/manual/en/functio...ymergegray.php
第一行评论
一次执行一个像素的实际灰度图像。
--------------------------- -------------------------------------------------- ------------------------------------------- -----------------------------------
* /
//请记住将参考指针用于$ image对象确保
静态更改为对象而非实例
/ **
*构造函数。动态设置所有属性
*
* @access public
* @param resource $ image(reference)
* @param资源$ newImage(参考)
* @param int $ image_width
* @param int $ image_height
* /
函数ImageGrayscaleGenerator(& $ image,& $ newImage,$ image_width,
$ image_height){// CONSTRUCTOR
global $ section;
foreach(数组($ section,''newImage''," $ {section} _width"," $
{section} _height")as $ val)$ this-> $ val =& $ {$ val};
}
/ **
*使图像灰度
*
* @access protected
* /
function makeGray(){// VOID METHOD
global $ section;
for($ i = 0; $ i< = 255; $ i ++)$ colorNDX [$ i] =
@imagecolorallocate($ this-> ; newImage,$ i,$ i,$ i);
if(is_array($ colorNDX)&& @sizeof($ colorNDX)0){
for($ y = 0; $ y< $ this-> {$ section。''_ height''}; $ y ++){
for($ x = 0; $ x< $ this-> {$ section。''_ width''}; $ x ++){
$ ndx = @imagecolorat($ this-> image,$ x,$ y) ;
$ ndxColorArray = @imagecolorsforindex($ this-> image,$ ndx);
$ avg = floor(($ ndxColorArray [''red''] + $ ndxColorArray [''green''] +
$ ndxColorArray [''blue''])/ 3);
@imagesetpixel($ this-> newImage, $ x,$ y,$ colorNDX [$ avg]);
}
}
}
}
}
if($ this-> isSuccessful&& $ image&& $ willGrayscale){// GRAYSCALE
IMAGE
$ igg =&新的ImageGrayscaleGenerator($ image,$ newImage,
$ image_width,$ image_height);
$ igg-> makeGray();
$ igg = null;
}
[/ php]
每当我运行这个类来灰度图像时,我就会结束随着
产生的图像被严重破坏到其原始的
格式完全无法修复的程度,此外,我最终得到了一个Apache
段错误并且必须重新启动服务器才能解决问题。
嗯,为什么?
菲尔
[php]
/**
* Class for grayscaling image
*
* @author Phil Powell
* @version 1.2.1
* @package IMAGE_CATALOG::IMAGE
*/
class ImageGrayscaleGenerator extends ImageResizeComponents {
/
*----------------------------------------------------------------------------------------------------------------------------------------------------------
This class exists due to the rather poor performance of the
imagecopymergegray() command
Borrowing code snippet from http://us3.php.net/manual/en/functio...ymergegray.php
first line comment
Will perform actual grayscaling of image one pixel at a time.
-----------------------------------------------------------------------------------------------------------------------------------------------------------
*/
// REMEMBER TO USE REFERENCE POINTER ONTO $image OBJECT TO ENSURE
"static" CHANGE TO OBJECT AND NOT TO INSTANCE
/**
* Constructor. Set all properties dynamically
*
* @access public
* @param resource $image (reference)
* @param resource $newImage (reference)
* @param int $image_width
* @param int $image_height
*/
function ImageGrayscaleGenerator(&$image, &$newImage, $image_width,
$image_height) { // CONSTRUCTOR
global $section;
foreach (array($section, ''newImage'', "${section}_width", "$
{section}_height") as $val) $this->$val =& ${$val};
}
/**
* Make the image grayscale
*
* @access protected
*/
function makeGray() { // VOID METHOD
global $section;
for ($i = 0; $i <= 255; $i++) $colorNDX[$i] =
@imagecolorallocate($this->newImage, $i, $i, $i);
if (is_array($colorNDX) && @sizeof($colorNDX) 0) {
for ($y = 0; $y < $this->{$section . ''_height''}; $y++) {
for ($x = 0; $x < $this->{$section . ''_width''}; $x++) {
$ndx = @imagecolorat($this->image, $x, $y);
$ndxColorArray = @imagecolorsforindex($this->image, $ndx);
$avg = floor(($ndxColorArray[''red''] + $ndxColorArray[''green''] +
$ndxColorArray[''blue'']) / 3);
@imagesetpixel($this->newImage, $x, $y, $colorNDX[$avg]);
}
}
}
}
}
if ($this->isSuccessful && $image && $willGrayscale) { // GRAYSCALE
IMAGE
$igg =& new ImageGrayscaleGenerator($image, $newImage,
$image_width, $image_height);
$igg->makeGray();
$igg = null;
}
[/php]
Whenever I run this class to grayscale an image, I wind up with the
resulting image being horribly mangled to the point of its original
format completely irreparable, furthermore, I wind up with an Apache
segfault and having to reboot the server to correct the problem.
Um, why?
Phil
推荐答案
image OBURECT TO ENSURE
static更改为对象而非实例
/ **
*构造函数。动态设置所有属性
*
* @access public
* @param resource
image OBJECT TO ENSURE
"static" CHANGE TO OBJECT AND NOT TO INSTANCE
/**
* Constructor. Set all properties dynamically
*
* @access public
* @param resource
image(参考)
* @param资源
image (reference)
* @param resource
newImage(参考)
* @param int
newImage (reference)
* @param int
这篇关于灰度图像会导致图像损坏+段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!