Shi Zhuolin's Blog - 高斯
type: channel
link: https://www.shizhuolin.com/tag/%E9%AB%98%E6%96%AF/
items: 用PHP+GD扩展实现图像高斯模糊处理

用PHP+GD扩展实现图像高斯模糊处理
type: item
date: 2014-02-24T09:50:00+00:00
description: 很久以前写的代码了，现在才想起贴上来. 稍微更改并结合人脸识别可以实现自动模糊人物的脸部.<?php require_once 'RGB.php'; //这里请看另一个 "PHP实现色彩空间转换" /** * 图像信息处理 * @author shizhuolin */ class Image { /** * 位图资源 * @var resource */ protected $_image; /** * 使用指定文件构造图像操作实例 * @param string $filename */ public function __construct($filename=null) { if ($filename) $this->open($filename); } /** * 读取指定文件明的图像 * @param string $filename * @return Image */ public function open($filename) { $handle = fopen($filename, "rb"); $contents = stream_get_contents($handle); fclose($handle); $image = imagecreatefromstring($contents); $width = imagesx($image); $height = imagesy($image); $truecolorimage = imagecreatetruecolor($width, $height); imagefilledrectangle($truecolorimage, 0, 0, $width, $height, 0xFFFFFF); imagecopyresampled($truecolorimage, $image, 0, 0, 0, 0, $width, $height, $width, $height); imagedestroy($image); $this->setImage($truecolorimage); return $this; } /** * 读取指定资源的图像 * @param resource $image * @return Image */ public function load($image) { $width = imagesx($image); $height = imagesy($image); $truecolorimage = imagecreatetruecolor($width, $height); imagefilledrectangle($truecolorimage, 0, 0, $width, $height, 0xFFFFFF); imagecopyresampled($truecolorimage, $image, 0, 0, 0, 0, $width, $height, $width, $height); if ($this->getImage()) { imagedestroy($this->getImage()); } $this->setImage($truecolorimage); return $this; } /** * 设置内部图像资源描述符 * @param resource $image */ protected function setImage($image) { $this->_image = $image; } /** * 获取内部操作图像资源 * @return resource */ public function getImage() { return $this->_image; } /** * 获取图像宽度 * @return int */ public function getWidth() { return imagesx($this->getImage()); } /** * 获取图像高度 * @return int */ public function getHeight() { return imagesy($this->getImage()); } /** * 获取指定坐标像素颜色 * @param type $x * @param type $y * @return type */ public function getColorAt($x, $y) { $rgb = imageColorAt($this->getImage(), $x, $y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0…