PHP 图像尺寸调整代码
作者:bea
代码如下: /********************** *@filename - path to the image *@tmpname - temporary path to thumbnail *@xmax - max width *@ymax - max height */ function resize_image($filename, $tmpname, $xmax, $ymax) { $ext = explode(".", $filena
代码如下:
/**********************
*@filename - path to the image
*@tmpname - temporary path to thumbnail
*@xmax - max width
*@ymax - max height
*/
function resize_image($filename, $tmpname, $xmax, $ymax)
{
$ext = explode(".", $filename);
$ext = $ext[count($ext)-1];
if($ext == "jpg" || $ext == "jpeg")
$im = imagecreatefromjpeg($tmpname);
elseif($ext == "png")
$im = imagecreatefrompng($tmpname);
elseif($ext == "gif")
$im = imagecreatefromgif($tmpname);
$x = imagesx($im);
$y = imagesy($im);
if($x <= $xmax && $y <= $ymax)
return $im;
if($x >= $y) {
$newx = $xmax;
$newy = $newx * $y / $x;
}
else {
$newy = $ymax;
$newx = $x / $y * $newy;
}
$im2 = imagecreatetruecolor($newx, $newy);
imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
return $im2;
}
这里是摘自之前发布的文章。更多的技巧可以参考。
收集的二十一个实用便利的PHP函数代码
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- php 验证码实例代码
- php trim 去除空字符的定义与语法介绍
- phpmyadmin 3.4 空密码登录的实现方法
- Discuz!下Memcache缓存实现方法
- 备份mysql数据库的php代码(一个表一个文件)
- php下将图片以二进制存入mysql数据库中并显示的实现代码
- php set_time_limit(0) 设置程序执行时间的函数
- PHP 可阅读随机字符串代码
- PHP 一个随机字符串生成代码
- PHP 循环列出目录内容的函数代码
- PHP 删除一个目录及目录下的所有文件的函数代码
- PHP JSON 数据解析代码
- PHP XML数据解析代码
- PHP 日志缩略名的创建函数代码
- PHP similar_text 字符串的相似性比较函数
- PHP Zip解压 文件在线解压缩的函数代码
- PHP Zip压缩 在线对文件进行压缩的函数
- PHP为表单获取的URL 地址预设 http 字符串函数代码
- PHP 强制性文件下载功能的函数代码(任意文件格式)