Captcha in codeigniter - php

hi i am generating captcha in codeigniter. I am getting problems while generating captcha. I am using captcha code inside the controller but it is giving an error where i am wrong.
Here is my controller code.
public function captcha(){
//$this->load->library('monofont.ttf');
$image_width = 120;
$image_height = 40;
$characters_on_image = 6;
//include('monofont.ttf');
$font = './monofont.ttf';
$possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
$random_dots = 0;
$random_lines = 20;
$captcha_text_color="0x142864";
$captcha_noice_color = "0x142864";
$code = '';
$i = 0;
while ($i < $characters_on_image) {
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}
$newPwd = $this->hexrgb();
$font_size = $image_height * 0.75;
$image = #imagecreate($image_width, $image_height);
$background_color = imagecolorallocate($image, 255, 255, 255);
$arr_text_color = hexrgb($captcha_text_color);
$text_color = imagecolorallocate($image, $arr_text_color['red'],
$arr_text_color['green'], $arr_text_color['blue']);
$arr_noice_color = hexrgb($captcha_noice_color);
$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'],
$arr_noice_color['green'], $arr_noice_color['blue']);
for( $i=0; $i<$random_dots; $i++ ) {
imagefilledellipse($image, mt_rand(0,$image_width),
mt_rand(0,$image_height), 2, 3, $image_noise_color);
}
for( $i=0; $i<$random_lines; $i++ ) {
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
}
$textbox = imagettfbbox($font_size, 0, $font, $code);
$x = ($image_width - $textbox[4])/2;
$y = ($image_height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
function hexrgb ($hexstr)
{
$int = hexdec($hexstr);
return array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
}
echo $code;
exit;
}
when ever i echo the $code its giving error
Call to undefined function hexrgb()
how to fix this?
my monofont.ttf file is placed inside the controller.

You should also pass parameter to function hexrgb(param) or in function declaration you have to make this parameter optional. simply problem is in function argument

Try This,
function captcha()
{
$image_width = 120;
$image_height = 40;
$characters_on_image = 6;
$font = FCPATH.'assets/monofont.ttf';
/* set font path */
$possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
$random_dots = 0;
$random_lines = 20;
$captcha_text_color="0x142864";
$captcha_noice_color = "0x142864";
$code = '';
$i = 0;
while ($i < $characters_on_image) {
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}
$this->session->set_flashdata('latter_code',$code);
$font_size = $image_height * 0.75;
$image = #imagecreate($image_width, $image_height);
$this->output->set_content_type('jpeg'); // You could also use ".jpeg" which will have the full stop removed before looking in config/mimes.php
$background_color = imagecolorallocate($image, 255, 255, 255);
$arr_text_color = $this->hexrgb($captcha_text_color);
/* changes of hexrgb to $this->hexrgb */
$text_color = imagecolorallocate($image, $arr_text_color['red'],
$arr_text_color['green'], $arr_text_color['blue']);
$arr_noice_color = $this->hexrgb($captcha_noice_color);
/* changes of hexrgb to $this->hexrgb */
$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'],
$arr_noice_color['green'], $arr_noice_color['blue']);
/* generating the dots randomly in background */
for( $i=0; $i<$random_dots; $i++ ) {
imagefilledellipse($image, mt_rand(0,$image_width),
mt_rand(0,$image_height), 2, 3, $image_noise_color);
}
/* generating lines randomly in background of image */
for( $i=0; $i<$random_lines; $i++ ) {
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
}
/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox($font_size, 0, $font, $code);
$x = ($image_width - $textbox[4])/2;
$y = ($image_height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
/* Show captcha image in the page html page */
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
}
set this function of after captcha function
function hexrgb ($hexstr)
{
$int = hexdec($hexstr);
return array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
}
and you want to set view file
<img src="<?php echo site_url('auth/captch')?>" alt="" class="captcha_image" id='captchaimg' style="height: 30px;width:100px"/>

Related

Try a simple captcha in CI4

I want to implement a simple captcha in ci4, I try to search a sample code in google and then copas it. But when run the code get result like this. Please help what wrong in my code ?
My controller is :
private function captcha()
{
$i = 0;
$imgHeight = 80;
$imgWidth = 250;
$randTotal = 7;
$randomDots = 50;
$randomLines = 25;
$font = realpath('./fonts/monofont.ttf');
$random = '';
$captTextColor = "0x142864";
$noiseColor = "0x142864";
$fontSize = $imgHeight * 0.65;
$random = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789'), 0,
$randTotal);
$image = #imagecreate($imgWidth, $imgHeight);
$arrTextColor = $this->hexToRGB($captTextColor);
$arrNoiseColor = $this->hexToRGB($noiseColor);
$bgColor = imagecolorallocate($image, 255, 255, 255);
$captTextColor = imagecolorallocate($image, $arrTextColor['red'], $arrTextColor['green'], $arrTextColor['blue']);
$imgNoiseColor = imagecolorallocate($image, $arrNoiseColor['red'], $arrNoiseColor['green'], $arrNoiseColor['blue']);
// Cetak titik acak di latar belakang gambar
for ($i = 0; $i < $randomDots; $i++) {
imagefilledellipse($image, mt_rand(0, $imgWidth), mt_rand(0, $imgHeight), 2, 3, $imgNoiseColor);
}
// Cetak garis acak di latar belakang gambar
for ($i = 0; $i < $randomLines; $i++) {
imageline(
$image,
mt_rand(0, $imgWidth),
mt_rand(0, $imgHeight),
mt_rand(0, $imgWidth),
mt_rand(0, $imgHeight),
$imgNoiseColor
);
}
// Cetak kotak teks dan tambahkan 6 kode huruf captcha
$textBox = imagettfbbox($fontSize, 0, $font, $random);
$x = ($imgWidth - $textBox[4]) / 2;
$y = ($imgHeight - $textBox[5]) / 2;
$_SESSION['captcha'] = $random;
// imagettftext($image, $fontSize, 0, $x, $y, $captTextColor, $font, $random);
imagettftext($image, $fontSize, 0, $x, $y, $captTextColor, $font, $random);
header('Content-Type: image/jpeg');
imagejpeg($image, null, null);
imagedestroy($image);
}
Thank you
Thank you.
Try adding the exit; command after the imagedestroy($image); line

How to validate my PHP captcha case insensitive?

I would like users submitting a captcha to be able to enter a mix of uppercase and lowercase, regardless of what the captcha image says, and the captcha to still be validated.
So for example, if the captcha image says 'r9zvk1', users should be able to input 'R9zvk1' and still have the captcha validated as correct.
My current captcha code:
<?php
session_start();
$permitted_chars = 'abcdefghjklmnpqrstuvwxyz0123456789';
function generate_string($input, $strength = 10) {
$input_length = strlen($input);
$random_string = '';
for($i = 0; $i < $strength; $i++) {
$random_character = $input[mt_rand(0, $input_length - 1)];
$random_string .= $random_character;
}
return $random_string;
}
$image = imagecreatetruecolor(200, 50);
imageantialias($image, true);
$colors = [];
$red = rand(125, 175);
$green = rand(125, 175);
$blue = rand(125, 175);
for($i = 0; $i < 5; $i++) {
$colors[] = imagecolorallocate($image, $red - 20*$i, $green - 20*$i, $blue - 20*$i);
}
imagefill($image, 0, 0, $colors[0]);
for($i = 0; $i < 10; $i++) {
imagesetthickness($image, rand(2, 10));
$line_color = $colors[rand(1, 4)];
imagerectangle($image, rand(-10, 190), rand(-10, 10), rand(-10, 190), rand(40, 60), $line_color);
}
$black = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image, 255, 255, 255);
$textcolors = [$black, $white];
$fonts = [dirname(__FILE__).'/fonts/font.ttf'];
$string_length = 5;
$captcha_string = generate_string($permitted_chars, $string_length);
$_SESSION['captcha_text'] = $captcha_string;
for($i = 0; $i < $string_length; $i++) {
$letter_space = 170/$string_length;
$initial = 15;
imagettftext($image, 24, rand(-15, 15), $initial + $i*$letter_space, rand(25, 45), $textcolors[rand(0, 1)], $fonts[array_rand($fonts)], $captcha_string[$i]);
}
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
My current captcha validation code:
<?PHP
session_start();
if($_POST['captcha'] != $_SESSION['captcha_text']) die("Sorry, the CAPTCHA code entered was incorrect!");
session_destroy();
?>

Simple captcha php script not displaying text on image

I was hoping to get in touch with someone on a situation that I cannot find the solution to anywhere.
I am trying to create a captcha on my website using php and although I was able to create an image and create the random captcha text.
I am unable to over lay the two. Here is my code:
<?PHP
session_start();
function generateRandomString($length = 10) {
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$len = strlen($letters);
$letter = $letters[rand(0, $len - 1)];
$text_color = imagecolorallocate($image, 0, 0, 0);
$word = "";
for ($i = 0; $i < 6; $i++) {
$letter = $letters[rand(0, $len - 1)];
imagestring($image, 7, 5 + ($i * 30), 20, $letter, $text_color);
$word .= $letter;
}
$_SESSION['captcha_string'] = $word;
}
function security_image(){
// $code = isset($_SESSION['captcha']) ? $_SESSION['captcha'] : generate_code();
//$font = 'content/fonts/comic.ttf';
$width = '110';
$height = '20';
$font_size = $height * 0.75;
// $image = #imagecreate($width, $height) or die('GD not installed');
global $image;
$image = imagecreatetruecolor($width, $height) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,200,50,$background_color);
$line_color = imagecolorallocate($image, 64,64,64);
for($i=0;$i<10;$i++) {
imageline($image,0,rand()%50,200,rand()%50,$line_color);
}
$pixel_color = imagecolorallocate($image, 0,0,255);
for($i=0;$i<1000;$i++) {
imagesetpixel($image,rand()%200,rand()%50,$pixel_color);
}
//$textbox = imagettfbbox($font_size, 0, $font, $code);
//$textbox = imagettfbbox($font_size, 0, $randomString);
$x = ($width - $textbox[4]) / 2;
$y = ($height - $textbox[5]) / 2;
// imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
imagettftext($image, $font_size, 0, $x, $y, $text_color , $word);
$images = glob("*.png");
foreach ($images as $image_to_delete) {
#unlink($image_to_delete);
}
imagepng($image, "image" . $_SESSION['count'] . ".png");
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
}
security_image();
?>
I have no idea what I’m doing wrong. I’ve spent over 10 hours on this “display text” issue. I don’t understand and I am desperate for help. I even downloaded working captcha version from other resources that break once I upload it to my server. I have no idea whats going on. At first I thought there was something wrong with my server but the fact that I can even create the pixels, lines image means that it is at least working.
Please help!!!! 
UPDATE---------------------------------------------
Thank you for your suggestions. Here is the edited code. I'm still getting the same issue.
<?PHP
session_start();
function security_image(){
global $image;
// $code = isset($_SESSION['captcha']) ? $_SESSION['captcha'] : generate_code();
$font = 'content/fonts/comic.ttf';
$width = '110';
$height = '20';
$font_size = $height * 0.75;
$image = imagecreatetruecolor($width, $height) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,200,50,$background_color);
$line_color = imagecolorallocate($image, 64,64,64);
for($i=0;$i<10;$i++) {
imageline($image,0,rand()%50,200,rand()%50,$line_color);
}
$pixel_color = imagecolorallocate($image, 0,0,255);
for($i=0;$i<1000;$i++) {
imagesetpixel($image,rand()%200,rand()%50,$pixel_color);
}
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$len = strlen($letters);
$letter = $letters[rand(0, $len - 1)];
$text_color = imagecolorallocate($image, 0, 0, 0);
$word = "";
for ($i = 0; $i < 6; $i++) {
$letter = $letters[rand(0, $len - 1)];
imagestring($image, 7, 5 + ($i * 30), 20, $letter, $text_color);
$word .= $letter;
}
$_SESSION['captcha_string'] = $word;
/*texbox unitinitialized (removed for the sake of just showing the word size doesnt matter)
$x = ($width - $textbox[4]) / 2;
$y = ($height - $textbox[5]) / 2;
*/
$x = ($width) / 2;
$y = ($height) / 2;
imagettftext($image,$font, 4, $x, $y, $word);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
}
security_image();?>
i made some suggested changes but the code seems to still do the same thing. Just display the lines and pixels as expected but the text still is missing... ?
There are some several "errors" in your functions, let's fix them:
In generateRandomString()
generateRandomString($length = 10)
$lenght is not used its scope.
$text_color = imagecolorallocate($image, 0, 0, 0);
$image is uninitialized
In security_image() function:
$textbox is uninitialized
$text_color and $word is uninitialized.
And Wrong parameter count for imagettftext() You add 7 parameters, and forget the font file parameter.
found the problem. using this:
http://php.net/manual/en/function.imagettftext.php
i was able to see that the font location was incorrect. using the example on that page and editing it to my needs worked.

PHP Script Works on One Domain but Not the Other

I have a PHP script that prints a random number for use as a captcha code. However, for the life of me, I can't figure out why it works on one domain but not the other, even though the servers have the same exact specs.
Displays a captcha code:
http://www.unitedfleetparts.com/php-/captcha_code.php
Displays NOTHING, even though it's the same exact script:
http://www.uatparts.com/php-/captcha_code.php
Please help me figure out how I can get this PHP script to work on my second domain. Any help is appreciated.
PHP Script:
<?php
session_start();
$image_width = 120;
$image_height = 40;
$characters_on_image = 6;
$font = './monofont.ttf';
$possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
$random_dots = 0;
$random_lines = 20;
$captcha_text_color="0x142864";
$captcha_noice_color = "0x142864";
$code = '';
$i = 0;
while ($i < $characters_on_image) {
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}
$font_size = $image_height * 0.75;
$image = #imagecreate($image_width, $image_height);
$background_color = imagecolorallocate($image, 255, 255, 255);
$arr_text_color = hexrgb($captcha_text_color);
$text_color = imagecolorallocate($image, $arr_text_color['red'],
$arr_text_color['green'], $arr_text_color['blue']);
$arr_noice_color = hexrgb($captcha_noice_color);
$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'],
$arr_noice_color['green'], $arr_noice_color['blue']);
for( $i=0; $i<$random_dots; $i++ ) {
imagefilledellipse($image, mt_rand(0,$image_width),
mt_rand(0,$image_height), 2, 3, $image_noise_color);
}
for( $i=0; $i<$random_lines; $i++ ) {
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
}
$textbox = imagettfbbox($font_size, 0, $font, $code);
$x = ($image_width - $textbox[4])/2;
$y = ($image_height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
$_SESSION['6_letters_code'] = $code;
function hexrgb ($hexstr)
{
$int = hexdec($hexstr);
return array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
}
?>

How can I replace one color with another in a png 24 alpha transparent image with GD

I have tried:
$index = imagecolorresolve ( $im, 0,0,0 ); // get black
imagecolorset($im, $index, 255, 0, 255); // SET NEW COLOR
This seems to work with png 8 but not 24, and if I do it with 8 it turns out all weird because of the anti-aliasing.
Here is the full test code I'm using. (this is just test code, so be gentle).
function LoadPNG($imgname, $color = false)
{
$im = #imagecreatefrompng($imgname);
imagealphablending($im, false);
if($color) {
$index = imagecolorresolve ( $im, 0,0,0 ); // get black
imagecolorset($im, $index, 255, 0, 255); // SET NEW COLOR
}
imageAlphaBlending($im, true);
imageSaveAlpha($im, true);
return $im;
}
header('Content-Type: image/png');
$img = LoadPNG("head.png", "red");
imagepng($img);
imagedestroy($img);
Based on the solution of inti, i made a script that works:
$imgname = "yourimage.png";
$im = imagecreatefrompng($imgname);
imagealphablending($im, false);
for ($x = imagesx($im); $x--;) {
for ($y = imagesy($im); $y--;) {
$rgb = imagecolorat($im, $x, $y);
$c = imagecolorsforindex($im, $rgb);
if ($c['red'] < 40 && $c['green'] < 40 && $c['blue'] < 40) { // dark colors
// here we use the new color, but the original alpha channel
$colorB = imagecolorallocatealpha($im, 255, 0, 255, $c['alpha']);
imagesetpixel($im, $x, $y, $colorB);
}
}
}
imageAlphaBlending($im, true);
imageSaveAlpha($im, true);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
I'd like a way to optimize it, because it is quite slow
You can try the following:
cycle all points
get the color of that point
if it matches your colorA, set that pixel to the desired colorB
Code:
for ($x=imagesx($im); $x--; ) {
for ($y=imagesy($im); $y--; ) {
$c = imagecolorat($im, $x, $y);
if ($c[0] == 0 && $c[1] == 0 && $c[2] == 0) {
// here we use the new color, but the original alpha channel
$colorB = imagecolorallocatealpha($im, 255, 0, 255, $c[3]);
imagesetpixel($im, $x, $y, $colorB);
}
}
}
Hope this helps!
this function will replace either 1 or all colors for 1 new color, maintaining transparency levels (otherwise borders will probably look awful, if PARTIAL transparency was used to draw borders).
COMPLETE ANSWER TO SIMILAR POST
<?php
function colorizeKeepAplhaChannnel( $inputFilePathIn, $targetRedIn, $targetGreenIn, $targetBlueIn, $outputFilePathIn ) {
$im_src = imagecreatefrompng( $inputFilePathIn );
$im_dst = imagecreatefrompng( $inputFilePathIn );
$width = imagesx($im_src);
$height = imagesy($im_src);
// Note this: FILL IMAGE WITH TRANSPARENT BG
imagefill($im_dst, 0, 0, IMG_COLOR_TRANSPARENT);
imagesavealpha($im_dst,true);
imagealphablending($im_dst, true);
$flagOK = 1;
for( $x=0; $x<$width; $x++ ) {
for( $y=0; $y<$height; $y++ ) {
$rgb = imagecolorat( $im_src, $x, $y );
$colorOldRGB = imagecolorsforindex($im_src, $rgb);
$alpha = $colorOldRGB["alpha"];
$colorNew = imagecolorallocatealpha($im_src, $targetRedIn, $targetGreenIn, $targetBlueIn, $alpha);
$flagFoundColor = true;
// uncomment next 3 lines to substitute only 1 color (in this case, BLACK/greys)
/*
$colorOld = imagecolorallocatealpha($im_src, $colorOldRGB["red"], $colorOldRGB["green"], $colorOldRGB["blue"], 0); // original color WITHOUT alpha channel
$color2Change = imagecolorallocatealpha($im_src, 0, 0, 0, 0); // opaque BLACK - change to desired color
$flagFoundColor = ($color2Change == $colorOld);
*/
if ( false === $colorNew ) {
//echo( "FALSE COLOR:$colorNew alpha:$alpha<br/>" );
$flagOK = 0;
} else if ($flagFoundColor) {
imagesetpixel( $im_dst, $x, $y, $colorNew );
//echo "x:$x y:$y col=$colorNew alpha:$alpha<br/>";
}
}
}
$flagOK2 = imagepng($im_dst, $outputFilePathIn);
if ($flagOK && $flagOK2) {
echo ("<strong>Congratulations, your conversion was successful </strong><br/>new file $outputFilePathIn<br/>");
} else if ($flagOK2 && !$flagOK) {
echo ("<strong>ERROR, your conversion was UNsuccessful</strong><br/>Please verify if your PNG is truecolor<br/>input file $inputFilePathIn<br/>");
} else if (!$flagOK2 && $flagOK) {
$dirNameOutput = dirname($outputFilePathIn)."/";
echo ("<strong>ERROR, your conversion was successful, but could not save file</strong><br/>Please verify that you have PERMISSION to save to directory $dirName <br/>input file $inputFilePathIn<br/>");
} else {
$dirNameOutput = dirname($outputFilePathIn)."/";
echo ("<strong>ERROR, your conversion was UNsuccessful AND could not save file</strong><br/>Please verify if your PNG is truecolor<br/>Please verify that you have PERMISSION to save to directory $dirName <br/>input file $inputFilePathIn<br/>");
}
echo ("TargetName:$outputFilePathIn wid:$width height:$height CONVERTED:|$flagOK| SAVED:|$flagOK2|<br/>");
imagedestroy($im_dst);
imagedestroy($im_src);
}
$targetRed = 255;
$targetGreen = 255;
$targetBlue = 0;
//$inputFileName = 'frameSquareBlack_88x110.png';
$inputFileName = 'testMe.png';
$dirName = "../img/profilePics/";
$nameTemp = basename($inputFileName, ".png");
$outputFileName = $nameTemp."_$targetRed"."_$targetGreen"."_$targetBlue.png";
$inputFilePath = $dirName.$inputFileName;
$outputFilePath = $dirName.$outputFileName;
//echo "inputFileName:$inputFilePath<br>outputName:$outputFilePath<br>";
colorizeKeepAplhaChannnel( $inputFilePath, $targetRed, $targetGreen, $targetBlue, $outputFilePath);
?>
<br/><br/>
Original <br/>
<img src="<?php echo $inputFilePath; ?>">
<br /><br />Colorized<br/>
<img src="<?php echo $outputFilePath; ?>">
<br />
A good way to doing it is using paintOpaqueImage(), which also permit using color tolerance
$targetColor = "#0074AD";
$fill = "#0074AA";
$tolerance = 30000;
$im = new Imagick( "yourimage.png");
if ($im->paintOpaqueImage ( $targetColor , $fill , $tolerance) ){
$im->writeImage("yourimage.png");
}
You can see the tolenrance doc in http://www.imagemagick.org/script/command-line-options.php#fuzz
This function work like a charm :
public function ImageToBlackAndWhite($im) {
for ($x = imagesx($im); $x--;) {
for ($y = imagesy($im); $y--;) {
$rgb = imagecolorat($im, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8 ) & 0xFF;
$b = $rgb & 0xFF;
$gray = ($r + $g + $b) / 3;
if ($gray < 0xFF) {
imagesetpixel($im, $x, $y, 0xFFFFFF);
}else
imagesetpixel($im, $x, $y, 0x000000);
}
}
imagefilter($im, IMG_FILTER_NEGATE);
}

Categories