登录  /  注册
首页 > php教程 > php手册 > 正文

php给图片加水印

php中文网
发布: 2016-06-06 19:47:04
原创
841人浏览过

这一篇我们来看看那如何给图片添加水印,其实是把原图片和水印图片合并在一起。 先看文件check_image_addwatermark.php代码 ? php // 修改图片效果 $db = mysql_connect ('localhost','root','Ctrip07185419') or die ('can not connect to database' ); mys

这一篇我们来看看那如何给图片添加水印,其实是把原图片和水印图片合并在一起。

先看文件check_image_addwatermark.php代码

php 
//修改图片效果
$db = mysql_connect('localhost','root','Ctrip07185419') or die('can not connect to database');
mysql_select_db('moviesite',$db) or die(mysql_error($db));
//上传文件的路径
$dir = 'D:\Serious\phpdev\test\images';
//设置环境变量
putenv('GDFONTPATH='.'C:\Windows\Fonts');
$font = "arial";

//upload_image.php页面传递过来的参数,如果是上传图片
if($_POST['submit'] == 'Upload')
{
    if($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK)
    {
        switch($_FILES['uploadfile']['error'])
        {
            case UPLOAD_ERR_INI_SIZE:
                die('The uploaded file exceeds the upload_max_filesize directive');
            break;
            case UPLOAD_ERR_FORM_SIZE:
                die('The upload file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form');
            break;
            case UPLOAD_ERR_PARTIAL:
                die('The uploaded file was only partially uploaded');
            break;
            case UPLOAD_ERR_NO_FILE:
                die('No file was uploaded');
            break;
            case UPLOAD_ERR_NO_TMP_DIR:
                die('The server is missing a temporary folder');
            break;    
            case UPLOAD_ERR_CANT_WRITE:
                die('The server fail to write the uploaded file to the disk');
            break;        
            case UPLOAD_ERR_EXTENSION:
                die('The upload stopped by extension');
            break;                
        }
    }
    $image_caption = $_POST['caption'];
    $image_username = $_POST['username'];
    $image_date = date('Y-m-d');
    list($width,$height,$type,$attr) = getimagesize($_FILES['uploadfile']['tmp_name']);
    $error = 'The file you upload is not a supported filetype';
    switch($type)
    {
        case IMAGETYPE_GIF:
            $image = imagecreatefromgif($_FILES['uploadfile']['tmp_name']) or die($error);
        break;
        case IMAGETYPE_JPEG:
            $image = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']) or die($error);
        break;
        case IMAGETYPE_PNG:
            $image = imagecreatefrompng($_FILES['uploadfile']['tmp_name']) or die($error);
        break;
        default:
        break;
    }
    $query = 'insert into images(image_caption,image_username,image_date) values("'.$image_caption.'" , "'.$image_username.'","'.$image_date.'")';
    $result = mysql_query($query,$db) or die(mysql_error($db));
    $last_id = mysql_insert_id();
    
    // $imagename = $last_id.'.jpg';
    // imagejpeg($image,$dir.'/'.$imagename);
    // imagedestroy($image);
    
    $image_id = $last_id;
    imagejpeg($image , $dir.'/'.$image_id.'.jpg');
    imagedestroy($image);
}
else  //如果图片已经上传,则从数据库中取图片名字
{
    $query = 'select image_id,image_caption,image_username,image_date from images where image_id='.$_POST['id'];
    $result = mysql_query($query,$db) or die(mysql_error($db));
    extract(mysql_fetch_assoc($result));
    list($width,$height,$type,$attr) = getimagesize($dir.'/'.$image_id.'.jpg');
}

//如果是保存图片
if($_POST['submit'] == 'Save')
{
    if(isset($_POST['id']) && ctype_digit($_POST['id']) && file_exists($dir.'/'.$_POST['id'].'.jpg'))
    {
        $image = imagecreatefromjpeg($dir.'/'.$_POST['id'].'.jpg');
    }
    else
    {
        die('invalid image specified');
    }
    $effect = (isset($_POST['effect'])) ? $_POST['effect'] : -1;
    switch($effect)
    {
        case IMG_FILTER_NEGATE:
            imagefilter($image , IMG_FILTER_NEGATE);     //将图像中所有颜色反转
        break;
        case IMG_FILTER_GRAYSCALE:
            imagefilter($image , IMG_FILTER_GRAYSCALE);  //将图像转换为灰度的
        break;
        case IMG_FILTER_EMBOSS:
            imagefilter($image , IMG_FILTER_EMBOSS);     //使图像浮雕化
        break;
        case IMG_FILTER_GAUSSIAN_BLUR:
            imagefilter($image , IMG_FILTER_GAUSSIAN_BLUR); //用高斯算法模糊图像
        break;    
    }
    
    if(isset($_POST['emb_caption']))
    {
        imagettftext($image , 12 , 0 , 20 , 20 , 0 , $font , $image_caption);
    }
    if(isset($_POST['emb_logo']))
    {
        //获取水印图片的尺寸并创建水印
        list($wmk_width , $wmk_height) = getimagesize('images/logo.png');
        $x = ($width-$wmk_width) / 2;
        $y = ($height-$wmk_height)/2;
        $wmk = imagecreatefrompng('images/logo.png');
        //把水印图片和原图片合并在一起
        imagecopymerge($image , $wmk , $x , $y , 0 , 0 , $wmk_width , $wmk_height , 20);
        //清除水印图片
        imagedestroy($wmk);
    }
    imagejpeg($image , $dir.'/'.$_POST['id'].'.jpg' , 100);
    ?>
    
        
            Here is your pic!

Your image has been saved!

php } else { ?> Here is your pic!

So how does it feel to be famous?

Here is the picture you just uploaded to your servers:

php if($_POST['submit'] == 'Upload') { $imagename = 'images/'.$image_id.'.jpg'; } else { $imagename = 'image_effect.php?id='.$image_id.'&e='.$_POST['effect']; if(isset($_POST['emb_caption'])) { $imagename .= '&capt='.urlencode($image_caption); } if(isset($_POST['emb_logo'])) { $imagename .= '&logo=1'; } } ?>
登录后复制
Image save as: $image_id?>
Height: echo $height;?>
Widht: echo $width;?>
Upload date: echo $image_date;?>

You may apply a special effect to your image from the list of option below. Note:saving an image with any of the filters applied can be undone

Filter:
php echo '; if(isset($_POST['emb_caption'])) { echo ' checked="checked"'; } echo ' />Embed caption in image?'; echo '
'; //添加水印选项 echo '; if(isset($_POST['emb_logo'])) { echo 'checked="checked"'; } echo ' />Embed watermarked logo in image?'; ?>

php } ?>

这里面主要是添加水印选项,如果选中添加水印则将logo.png作为水印图片和原来的图片合并在一起。

在预览文件中添加了对应的逻辑,代码如下:

php 
$dir = 'D:\Serious\phpdev\test\images';
//设置环境变量
putenv('GDFONTPATH='.'C:\Windows\Fonts');
$font = "arial";

if(isset($_GET['id']) && ctype_digit($_GET['id']) && file_exists($dir.'/'.$_GET['id'].'.jpg'))
{
    $image = imagecreatefromjpeg($dir.'/'.$_GET['id'].'.jpg');
}
else
{
    die('invalid image specified');
}

$effect = (isset($_GET['e'])) ? $_GET['e'] : -1;

switch($effect)
{
    case IMG_FILTER_NEGATE:
        imagefilter($image , IMG_FILTER_NEGATE);
    break;
    case IMG_FILTER_GRAYSCALE:
        imagefilter($image , IMG_FILTER_GRAYSCALE);
    break;    
    case IMG_FILTER_EMBOSS:
        imagefilter($image , IMG_FILTER_EMBOSS);
    break;    
    case IMG_FILTER_GAUSSIAN_BLUR:
        imagefilter($image , IMG_FILTER_GAUSSIAN_BLUR);
    break;    
}

if(isset($_GET['capt']))
{
    //echo $_GET['capt'];
    imagettftext($image, 12, 0, 20, 20, 0, $font, $_GET['capt']);
}
if(isset($_GET['logo']))
{
    list($widht , $height) = getimagesize($dir.'/'.$_GET['id'].'.jpg');
    list($wmk_width , $wmk_height) = getimagesize('images/logo.png');
    $x = ($widht-$wmk_width) / 2;
    $y = ($height-$wmk_height) / 2;
    
    $wmk = imagecreatefrompng('images/logo.png');
    imagecopymerge($image , $wmk , $x , $y , 0 , 0 , $wmk_width , $wmk_height , 20);
    imagedestroy($wmk);
}
header('Content-Type:image/jpeg');
imagejpeg($image , '' , 100);

?>
登录后复制

最后上传的水印图片效果如下:

php给图片加水印

注意主要的逻辑就是通过 imagecopymerge() 方法把两个图片合并在一起造成水印效果。来看看这个方法的方法原型和参数:

bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int$src_x , int $src_y , int $src_w , int $src_h , int $pct )

将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。两图像将根据 pct 来决定合并程度,其值范围从 0 到 100。当 pct = 0 时,实际上什么也没做,当为 100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。

来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学