您的位置:首页 > 编程语言 > PHP开发

调整图片大小的功能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);
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: