• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PHP缩略图处理类

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

<?php
/**
 * 缩略图处理类
 *
 * author: silvester   [email protected]  http://www.cnblogs.com/silvester
 */
class Songfeng_Image_Ratio {
	/**
	 * 图片大小比例调整
	 *
	 * @param $filename       图片路径
	 * @param $w              目标宽度
	 * @param $h             目标高度
	 * @param $override       是否覆盖原文件
	 * @param $background  是否产生背景, 如果要求产生背景则产生图像是指定的大小, 图片内容居中
	 * @param $color            背影色
	 */
	public function RatioAdjuct($filename = '', $w = 440, $h = 300, $override = null, $background = null, $color = '0xFFFFFF') {
		list ( $imgWidth, $imgHeight ) = getImageSize ( $filename );
		
		$ratioX = $imgWidth / $w;
		$ratioY = $imgHeight / $h;
		
		if ($ratioX > $ratioY || $ratioX == $ratioY) {
			$dst_w = $w;
			$dst_h = ceil ( $imgHeight / $ratioX );
		} else if ($ratioY > $ratioX) {
			$dst_h = $h;
			$dst_w = ceil ( $imgWidth / $ratioY );
		}
		
		//判断图片类型
		switch (strtolower ( strrchr ( $filename, '.' ) )) {
			case '.jpg' :
			case '.jpeg' :
				$im = imageCreateFromJpeg ( $filename );
				break;
			case '.gif' :
				$im = imageCreateFromGif ( $filename );
				break;
			
			case '.png' :
				$im = imageCreateFromPng ( $filename );
		}
		
		//是否有背景色
		if (null !== $background) {
			//将背景色转换为十进制的红绿蓝值
			$dec = hexdec ( $color );
			$red = 0xFF & ($dec >> 0x10);
			$green = 0xFF & ($dec >> 0x8);
			$blue = 0xFF & $dec;
			
			//居中定位并复制
			$dst_pos = array ('d_x' => 0, 'd_y' => 0 );
			($dst_w == $w) ? ($dst_pos ['d_y'] = (($h - $dst_h) / 2)) : ($dst_pos ['d_x'] = (($w - $dst_w) / 2));
			
			$imBox = imageCreateTrueColor ( $w, $h );
			$color_bg = imageColorAllocate ( $imBox, $red, $green, $blue );
			imageFill ( $imBox, 0, 0, $color_bg );
			imageCopyResized ( $imBox, $im, $dst_pos ['d_x'], $dst_pos ['d_y'], 0, 0, $dst_w, $dst_h, $imgWidth, $imgHeight );
		} else {
			$imBox = imageCreateTrueColor ( $dst_w, $dst_h );
			imageCopyResized ( $imBox, $im, 0, 0, 0, 0, $dst_w, $dst_h, $imgWidth, $imgHeight );
		}
		
		//不替换源图片
		if (null === $override)
			$filename = str_replace ( strrchr ( $filename, '.' ), '', $filename ) . '_thumb.png';
		
		return imagejpeg ( $imBox, $filename ) ? $filename : false;
	}
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php后台操作以及一些减缓服务器压力的问题发布时间:2022-07-10
下一篇:
ZH奶酪:PHP中添加HTML代码的三种方法发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap