<?php
// $name="./img/1.jpg";
// $w=300;
// $h=300;
// $prefix='thumb_111';
//缩略前缀,原图保留,缩略图也要保留
//获取相关参数,计算比例
function suolue($name, $w,$h,$prefix='thumb_111'){
function getinfo($name){
//getimagesize($dname)函数可以动态的获取图片类型,大小,宽度和高度等
$info=getimagesize($name);
$width=$info[0];
$height=$info[1];
$mime=$info['mime'];
switch ($mime) {
case 'image/jpeg':
$res=imagecreatefromjpeg($name);
break;
case 'image/gif':
$res=imagecreatefromgif($name);
break;
case 'image/png':
$res=imagecreatefrompng($name);
break;
case 'image/wbmp':
$res=imagecreatefromwbmp($name);
break;
}
return array('width'=>$width,'height'=>$height,'res'=>$res);
}
$info=getinfo($name);
$width=$info['width'];
$height=$info['height'];
$res=$info['res'];
//判断图片是横图还是竖图
if($width>$height){
//横图
$b1=$height/$width;
$h=$w*$b1;
}else{
//竖图
$b1=$width/$height;
$w=$h*$b1;
}
//3,创建一个画布,画布的宽度,高度为要锁房的宽度和高度
$img=imagecreatetruecolor($w,$h);
//4,开始把图片重新设置大小
imagecopyresampled($img, $res, 0, 0, 0, 0, $w, $h, $width, $height);
//5,输出图像
// header("content-type:image/png");
//5,会趋于扩展名,生成新文件名,保存图像
$ext=pathinfo($name,PATHINFO_EXTENSION);
$rand_name=$prefix.md5(time().mt_rand()).".".$ext;
// imagepng($img);
switch(strtolower($ext)){
case 'jpg':
case 'jpeg':
case 'jpe':
imagejpeg($img,$rand_name);
break;
case 'png':
imagepng($img,$rand_name);
break;
case 'gif':
imagegif($img,$rand_name);
break;
case 'bmp':
case 'wbmp':
imagewbmp($img,$rand_name);
break;
}
//销毁资源
imagedestroy($res);
imagedestroy($img);
}
PHP使用GD函数库制作缩略图+保存本地
最新推荐文章于 2023-08-02 08:46:52 发布