<?php header("Content-type: image/jpeg"); //原始映像 $dst = "images/flower_1.jpg"; //得到原始圖片資訊 $dst_im = imagecreatefromjpeg($dst); $dst_info = getimagesize($dst); //浮水印映像 $src = "images/logo.gif"; $src_im = imagecreatefromgif($src); $src_info = getimagesize($src); //浮水印透明度 $alpha = 30; //合并浮水印圖片 imagecopymerge($dst_im,$src_im,$dst_info[0]-$src_info[0],$dst_info[1]-$src_info[1],0,0,$src_info[0], $src_info[1],$alpha); //輸出合并後浮水印圖片 imagejpeg($dst_im); imagedestroy($dst_im); imagedestroy($src_im); ?> 新版本之後imagecopymerge函數幾乎不使用了,我們可直接使用imagecopy來產生浮水印兩個函數的功能是完全一樣的。 //增加浮水印 $watermark =1; $watertype =2; $waterstring =''; $waterimg="z.png"; //浮水印圖片 $sFilePath ='aa.jpg'; if($watermark==1) { $image_size = getimagesize($sFilePath); //上傳的圖片 $water_size = getimagesize($waterimg); //浮水印圖片 $iinfo=getimagesize($sFilePath,$iinfo); $nimage=imagecreatetruecolor($image_size[0],$image_size[1]); $white=imagecolorallocate($nimage,255,255,255); $black=imagecolorallocate($nimage,0,0,0); $red=imagecolorallocate($nimage,255,0,0); imagefill($nimage,0,0,$white); switch ($iinfo[2]) { case 1: $simage =imagecreatefromgif($sFilePath); break; case 2: $simage =imagecreatefromjpeg($sFilePath); break; case 3: $simage =imagecreatefrompng($sFilePath); break; // case 6: // $simage =imagecreatefromwbmp($sFilePath); // break; default: die("不支援的檔案類型"); exit; } imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]); switch($watertype) { case 1: //加浮水印字串 imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black); break; case 2: //加浮水印圖片 $simage1 =imagecreatefrompng($waterimg); $x= $image_size[0]-$water_size[0]; $y= $image_size[1]-$water_size[1]; imagecopy($nimage,$simage1,$x,$y,0,0,240,65); imagedestroy($simage1); break; } switch ($iinfo[2]) { case 1: imagegif($nimage, $sFilePath); // imagejpeg($nimage, $sFilePath); break; case 2: imagejpeg($nimage, $sFilePath); break; case 3: imagepng($nimage, $sFilePath); break; // case 6: // imagewbmp($nimage, $sFilePath); // //imagejpeg($nimage, $sFilePath); // break; } //覆蓋原上傳檔案 imagedestroy($nimage); imagedestroy($simage); } |