缩略图处理类(自动比例缩放, 背景颜色指定)
时间:2009-11-24
来源:互联网
去年写的一个图像比例缩略处理类. 发现自己好长时间没来了, 发点东西, 希望对大家能有用.
使用效果: http://www.dewdew.com.cn/category-internal-9
[code]<?php
/**
* 缩略图处理类
*
* author: songfeng [email protected] http://hi.baidu.com/ccex
*/
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;
}
}
[/code]

作者: hljwxdn 发布时间: 2009-11-24
非等比例呢? 我需要固定的宽高 缩放以后多余的截掉
作者: skxc 发布时间: 2009-11-24
等比例支持,
非等比例? 你是不是要固定图片的大小, 你可以指定图片的大小并指定背景色, 这样就是固定的大小, 其余的部分会用背景色填充.
作者: hljwxdn 发布时间: 2009-11-24
原图:
然后生成100*100缩略图
等比例: 非等比例:
作者: skxc 发布时间: 2009-11-24
作者: skxc 发布时间: 2009-11-24
哦, 我明白你的意思了. 但这样也许会影响原图片内容, 但这也应该是一种需求, 我抽时间把它作为选项加进去.
作者: hljwxdn 发布时间: 2009-11-24
作者: 重楼 发布时间: 2009-11-24
作者: spaceplus 发布时间: 2009-11-25
作者: fengbo7496 发布时间: 2009-11-25
没有问题.
作者: hljwxdn 发布时间: 2009-11-26
作者: greenrock 发布时间: 2009-11-26
作者: mailangel123 发布时间: 2009-11-26
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28