0515---第17次PHP作业----缩略图

function thumb($sourceImgPath,$wid="230",$hei="230") {

$rootPath = dirname(dirname(__FILE__));

$newPath = $rootPath.$sourceImgPath;
$sourceImgInfo=getimagesize($newPath);
$s_w=$sourceImgInfo[0];//原图宽
$s_h=$sourceImgInfo[1];//原图高
switch ($sourceImgInfo[2]){
    case 1:
        $S_img=imagecreatefromgif($newPath);
        break;
    case 2:
        $S_img=imagecreatefromjpeg($newPath);
        break;
    case 3:
        $S_img=  imagecreatefrompng($newPath);
        break;

}

$t_img=imagecreatetruecolor($wid, $hei);
$bgcolor=imagecolorallocate($t_img, 255, 255, 255);
imagefill($t_img, 0, 0,$bgcolor);

$z_w=$wid/$s_w;
$z_h=$hei/$s_h;
$zoom=min($z_w,$z_h);
$new_w=$zoom*$s_w;
$new_h=$zoom*$s_h;


imagecopyresized($t_img, $S_img,($wid-$new_w)/2,($hei-$new_h)/2,0,0,$new_w,$new_h, $s_w, $s_h);
        //$t_img缩略图画布
        //$s_img:原图
        //X1,Y1 缩略图图像开始位置(0,0)    
        //X2,Y2 原图从什么位置开始取图像(0,0)
        //X3,Y3 缩略图图像结束位置($wid,$hei)
        //X4,Y4  原图从什么位置结束取图像(s_w,s_h)

     //header("content-type:image/png");
    
     
       $p=explode('.',$newPath);
    $ext = array_pop($p);
   
 
 $newName = MD5(uniqid()).'.'.$ext;   
 
    $PathName =$rootPath. '/upload/sincopar/';
    if (!is_dir($PathName)) {
        if (!mkdir($PathName, 0777, true)) {
            $_SESSION['fileerror']= 13;
            header('location:../after_end/info.php');
            exit();
        }
    }
    $imagepng = imagepng($t_img,$PathName.$newName);
    imagedestroy($t_img);
    imagedestroy($S_img);
    return '/upload/sincopar/'.$newName;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值