1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 等比缩放公式_PHP图像等比缩放代码

等比缩放公式_PHP图像等比缩放代码

时间:2020-08-04 16:36:40

相关推荐

等比缩放公式_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);

?>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。