码迷,mamicode.com
首页 > Web开发 > 详细

PHP初级实现图片添加水印

时间:2016-09-12 13:57:00      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

<?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‘);

?>

PHP初级实现图片添加水印

标签:

原文地址:http://www.cnblogs.com/liebedichjbj/p/5864369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!