用php程序能消除水印吗,php——水印
很多開發(fā)語言都會實(shí)現(xiàn)水印功能,之前我用OC和swift都實(shí)現(xiàn)過,最近學(xué)下php,順便練下手,記錄下以便查閱。
//水印
function water($source,$water = '1_jaccty.png',$position = 0,$alpha = 100,$type = "jpeg",$path = 'waterTest',$isRandName = true){
//打開圖片
$sourceRes = open($source);
$waterRes = open($water);
//獲取原圖寬高
$source_info = getimagesize($source);
$source_w = $source_info[0];
$source_h = $source_info[1];
//獲取水印的寬高
$water_info = getimagesize($water);
$water_w = $water_info[0];
$water_h = $water_info[1];
//計(jì)算位置
$image_slipw = $source_w / 3;
$image_sliph = $source_h / 3;
$position_x = ($position % 3-1)*$image_slipw;
$position_y = floor($position / 3)*$image_sliph;
if ($position!=0) {
$x = mt_rand($position_x,$position_x+$image_slipw-$water_w);
$y = mt_rand($position_y,$position_y+$image_sliph-$water_h);
}else{
$x = mt_rand(0,$source_w-$water_w);
$y = mt_rand(0,$source_h-$water_h);
}
//合并
imagecopymerge($sourceRes, $waterRes, $x, $y, 0, 0, $water_w, $water_h, $alpha);
$imageType = 'image'.$type;//根據(jù)$type來調(diào)用哪個(gè)函數(shù)
/*
imagepng();
imagejpeg();
imagewbmp();
*/
//處理path問題
if ($isRandName) {
$name = uniqid().'.'.$type;
}else{
$pathInfo = pathinfo($source);
$name = $pathInfo['filename'].'.'.$type;
}
$path = rtrim($path,'/').'/'.$name;//防止已有/斜線
$imageType($sourceRes,$path);//畫圖
imagedestroy($sourceRes);
imagedestroy($waterRes);
}
//打開圖片的函數(shù)
function open($path){
if (!file_exists($path)) {
exit('文件不存在');
}
$info = getimagesize($path);
switch ($info['mime']) {
case 'image/jpeg':
case 'image/jpg':
case 'image/pjpeg':
$res = imagecreatefromjpeg($path);
break;
case 'image/png':
$res = imagecreatefrompng($path);
break;
case 'image/gif':
$res = imagecreatefromgif($path);
break;
case 'image/wbmp':
case 'image/bmp':
$res = imagecreatefromwbmp($path);
break;
}
return $res;
}
$source = "1.jpg";
water($source);
?>
總結(jié)
以上是生活随笔為你收集整理的用php程序能消除水印吗,php——水印的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信用卡起诉了还能协商还款吗
- 下一篇: 国防科大体检不合格被退学怎么办