Home > Backend Development > PHP Tutorial > PHP implements the inverse color processing function of images php skills

PHP implements the inverse color processing function of images php skills

jacklove
Release: 2023-04-01 22:34:01
Original
1518 people have browsed it

This article mainly introduces the function of inverse color processing of images in PHP, involving PHP's related operation skills for image reading, numerical calculations, etc. Friends in need can refer to the following

The examples in this article are described PHP implements the function of inverting the color of images. I share it with you for your reference. The details are as follows:

Today I have a need to use php to invert the color of the image and turn it gray. I didn’t know if it was possible before, but then I saw imagefilter() The function is more than enough to turn gray, 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 transfer code:

<?php
/**
* 主要用于图片的处理函数
*/
//图片的反色功能
function color($url) {
  //获取图片的信息
    list($width, $height, $type, $attr)= getimagesize($url);
    $imagetype = strtolower(image_type_to_extension($type,false));
    $fun = &#39;imagecreatefrom&#39;.($imagetype == &#39;jpg&#39;?&#39;jpeg&#39;:$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[&#39;red&#39;];
        $green = 256 - $color[&#39;green&#39;];
        $blue = 256 - $color[&#39;blue&#39;];
        $hex = imagecolorallocate($img, $red, $green, $blue);
        //给每一个像素分配颜色值
        imagesetpixel($img, $x, $y, $hex);
      }
    }
    //输出图片
    switch ($imagetype) {
      case &#39;gif&#39;:
      imagegif($img);
      break;
      case &#39;jpeg&#39;:
      imagejpeg($img);
      break;
      case &#39;png&#39;:
      imagepng($img);
      break;
      default:
      break;
    }
}
Copy after login

Test code:

$imgurl=&#39;1.jpg&#39;;
echo color($imgurl);
Copy after login

Original picture (take this childhood-ruining spoof picture commonly used by the editor as an example):

Run Finally (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~):

You may feel Articles of interest:

#php examples of installing extensions through pecl to explain php skills

php study notes-basic php skills for using mb_strstr

PHP deletes the element method of the specified subscript in the array php example

The above is the detailed content of PHP implements the inverse color processing function of images php skills. 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