• 微信

PHP实例代码:使用GD函数库制作缩略图+保存本地

作者:Alpha时间:2018-11-05 阅读数:3012 +人阅读
<?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);
}


本站所有文章、数据、图片均来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:595397166@qq.com

标签: 小程序 小程序

阿尔法

软件开发工程师#全栈工程师

{include file=foot.html}