PHP初级实现图片添加水印

<?php
function water($dstpath,$srcpath,$pos = 1,$tmd = 80,$pre = "w_"){
   $dstinfo = getimageinfo($dstpath);
   switch($dstinfo['type']){
          case 1:
             $dstim = imagecreatefromgif($dstpath);
             break;
          case 2:
             $dstim = imagecreatefromjpeg($dstpath);
             break;
          case 3:
             $dstim = imagecreatefrompng($dstpath);
             break;
   }
   $srcinfo = getimageinfo($srcpath);
   switch($srcinfo['type']){
           case 1:
              $srcim = imagecreatefromgif($srcpath);
              break;
           case 2:
              $srcim = imagecreatefromjpeg($srcpath);
              break;
           case 3:
              $srcim = imagecreatefrompng($srcpath);
              break;
   }

    switch($pos){
        case 1:
            $x = 0;
            $y = 0;
            break;
        case 2:
            $x = $dstinfo['width']/2-$srcinfo['width']/2;
            $y = 0;
            break;
        case 3:
            $x = $dstinfo['width']-$srcinfo['width'];
            $y = 0;
            break;
        case 4;
             $x = 0;
             $y = $dstinfo['height']/2-$srcinfo['height']/2;
             break;

    }
    imagecopymerge($dstim,$srcim,$x,$y,0,0,$srcinfo['width'],$srcinfo['height'],$tmd);

     $p= pathinfo($dstpath);
     $picname = $p['dirname'].'/'.$pre.$p['basename'];

     switch($dstinfo['type']){
         case 1:
             imagegif($dstim,$picname);
             break;
         case 2;
              imagejpeg($dstim,$picname);
              break;
         case 3:
              imagepng($dstim,$picname);
              break;
     }  


     imagedestroy($dstim);
     imagedestroy($srcim);
     return $picname;
}

function getimageinfo($picpath){
     $info = getimagesize($picpath);
     $imginfo['width']=$info[0];
     $imginfo['height']=$info[1];
     $imginfo['type']=$info[2];
     $imginfo['mime']=$info['mime'];
     return $imginfo;
}

water('Meinv085.jpg','100.jpg');

?>

posted @ 2016-09-12 12:30  从哪来到哪去  阅读(170)  评论(0编辑  收藏  举报