PHP to implement anti-color processing of images PHP tips

Source: Internet
Author: User
This article mainly introduces the implementation of PHP anti-color processing of the image, involving PHP for image reading, numerical operations and other related operations skills, the need for friends can refer to the following

In this paper, we describe the anti-color processing function of PHP for image. Share to everyone for your reference, as follows:

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 ~):

Articles you may be interested in:

PHP tips for installing extensions by using the Pecl method

PHP tips for mb_strstr basic PHP Learning Notes

PHP deletes an array of elements specified in the subscript method PHP instance

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.