You are here: Grayscaling images causes mangled image + segfault « PHP Programming Language « IT news, forums, messages
Grayscaling images causes mangled image + segfault

Posted by comp.lang.php on 04/14/07 17:20

[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/function.imagecopymergegray.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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация