Tuesday, February 23, 2010

Can anyone give me a clear example on the following php image functions?

Just don't know how in the world i could use them...


-imagecolorexact


-imagecolorclosest


-imageantialias


-imagecolorallocate





I've been trying to understand the simple description in php.net, but I'm accustomed to seeing how it is done and the result. I've tried looking for examples through google and yahoo... just can't find any... please help...!!! I badly need help... I'm desperate... two more weeks, and I'm going to fail this subject if I don't finish this by next week...Can anyone give me a clear example on the following php image functions?
Hello,





imagecolorexact returns the ';index';of the color in the image.


So if you want to find out if the image has that exact colour then you can use this method:





=============


// Some settings


$src = ';image.gif';;





// Color to find from the 255 pallet


$red = 9; $green = 9; $blue = 1;





// Create pointer to image


$pic = imagecreatefromgif ( $src );





// Grab the index of the colour


$index = imagecolorexact ( $pic, $red, $green, $blue );





// Check if the colour exists


if ( $index != -1 ) echo 'The color exists!';


else echo 'The color does not exist!';





imagedestroy ( $pic );





=============





imagecolorclosest will let you find the ';CLOSEST'; color to the colour specified in that image, similar to the previous function but in this case it finds a similar colour in the image.





imagecolorallocate will just allocate a colour to the image, such as setting the image background to black. Before you do that you have to create the image first ...





An example that uses the above two functions is something like this:


=========


If the *exact color is found in the image, it will be returned. If we don't have the exact color, we try to allocate that colour. If we can't allocate that colour, we return the closest color in the image.








// Check if the exact colour exists


$c=imagecolorexact($image, $r, $g, $b);





// If color has been found, then return the image


if ($c!=-1) {





return $c;





} else {





// The colour hasn't been found,


// Lets try and allocate that colour to the image


$c=imagecolorallocate($image, $r, $g, $b);





// Check if we can allocate the colour


if ($c!=-1) {





return $c;





} else {





return imagecolorclosest($image, $r, $g, $b);


}


=========





imageantialias will allow you to tell the image library to Anti-Alias the image lines. It will make lines smoother and crisp, read the following article regarding this,


http://en.wikipedia.org/wiki/Anti-aliasi鈥?/a>





You set the property to true if you want to use imageantialias, after you create the image, imageantialias($image, true);








If you want to know more about Image manipulation in PHP, look at the comments on the PHP docs, they will help greatly...








Good Luck
  • thinning hair
  • No comments:

    Post a Comment