加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP图像像素化?

发布时间:2020-12-13 22:42:59 所属栏目:PHP教程 来源:网络整理
导读:我需要用php制作这个效果.我知道 PHP图像过滤器中有IMG_FILTER_PIXELATE.但我需要它更流畅和浮雕?像在这张图片中: 此效果将使用户上传的任何图像变为像素化,并且图片的边缘变为红色(我知道IMG_FILTER_EDGEDETECT但我不知道如何使用它来更改边缘颜色). 我不
我需要用php制作这个效果.我知道 PHP图像过滤器中有IMG_FILTER_PIXELATE.但我需要它更流畅和浮雕?像在这张图片中:

此效果将使用户上传的任何图像变为像素化,并且图片的边缘变为红色(我知道IMG_FILTER_EDGEDETECT但我不知道如何使用它来更改边缘颜色).

我不知道该怎么做.

由于最后的答案是理论上的,似乎还不够,我已经创建了一个实际的例子:
注意:这远非“理想”和完美的像素化效果函数,但它确实起作用.您可以根据自己的需要随意编辑.
<?php
/* Function to make pixelated images
* Supported input: .png .jpg .jpeg .gif
* 
*
* Created on 24.01.2011 by Henrik Peinar
*/


/*
* image - the location of the image to pixelate 
* pixelate_x - the size of "pixelate" effect on X axis (default 10)
* pixelate_y - the size of "pixelate" effect on Y axis (default 10)
* output - the name of the output file (extension will be added)
*/
function pixelate($image,$output,$pixelate_x = 20,$pixelate_y = 20)
{
    // check if the input file exists
    if(!file_exists($image))
        echo 'File "'. $image .'" not found';

    // get the input file extension and create a GD resource from it
    $ext = pathinfo($image,PATHINFO_EXTENSION);
    if($ext == "jpg" || $ext == "jpeg")
        $img = imagecreatefromjpeg($image);
    elseif($ext == "png")
        $img = imagecreatefrompng($image);
    elseif($ext == "gif")
        $img = imagecreatefromgif($image);
    else
        echo 'Unsupported file extension';

    // now we have the image loaded up and ready for the effect to be applied
    // get the image size
    $size = getimagesize($image);
    $height = $size[1];
    $width = $size[0];

    // start from the top-left pixel and keep looping until we have the desired effect
    for($y = 0;$y < $height;$y += $pixelate_y+1)
    {

        for($x = 0;$x < $width;$x += $pixelate_x+1)
        {
            // get the color for current pixel
            $rgb = imagecolorsforindex($img,imagecolorat($img,$x,$y));

            // get the closest color from palette
            $color = imagecolorclosest($img,$rgb['red'],$rgb['green'],$rgb['blue']);
            imagefilledrectangle($img,$y,$x+$pixelate_x,$y+$pixelate_y,$color);

        }       
    }

    // save the image
    $output_name = $output .'_'. time() .'.jpg';

    imagejpeg($img,$output_name);
    imagedestroy($img); 
}


pixelate("test.jpg","testing");


?>

这是在图像上创建像素化效果的示例函数.
以下是使用此功能的示例结果:
原版的:

像素化5px:

像素化10px:

像素化20px:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读