PHP简单图片水印

简单水印效果,注释掉的部分是调整透明度的,但是效果不太好…

<?php
//function watermark($imageType,$imageFile,$logoFile,$alpha=0.5){
function watermark($imageType,$imageFile,$logoFile){
    switch($imageType){
        case 'png':$image=imagecreatefrompng($imageFile);break;
        case 'jpg':$image=imagecreatefromjpeg($imageFile);break;
        case 'gif':$image=imagecreatefromgif($imageFile);break;
    }
    $logo=imagecreatefrompng($logoFile);
    $imageWidth=imagesx($image);
    $imageHeight=imagesy($image);
    $logoWidth=imagesx($logo);
    $logoHidth=imagesy($logo);
    //透明通道
    imagealphablending($image,true);
    //添加水印
    imagecopy($image,$logo,$imageWidth-$logoWidth,$imageHeight-$logoHidth,0,0,$logoWidth,$logoHidth);
    //imagecopymerge($image,$logo,$imageWidth-$logoWidth,$imageHeight-$logoHidth,0,0,$logoWidth,$logoHidth,50);
    //销毁
    imagedestroy($logo);
    return $image;
}

//判断上传
if(is_uploaded_file($_FILES['image']['tmp_name']) & is_uploaded_file($_FILES['logo']['tmp_name'])){
    //图片格式
    switch($_FILES['image']['type']){
        case 'image/png':case 'image/x-png':$type='png';break;
        case 'image/jpeg':case 'image/pjpeg':$type='jpg';break;
        case 'image/gif':$type='gif';break;
        default:return;
    }
    $image=watermark($type,$_FILES['image']['tmp_name'],$logoFIle=$_FILES['logo']['tmp_name']);
    //输出图片
    header("Content-Type: image/{$type}");
    switch($type){
        case 'png':imagepng($image);
        case 'jpg':imagejpeg($image);
        case 'gif':imagegif($image);
    }
    //销毁
    imagedestroy($image);
}else{
?>
<!DOCTYPE html>
<html>
<head>
    <title>Watermark</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
    image(PNG, GIF or JPG):<input type="file" name="image"/><br/>
    logo(PNG):<input type="file" name="logo"/><br/>
    <input type="submit" value="upload"/>
</form>
</body>
</html>
<?php
}
?>

因为图片是只输出不保存的,所以chrome等浏览器没法直接右键保存

流程大概是这样的: 右键图片-审查元素-Resources-找到watermark.php-右键Resources窗口里的图片-另存为