zoukankan      html  css  js  c++  java
  • 图像处理_等比例居中裁剪图片_可添加水印

     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);
     
  • 相关阅读:
    [转]ASP.Net+XML打造留言薄
    [导入]如何构造一个C#语言的爬虫蜘蛛程序
    [导入]CSS基本布局16例
    [导入]ASP.NET26个常用性能优化方法
    javascript控制cookies及在跳出本页给出提示,是否放弃操作!!
    RunOnBeforeUnload()
    [导入]网易娱乐频道也在用风讯CMS
    etcd集群配置
    openstack上传镜像
    Ambari 节点坏掉不要的节点 无法删除解决方法
  • 原文地址:https://www.cnblogs.com/huangcaijin/p/13064502.html
Copyright © 2011-2022 走看看