Home > php教程 > php手册 > body text

php给图片加水印

WBOY
Release: 2016-06-06 19:47:04
Original
1179 people have browsed it

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

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

先看文件check_image_addwatermark.php代码

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

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

</span><span>//</span><span>如果是保存图片</span>
<span>if</span>(<span>$_POST</span>['submit'] == 'Save'<span>)
{
    </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['id']) && <span>ctype_digit</span>(<span>$_POST</span>['id']) && <span>file_exists</span>(<span>$dir</span>.'/'.<span>$_POST</span>['id'].'.jpg'<span>))
    {
        </span><span>$image</span> = imagecreatefromjpeg(<span>$dir</span>.'/'.<span>$_POST</span>['id'].'.jpg'<span>);
    }
    </span><span>else</span><span>
    {
        </span><span>die</span>('invalid image specified'<span>);
    }
    </span><span>$effect</span> = (<span>isset</span>(<span>$_POST</span>['effect'])) ? <span>$_POST</span>['effect'] : -1<span>;
    </span><span>switch</span>(<span>$effect</span><span>)
    {
        </span><span>case</span> IMG_FILTER_NEGATE:<span>
            imagefilter(</span><span>$image</span> , IMG_FILTER_NEGATE);     <span>//</span><span>将图像中所有颜色反转</span>
        <span>break</span><span>;
        </span><span>case</span> IMG_FILTER_GRAYSCALE:<span>
            imagefilter(</span><span>$image</span> , IMG_FILTER_GRAYSCALE);  <span>//</span><span>将图像转换为灰度的</span>
        <span>break</span><span>;
        </span><span>case</span> IMG_FILTER_EMBOSS:<span>
            imagefilter(</span><span>$image</span> , IMG_FILTER_EMBOSS);     <span>//</span><span>使图像浮雕化</span>
        <span>break</span><span>;
        </span><span>case</span> IMG_FILTER_GAUSSIAN_BLUR:<span>
            imagefilter(</span><span>$image</span> , IMG_FILTER_GAUSSIAN_BLUR); <span>//</span><span>用高斯算法模糊图像</span>
        <span>break</span><span>;    
    }
    
    </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_caption'<span>]))
    {
        imagettftext(</span><span>$image</span> , 12 , 0 , 20 , 20 , 0 , <span>$font</span> , <span>$image_caption</span><span>);
    }
    </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_logo'<span>]))
    {
        </span><span>//</span><span>获取水印图片的尺寸并创建水印</span>
        <span>list</span>(<span>$wmk_width</span> , <span>$wmk_height</span>) = <span>getimagesize</span>('images/logo.png'<span>);
        </span><span>$x</span> = (<span>$width</span>-<span>$wmk_width</span>) / 2<span>;
        </span><span>$y</span> = (<span>$height</span>-<span>$wmk_height</span>)/2<span>;
        </span><span>$wmk</span> = imagecreatefrompng('images/logo.png'<span>);
        </span><span>//</span><span>把水印图片和原图片合并在一起</span>
        imagecopymerge(<span>$image</span> , <span>$wmk</span> , <span>$x</span> , <span>$y</span> , 0 , 0 , <span>$wmk_width</span> , <span>$wmk_height</span> , 20<span>);
        </span><span>//</span><span>清除水印图片</span>
        imagedestroy(<span>$wmk</span><span>);
    }
    imagejpeg(</span><span>$image</span> , <span>$dir</span>.'/'.<span>$_POST</span>['id'].'.jpg' , 100<span>);
    </span>?>
    
        
            <title>Here is your pic!</title>
        
        
            <h1>Your image has been saved!</h1>
            <img src="images/<?php%20echo%20<span>%24_POST</span>%5B'id'%5D;?>.jpg" alt="">
        
    
<span>php 
}
</span><span>else</span><span>
{
</span>?>
    
        
            <title>Here is your pic!</title>
        
        
            <h1>So how does it feel to be famous?</h1>
            <p>Here is the picture you just uploaded to your servers:</p>
            <!--<img  src="images/<?php echo <span alt="php给图片加水印" >$imagename</span>;?>" alt=""   style="max-width:90%" />-->
        
    
    <span>php
        </span><span>if</span>(<span>$_POST</span>['submit'] == 'Upload'<span>)
        {
            </span><span>$imagename</span> = 'images/'.<span>$image_id</span>.'.jpg'<span>;
        }
        </span><span>else</span><span>
        {
            </span><span>$imagename</span> = 'image_effect.php?id='.<span>$image_id</span>.'&e='.<span>$_POST</span>['effect'<span>];
            </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_caption'<span>]))
            {
                </span><span>$imagename</span> .= '&capt='.<span>urlencode</span>(<span>$image_caption</span><span>);
            }
            </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_logo'<span>]))
            {
                </span><span>$imagename</span> .= '&logo=1'<span>;
            }
        }
    </span>?>
    <img src="<?php%20echo%20<span>%24imagename</span>;?>"   style="max-width:90%" alt="">
    
Copy after login
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作为水印图片和原来的图片合并在一起。

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

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

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

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

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

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

</span>?>
Copy after login

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

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 透明。

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!