PHP implementation of anti-color image processing steps

Source: Internet
Author: User
This time to everyone to bring PHP implementation of the image of the anti-color processing steps, PHP implementation of the image of the anti-color processing of the attention of the matter, the following is the actual case, a look.

Today, there is a need to use PHP to color the picture, and turn gray, before I did not know is not feasible, and then saw the imagefilter() function, used to turn gray more than enough, good strong;

ImageFilter ($im, Img_filter_grayscale)

Of course, there are people in CSS to set the gray

<style type= "Text/css" >img {-webkit-filter:grayscale (1);/* WebKit */filter:gray;/* ie6-9 */filter:grayscale (1) ; * * */}</style>

PHP color code:

<?php/*** is mainly used for image processing function *///image inverse color function color ($url) {//Get picture Information list ($width, $height, $type, $attr) = GetImageSize (    $url);    $imagetype = Strtolower (Image_type_to_extension ($type, false)); $fun = ' Imagecreatefrom '. ($imagetype = = ' jpg '? ')    JPEG ': $imagetype);    $img = $fun ($url); for ($y =0; $y < $height; $y + +) {for ($x =0; $x < $width; $x + +) {//Get the value of the color $index = Imagecolorat (        $img, $x, $y);        Gets an array of colors $color = Imagecolorsforindex ($img, $index);        The inverse of the color value $red = 256-$color [' Red '];        $green = 256-$color [' Green '];        $blue = 256-$color [' Blue '];        $hex = Imagecolorallocate ($img, $red, $green, $blue);      Assigns a color value to each pixel Imagesetpixel ($img, $x, $y, $hex);      }}//Output picture switch ($imagetype) {case ' gif ': imagegif ($img);      Break      Case ' JPEG ': imagejpeg ($img);      Break      Case ' png ': imagepng ($img);      Break    Default:break; }}

Test code:

$imgurl = ' 1.jpg '; echo color ($imgurl);

The original picture (in the small series commonly used in the destruction of childhood spoof diagram for example):

After the operation (here to test-oriented, as for the image of the three-view subversion or facial features, small series will not ask the ~):

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP implementation parsing XML and generating SQL statement steps in detail

PHP implementation of regular expression grouping capture steps in detail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.