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

php中世界最好的语言
Release: 2023-03-26 20:28:01
Original
1858 people have browsed it

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)
Copy after login
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>
Copy after login

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;
    }
}
Copy after login

Test code:

$imgurl='1.jpg';
echo color($imgurl);
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!