调整图片大小的功能php-img
2016-03-04 09:50
507 查看
<?php
//调整图片大小
$image = "test.jpg";
$max_width = 200;
$max_height = 200;
$size = getimagesize($image);
//得到图像的大小
$width = $size[0];
$height = $size[1];
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if (($width <= $max_width)
&& ($height <=
$max_height))
{
$tn_width =
$width;
$tn_height =
$height;
}
elseif (($x_ratio * $height) <
$max_height)
{
$tn_height =
ceil($x_ratio * $height);
$tn_width =
$max_width;
}
else
{
$tn_width =
ceil($y_ratio * $width);
$tn_height =
$max_height;
}
$src = imagecreatefromjpeg($image);
$dst = imagecreatetruecolor($tn_width, $tn_height);
//新建一个真彩色图像
imagecopyresampled($dst, $src, 0, 0, 0, 0,
$tn_width, $tn_height,
$width, $height);
//重采样拷贝部分图像并调整大小
header('Content-Type: image/jpeg');
imagejpeg($dst,null,100);
imagedestroy($src);
imagedestroy($dst);
?>
//调整图片大小
$image = "test.jpg";
$max_width = 200;
$max_height = 200;
$size = getimagesize($image);
//得到图像的大小
$width = $size[0];
$height = $size[1];
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if (($width <= $max_width)
&& ($height <=
$max_height))
{
$tn_width =
$width;
$tn_height =
$height;
}
elseif (($x_ratio * $height) <
$max_height)
{
$tn_height =
ceil($x_ratio * $height);
$tn_width =
$max_width;
}
else
{
$tn_width =
ceil($y_ratio * $width);
$tn_height =
$max_height;
}
$src = imagecreatefromjpeg($image);
$dst = imagecreatetruecolor($tn_width, $tn_height);
//新建一个真彩色图像
imagecopyresampled($dst, $src, 0, 0, 0, 0,
$tn_width, $tn_height,
$width, $height);
//重采样拷贝部分图像并调整大小
header('Content-Type: image/jpeg');
imagejpeg($dst,null,100);
imagedestroy($src);
imagedestroy($dst);
?>
相关文章推荐
- PHP Socket 编程-socket讲解与实例
- Php session内部执行流程的再…
- 深入浅出PHP下的文件上传
- 结合Mod Rewrite以及PHP生成…
- php里session的用法(超级经典)
- PHP中用SESSION保存对象
- PHP时间转换--最全UTC GMT&nb…
- PHP中看时间函数:return (ti…
- php中$_SERVER[PHP_SELF] 和&…
- PHP include_path设置
- 随笔--php
- php抓取网页内容的方法
- PHP-CGI 进程 CPU …
- PHP对大文件的处理思路
- PHP中extract()函数的妙用
- php学习笔记之--函数的声明与应用
- php 的自动载入
- php在线处理视频 (在线视频转…
- PHP XML操作的各种方法解析(…
- php调用webservices操作