用PHP实现图象锐化代码


Posted in PHP onJune 14, 2007
<?
//读取图像的类型  
//1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF  
function GetImageType($filename) {
	return (($imginfo=@getimagesize($filename))!=null ? $imginfo[2] : null);
}
//图像锐化  
//$scr_im:图像资源句柄,$degree:锐化度数  
function Sharp(&$src_im, &$dst_im, $degree) {
	$src_x = imagesx($src_im);
	$src_y = imagesy($src_im);
	//$dst_im = imagecreate($src_x, $src_y);  
	//imagecopy($dst_im, $src_im, 0, 0, 0, 0, $src_x, $src_y);  
	$cnt = 0;
	for ($x=1; $x<$src_x; $x++)  
	 for ($y=1; $y<$src_y; $y++) {
		$src_clr1 = imagecolorsforindex($src_im, imagecolorat($src_im, $x-1, $y-1));
		$src_clr2 = imagecolorsforindex($src_im, imagecolorat($src_im, $x, $y));
		$r = intval($src_clr2["red"]+$degree*($src_clr2["red"]-$src_clr1["red"]));
		$g = intval($src_clr2["green"]+$degree*($src_clr2["green"]-$src_clr1["green"]));
		$b = intval($src_clr2["blue"]+$degree*($src_clr2["blue"]-$src_clr1["blue"]));
		$r = min(255, max($r, 0));
		$g = min(255, max($g, 0));
		$b = min(255, max($b, 0));
		//echo "r:$r, g:$g, b:$b";  
		if (($dst_clr=imagecolorexact($dst_im, $r, $g, $b))==-1)  
		 $dst_clr = Imagecolorallocate($dst_im, $r, $g, $b);
		$cnt++;
		if ($dst_clr==-1) die("color allocate faile at $x, $y ($cnt).");
		imagesetpixel($dst_im, $x, $y, $dst_clr);
	}
	return $dst_im;
}
$ImageFunctions = array("imagecreatefromwbmp", "imagecreatefromgif", "imagecreatefromjpeg", "imagecreatefrompng");
if (!empty($_POST["ImageName"])) {
	set_time_limit(10*60);
	if (($ImageType=GetImageType($_POST["ImageName"]))==false)  
	 die("指定文件不存在或不是有效的图片或不支持类型!");
	if ($ImageType==6) $ImageType = 0;
	if ($ImageType>3) die("不支持的图片类型!");
	$im1 = $ImageFunctions[$ImageType]($_POST["ImageName"]);
	$im2 = $ImageFunctions[$ImageType]($_POST["ImageName"]);
	//print_r(imagecolorsforindex($im, imagecolorat($im, 10, 10)));  
	Sharp($im1, $im2, $_POST["Degree"]);
	header("Content-type: image/png");
	imagepng($im2);
	imagedestroy($im1);
	imagedestroy($im2);
}
?>
function Sharp2(&$im, $degree) {
	$cnt = 0;
	for ($x=imagesx($im)-1; $x>0; $x--)  
	 for ($y=imagesy($im)-1; $y>0; $y--) {
		$clr1 = imagecolorsforindex($im, imagecolorat($im, $x-1, $y-1));
		$clr2 = imagecolorsforindex($im, imagecolorat($im, $x, $y));
		$r = intval($clr2["red"]+$degree*($clr2["red"]-$clr1["red"]));
		$g = intval($clr2["green"]+$degree*($clr2["green"]-$clr1["green"]));
		$b = intval($clr2["blue"]+$degree*($clr2["blue"]-$clr1["blue"]));
		$r = min(255, max($r, 0));
		$g = min(255, max($g, 0));
		$b = min(255, max($b, 0));
		//echo "r:$r, g:$g, b:$b
		";  
 if (($new_clr=imagecolorexact($im, $r, $g, $b))==-1)  
 $new_clr = Imagecolorallocate($im, $r, $g, $b);  
 $cnt++;  
 if ($new_clr==-1) die("color allocate faile at $x, $y ($cnt).");  
 imagesetpixel($im, $x, $y, $new_clr);  
 }  
}
PHP 相关文章推荐
PHP脚本的10个技巧(2)
Oct 09 PHP
Windows下的PHP5.0详解
Nov 18 PHP
PHP求最大子序列和的算法实现
Jun 24 PHP
PHP中PDO的错误处理
Sep 04 PHP
在win系统安装配置 Memcached for PHP 5.3 图文教程
Mar 03 PHP
php利用反射实现插件机制的方法
Mar 14 PHP
PHP函数checkdnsrr用法详解(Windows平台用法)
Mar 21 PHP
Yii2实现同时搜索多个字段的方法
Aug 10 PHP
thinkPHP5.0框架配置格式、加载解析与读取方法
Mar 17 PHP
php实现微信企业号支付个人的方法详解
Jul 26 PHP
PHP环形链表实现方法示例
Sep 15 PHP
PHP多进程简单实例小结
Nov 09 PHP
PHP静态新闻列表自动生成代码
Jun 14 #PHP
discuz安全提问算法
Jun 06 #PHP
PHP5.2下chunk_split()函数整数溢出漏洞 分析
Jun 06 #PHP
phpMyAdmin下载、安装和使用入门教程
May 31 #PHP
测试您的 PHP 水平的题目
May 30 #PHP
Discuz板块横排显示图片的实现方法
May 28 #PHP
中国站长站 For Dede4.0 采集规则
May 27 #PHP
You might like
php 数组的指针操作实现代码
2011/02/08 PHP
百度站点地图(百度sitemap)生成方法分享
2014/01/09 PHP
PHP积分兑换接口实例
2015/02/09 PHP
Zend Framework教程之Application和Bootstrap用法详解
2016/03/10 PHP
浅谈PHP检查数组中是否存在某个值 in_array 函数
2016/06/13 PHP
PHP实现微信模拟登陆并给用户发送消息的方法【文字,图片,图文】
2017/06/29 PHP
javascript比较文档位置
2008/04/08 Javascript
JS实用的动画弹出层效果实例
2015/05/05 Javascript
几种经典排序算法的JS实现方法
2016/03/25 Javascript
JavaScript实现简单Tip提示框效果
2016/04/20 Javascript
JavaScript原生编写《飞机大战坦克》游戏完整实例
2017/01/04 Javascript
利用n工具轻松管理Node.js的版本
2017/04/21 Javascript
Vue基于NUXT的SSR详解
2017/10/24 Javascript
利用百度地图API获取当前位置信息的实例
2017/11/06 Javascript
vue2.0 循环遍历加载不同图片的方法
2018/03/06 Javascript
解决vue项目打包后提示图片文件路径错误的问题
2018/07/04 Javascript
JS 实现获取验证码 倒计时功能
2018/10/29 Javascript
微信小程序实现导航栏和内容上下联动功能代码
2020/06/29 Javascript
原生JavaScript实现购物车
2021/01/10 Javascript
[01:00:30]TFT vs VGJ.T Supermajor 败者组 BO3 第一场 6.5
2018/06/06 DOTA
Caffe均值文件mean.binaryproto转mean.npy的方法
2018/07/09 Python
python爬虫爬取幽默笑话网站
2019/10/24 Python
使用Keras预训练模型ResNet50进行图像分类方式
2020/05/23 Python
深入理解Python 多线程
2020/06/16 Python
加拿大最大的箱包及旅游配件零售商:Bentley Leathers
2017/07/19 全球购物
蒂芙尼澳大利亚官方网站:Tiffany&Co. Australia
2017/08/27 全球购物
美国婴儿和儿童家具网上商店:ABaby.com
2018/07/02 全球购物
普通PHP程序员笔试题
2016/01/01 面试题
我们的节日端午节活动方案
2014/03/02 职场文书
2014年党务公开方案
2014/05/08 职场文书
单位介绍信格式
2015/01/31 职场文书
2015年底工作总结范文
2015/05/15 职场文书
资产移交协议书
2016/03/24 职场文书
导游词之无锡丝业博物馆
2019/11/12 职场文书
详解MySQL的主键查询为什么这么快
2022/04/03 MySQL
pd.DataFrame中的几种索引变换的实现
2022/06/16 Python