Home  >  Article  >  Backend Development  >  Detailed explanation of the steps to implement inverse color processing of images in PHP

Detailed explanation of the steps to implement inverse color processing of images in PHP

php中世界最好的语言
php中世界最好的语言Original
2018-05-19 11:38:471847browse

This time I will bring you a detailed explanation of the steps for inverting images in PHP. What are the precautions for inverting images in PHP? The following is a practical case, let’s take a look.

Today I have a need to use PHP to invert the color of the image and convert it to gray. I didn’t know if it was possible before, but then I saw the

imagefilter() function, which is more than enough to convert it to gray. It is so powerful. ;

imagefilter($im, IMG_FILTER_GRAYSCALE)
Of course, some people also set gray in css

<style type="text/css">
img {
-webkit-filter: grayscale(1);/* Webkit */
filter:gray;/* IE6-9 */
filter: grayscale(1);/* W3C */
}
</style>

php color change code:

<?php
/**
* 主要用于图片的处理函数
*/
//图片的反色功能
function color($url) {
  //获取图片的信息
    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++) {
        //获取颜色的所以值
        $index = imagecolorat($img, $x, $y);
        //获取颜色的数组
        $color = imagecolorsforindex($img, $index);
        //颜色值的反转
        $red = 256 - $color['red'];
        $green = 256 - $color['green'];
        $blue = 256 - $color['blue'];
        $hex = imagecolorallocate($img, $red, $green, $blue);
        //给每一个像素分配颜色值
        imagesetpixel($img, $x, $y, $hex);
      }
    }
    //输出图片
    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);
Original picture (take this childhood-ruining spoof picture commonly used by the editor as an example):

After running (this is mainly for testing, As for whether the picture subverts the three views or the five senses, the editor will not ask too much~):

I believe you have mastered the method after reading the case in this article. For more exciting things, please pay attention to php Other related articles on the Chinese website!

Recommended reading:

Detailed explanation of the steps to parse xml and generate sql statements in PHP

PHP implements regular expression group capture Detailed explanation of steps

The above is the detailed content of Detailed explanation of the steps to implement inverse color processing of images in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn