01 | 
			
				function resizeImage($im,$maxwidth,$maxheight,$name,$filetype) | 
		
				02 | 
			
				{ | 
		
				03 | 
			
				    $pic_width = imagesx($im); | 
		
				04 | 
			
				    $pic_height = imagesy($im); | 
		
				05 | 
			
				06 | 
			
				    if(($maxwidth && $pic_width > $maxwidth) || ($maxheight &&$pic_height > $maxheight)) | 
		
				07 | 
			
				    { | 
		
				08 | 
			
				        if($maxwidth && $pic_width>$maxwidth) | 
		
				09 | 
			
				        { | 
		
				10 | 
			
				            $widthratio = $maxwidth/$pic_width; | 
		
				11 | 
			
				            $resizewidth_tag = true; | 
		
				12 | 
			
				        } | 
		
				13 | 
			
				14 | 
			
				        if($maxheight && $pic_height>$maxheight) | 
		
				15 | 
			
				        { | 
		
				16 | 
			
				            $heightratio = $maxheight/$pic_height; | 
		
				17 | 
			
				            $resizeheight_tag = true; | 
		
				18 | 
			
				        } | 
		
				19 | 
			
				20 | 
			
				        if($resizewidth_tag && $resizeheight_tag) | 
		
				21 | 
			
				        { | 
		
				22 | 
			
				            if($widthratio<$heightratio) | 
		
				23 | 
			
				                $ratio = $widthratio; | 
		
				24 | 
			
				            else | 
		
				25 | 
			
				                $ratio = $heightratio; | 
		
				26 | 
			
				        } | 
		
				27 | 
			
				28 | 
			
				        if($resizewidth_tag && !$resizeheight_tag) | 
		
				29 | 
			
				            $ratio = $widthratio; | 
		
				30 | 
			
				        if($resizeheight_tag && !$resizewidth_tag) | 
		
				31 | 
			
				            $ratio = $heightratio; | 
		
				32 | 
			
				33 | 
			
				        $newwidth = $pic_width * $ratio; | 
		
				34 | 
			
				        $newheight = $pic_height * $ratio; | 
		
				35 | 
			
				36 | 
			
				        if(function_exists("imagecopyresampled")) | 
		
				37 | 
			
				        { | 
		
				38 | 
			
				            $newim = imagecreatetruecolor($newwidth,$newheight); | 
		
				39 | 
			
				           imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height); | 
		
				40 | 
			
				        } | 
		
				41 | 
			
				        else | 
		
				42 | 
			
				        { | 
		
				43 | 
			
				            $newim = imagecreate($newwidth,$newheight); | 
		
				44 | 
			
				           imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height); | 
		
				45 | 
			
				        } | 
		
				46 | 
			
				47 | 
			
				        $name = $name.$filetype; | 
		
				48 | 
			
				        imagejpeg($newim,$name); | 
		
				49 | 
			
				        imagedestroy($newim); | 
		
				50 | 
			
				    } | 
		
				51 | 
			
				    else | 
		
				52 | 
			
				    { | 
		
				53 | 
			
				        $name = $name.$filetype; | 
		
				54 | 
			
				        imagejpeg($im,$name); | 
		
				55 | 
			
				    }           | 
		
				56 | 
			
				} | 
		
参数说明:
$im 图片对象,应用函数之前,你需要用imagecreatefromjpeg()读取图片对象,如果PHP环境支持PNG,GIF,也可使用imagecreatefromgif(),imagecreatefrompng();
$maxwidth 定义生成图片的最大宽度(单位:像素)
$maxheight 生成图片的最大高度(单位:像素)
$name 生成的图片名
$filetype 最终生成的图片类型(.jpg/.png/.gif)
代码注释:
第3~4行:读取需要缩放的图片实际宽高
第8~26行:通过计算实际图片宽高与需要生成图片的宽高的压缩比例最终得出进行图片缩放是根据宽度还是高度进行缩放,当前程序是根据宽度进行图片缩放。如果你想根据高度进行图片缩放,你可以将第22行的语句改成$widthratio>$heightratio
第28~31行:如果实际图片的长度或者宽度小于规定生成图片的长度或者宽度,则要么根据长度进行图片缩放,要么根据宽度进行图片缩放。
第33~34行:计算最终缩放生成的图片长宽。
第36~45行:根据计算出的最终生成图片的长宽改变图片大小,有两种改变图片大小的方法:ImageCopyResized()函数在所有GD版本中有效,但其缩放图像的算法比较粗糙。ImageCopyResamples(),其像素插值算法得到的图像边缘比较平滑,但该函数的速度比ImageCopyResized()慢。
第47~49行:最终生成经过处理后的图片,如果你需要生成GIF或PNG,你需要将imagejpeg()函数改成imagegif()或imagepng()
第51~56行:如果实际图片的长宽小于规定生成的图片长宽,则保持图片原样,同理,如果你需要生成GIF或PNG,你需要将imagejpeg()函数改成imagegif()或imagepng()。


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)