canva画图 图片居中裁剪_图像处理_等比例居中裁剪图片_可添加水印

1 //参数:图片路径,生成的宽度,生成的高度2 //功能:切片图片不影响比例居中切片支持jpg,jpeg,png,gif,wbmp格式图片3 function imgthumb($url,$w,$h,$str="",$print){4 try{5 // if(!file_exists($url))6 // {7 // throw new Exception('文件不存在!');8 // }9 $ext=pathinfo($url,PATHINFO_EXTENSION);10 switch($ext){11 case 'jpg':12 case 'jpeg':13 $im=imagecreatefromjpeg($url);14 break;15 case 'png':16 $im=imagecreatefrompng($url);17 break;18 case 'gif':19 $im=imagecreatefromgif($url);20 break;21 case 'wbmp':22 $im=imagecreatefromwbmp($url);23 break;24 default:25 die('文件格式不支持');26 }27

28 $imarr=getimagesize($url);29 $height=$imarr[1];30 $width=$imarr[0];31 $pro=$height / $width;32 $targetpro=$h / $w;33 $trueimg=imagecreatetruecolor($w,$h);34 if($targetpro>$pro)35 {36 $ws=$h/$pro;37 $x=($ws-$w)/2;38 imagecopyresampled($trueimg,$im,0,0,$x,0,$ws,$h,$width,$height);39 }40 else{41 $hs=$w*$pro;42 $y=-($h-$hs)/2;43 imagecopyresampled($trueimg,$im,0,0,0,$y,$w,$hs,$width,$height);44 }45 if($print)46 {47 $trueimg=imgprint($trueimg,$str);48 }49 header('content-type:'.$imarr['mime']);50 switch($ext){51 case 'jpg':52 case 'jpeg':53 case 'png':54 imagejpeg($trueimg,null,100);55 break;56

57 imagepng($trueimg);58 break;59 case 'gif':60 imagegif($trueimg);61 break;62 case 'wbmp':63 image2wbmp($trueimg);64 break;65 default:66 return false;67 }68 imagedestroy($trueimg);69 }catch(Exception $e){70 echo $e->getMessage();71 }72 }73 function imgprint($to,$str)74 {75 try{76

77 // 建立一幅的图像78 $im = imagecreate(200,40);79 // 白色背景和蓝色文本80 $bg = imagecolorallocate($im, 255, 255, 255);81 $textcolor = imagecolorallocate($im, 255, 0, 0);82 // 把字符串写在图像左上角83 imagestring($im,5, 30, 10, $str, $textcolor);84 #$toimg=imagecreatefromjpeg($to);85 $toimg=$to;86 imagecopymerge($toimg,$im,imagesx($toimg)-imagesx($im),imagesy($toimg)-imagesy($im),0,0,200,40,50);//修改位置87 return $toimg;88 #header('content-type:'.getimagesize($to)['mime']);89 #imagejpeg($toimg,null,100);90 }catch(Exception $e){91 echo $e->getMessage();92 }93 }94 $img=$_REQUEST['imgpath'];95 $x=isset($_REQUEST['x'])?$_REQUEST['x']:300;96 $y=isset($_REQUEST['y'])?$_REQUEST['y']:300;97 $print=isset($_REQUEST['print'])?$_REQUEST['print']:false;98 $str='xxxx';99 imgthumb($img,$x,$y,$str,$print);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值