PHP图像等比缩放代码

PHP图像等比缩放代码

<?php
/*
* 图像的等比缩放
* @param $FileName 原图片文件
* @param $Width 目标片的宽度
* @param $Height 目标片的高度
*
*/
//图像的等比缩放
function thumb($FileName,$Width,$Height){
//list遍历出getimagesize数组的值!
//$srcWidth 原图宽度
//$srcHieght 原图的高度
//$type 原图的类型
list($srcWidth,$srcHeight,$type)=getimagesize($FileName);
$imageType=array(1=>’gif’,2=>’jpeg’,3=>’png’);
//使用变量函数
$from = ‘imagecreatefrom’.$imageType[$type];
$out = ‘image’.$imageType[$type];
//等比缩放固定公式固定的公式
if($Width && ($srcWidth < $srcHeight)){
$Width = floor(($Height/$srcHeight)*$srcWidth);
}else{
$Height = floor(($Width/$srcWidth)*$srcHeight);
}
$srcimg = $from($FileName);
$desimg = imagecreatetruecolor($Width,$Height);
//关键的缩放代码
imagecopyresampled($desimg,$srcimg,0,0,0,0,$Width,$Height,$srcWidth,$srcHeight);
//输出缩放后的代码
header(“content-type:image/{$imageType[$type]}”);
$out($desimg);
$out($desimg,’y_’.ltrim($FileName,’./’));
//销毁原图片和输出后的图片
imagedestroy($desimg);
imagedestroy($srcimg);

}
thumb(‘./aaa.jpg’,500,600);
?>

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据