Hi, readers today I am going to share image processing. In PHP, we can perform so many operations on the image. Here in this article, you will find the solution how to convert a normal image to a greyscale image in PHP, describe in detail related inbuilt PHP function also which is used in this demo.
PHP
Most people or programmer using PHP because PHP is easy to understand and there are so many images processing built-in libraries which is most useful for the programmer to write code.
Index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
$source_file = "thecodehelpers.jpg"; $img1 = ImageCreateFromJpeg($source_file); $imagew = imagesx($img1); $imageh = imagesy($img1); for ($i=0; $i<$imagew; $i++) { for ($j=0; $j<$imageh; $j++) { // here we are getting rgb value $rgb = ImageColorAt($im, $i, $j); // here we are going to extract value for r, g, b $rr = ($rgb >> 16) & 0xFF; $gg = ($rgb >> 8) & 0xFF; $bb = $rgb & 0xFF; // get the Value from the RGB value $g = round(($rr + $gg + $bb) / 3); $val = imagecolorallocate($im, $g, $g, $g); // here we are set the gray value imagesetpixel ($im, $i, $j, $val); } } header('Content-type: image/jpeg'); $rr=imagejpeg($im); |
Image Create From JPEG
Image created from jpeg() returns an image identifier it is representing the image obtained from the given filename. This function will create a new image from file URL but note that JPEG support is only available if PHP was compiled against GD-1.8 or later. So please keep in mind.
Images
This function is inbuilt function of PHP ,which will return exact width of particular given image.
Images
This function is inbuilt function of PHP ,which will return exact height of particular given image.
Image Color Allocate
This is an inbuilt function of PHP, which is mostly use to set color inside image and this function will return colors which is given in RGB format.
So that is it for today, I hope now you guys have understood how to convert image to greyscale image in PHP through this information. If you still have any query you can ask me just by commenting below.
Also read this article: How to Redirect Any URL to Https in PHP using htaccess File
Sweet blog! I found it while browsing on Yahoo
News. Do you have any tips on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get
there! Many thanks