php实现等比例生成图片缩略图不失真
2016-10-16 23:52
561 查看
//等比例生成图片缩略图不失真
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);
imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);//图像边缘比较平滑.质量较好(但速度比 ImageCopyResized() 慢
}
else{
$newim = imagecreate($newwidth,$newheight);
imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);//在所有GD版本中有效,缩放图像的算法比较粗糙
}
$name = $name.$filetype;
imagejpeg($newim,$name);
imagedestroy($newim);
}
else{
$name = $name.$filetype;
imagejpeg($im,$name);
}
}
$im = imagecreatefromjpeg('1.jpg');
resizeImage($im,250,215,'5','.jpg');
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);
imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);//图像边缘比较平滑.质量较好(但速度比 ImageCopyResized() 慢
}
else{
$newim = imagecreate($newwidth,$newheight);
imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);//在所有GD版本中有效,缩放图像的算法比较粗糙
}
$name = $name.$filetype;
imagejpeg($newim,$name);
imagedestroy($newim);
}
else{
$name = $name.$filetype;
imagejpeg($im,$name);
}
}
$im = imagecreatefromjpeg('1.jpg');
resizeImage($im,250,215,'5','.jpg');
相关文章推荐
- 使用PHP实现生成固定大小图片缩略图功能(智能裁剪,图片不失真)
- 使用PHP实现生成固定大小图片缩略图功能(智能裁剪,图片不失真)
- 在ASP.NET中实现上传图片,并按比例生成缩略图
- 在ASP.NET中实现上传图片,并按比例生成缩略图
- java实现创建缩略图、伸缩图片比例生成的方法
- PHP图片等比例缩放生成缩略图函数分享
- PHP Imagick完美实现图片裁切、生成缩略图、添加水印
- PHP图片缩放函数:实现等比例不失真缩放
- php实现图片上传并利用ImageMagick生成缩略图
- PHP实现原比例生成缩略图的方法
- php实现按指定大小等比缩放生成上传图片缩略图的方法
- php gd2 上传图片/文字水印/图片水印/等比例缩略图/实现代码
- js+php实现的图片选框手动裁剪生成缩略图代码
- php实现上传图片生成缩略图示例
- PHP Imagick完美实现图片裁切、生成缩略图、添加水印
- 在ASP.NET中实现上传图片,并按比例生成缩略图
- 使用gd库实现php服务端图片裁剪和生成缩略图功能分享
- php实现上传图片生成缩略图示例
- 黄聪:php实现上传图片和等比例缩略图压缩图片
- php实现上传图片和等比例缩略图压缩图片