PHP初級實現圖片添加浮水印

來源:互聯網
上載者:User

標籤:

<?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初級實現圖片添加浮水印

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.