php等比例压缩图片
2016-08-19 20:29
411 查看
<?php function resizeImage($im,$maxwidth,$maxheight,$name,$filetype) { $pic_width = imagesx($im); $pic_height = imagesy($im); if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) { if($maxwidth && $pic_width>$maxwidth) { $widthratio = $maxwidth/$pic_width; $resizewidth_tag = true; } if($maxheight && $pic_height>$maxheight) { $heightratio = $maxheight/$pic_height; $resizeheight_tag = true; } if($resizewidth_tag && $resizeheight_tag) { if($widthratio<$heightratio) $ratio = $widthratio; else $ratio = $heightratio; } if($resizewidth_tag && !$resizeheight_tag) $ratio = $widthratio; if($resizeheight_tag && !$resizewidth_tag) $ratio = $heightratio; $newwidth = $pic_width * $ratio; $newheight = $pic_height * $ratio; if(function_exists("imagecopyresampled")) { $newim = imagecreatetruecolor($newwidth,$newheight);//PHP系统函数 imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);//PHP系统函数 } else { $newim = imagecreate($newwidth,$newheight); imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height); } $name = $name.$filetype; imagejpeg($newim,$name); imagedestroy($newim); } else { $name = $name.$filetype; imagejpeg($im,$name); } } //使用方法: $im=imagecreatefromjpeg("./20140416103023202.jpg");//参数是图片的存方路径 $maxwidth="600";//设置图片的最大宽度 $maxheight="400";//设置图片的最大高度 $name="123";//图片的名称,随便取吧 $filetype=".jpg";//图片类型 resizeImage($im,$maxwidth,$maxheight,$name,$filetype);//调用上面的函数
网上摘抄,还没做测试
相关文章推荐
- php等比例压缩图片【原创】
- php gd等比例缩放压缩图片函数
- php图片等比例压缩
- php实现上传图片和等比例缩略图压缩图片
- PHP等比例压缩图片的实例代码
- PHP 按一定比例压缩图片,保持清晰度
- 黄聪:php实现上传图片和等比例缩略图压缩图片
- php gd等比例缩放压缩图片函数
- PHP 图片操作(按照指定尺寸压缩,按照比例裁剪)
- php实现等比例压缩图片
- php等比例压缩图片
- PHP等比例缩放图片大小压缩图片空间
- PHP 按一定比例压缩图片,保持清晰度
- PHP图片处理函数 类 (水印图,缩略图)[关于等比例压缩与裁剪压缩]
- php等比例压缩图片(jpg,jif,png图片等比例压缩)
- PHP按一定比例压缩图片的方法
- 图片等比例压缩方法
- php上传图片和等比例缩略图图片的例子
- PHP图片缩放函数:实现等比例不失真缩放
- php图像处理类,上传,压缩,添加文字、图片水印