php 给图片加水印

昨天下午同事问我一个php的问题,就是给图片加水印,php我也一知半解,网上资料找了一通,自己就写了一个加水印的php类。

具体代码如下:





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

<?php

class WaterGener{

    private
$default_text="Just for test";

    private
$default_waterpic="girl.jpg";

    private
$default_qulity=75;

    //默认使用的字体

    private
$font = ‘simhei.ttf‘; //定义字体

    //默认的padding 的值

    private
$padding=5;

    /**

    构造函数

    **/

    function
__construct(){

    }

    //获取图片类型

    private
function getImage($path){

        if(!empty($path) && file_exists($path)) {

            $water_info = @getimagesize($path);

            $water_im;

            switch($water_info[2])   {    //取得水印图片的格式 

                 case
1:$water_im = @imagecreatefromgif($path);break;

                 case
2:$water_im = @imagecreatefromjpeg($path);break;

                 case
3:$water_im = @imagecreatefrompng($path);break;

                 default:return
1;

             }

             return
$water_im;

        }

        return
-1;

    }

    public
function buildWaterImage($picture,$logo="",$savePath="demo.jpg"){

        //需要判断图片的类型,水印图片的类型

        if(!empty($path) && file_exists($path))return
-1;

        $logoImage =$this->getImage($logo===""?$this->default_waterpic:$logo);

        $photoImage =$this->getImage($picture);

        if($photoImage==-1){

            echo "没有找到图片";

            return;

        }

        imagealphablending($photoImage, true);

        $logo_size = getimagesize($logo);

        $logoW = $logo_size[0];

        $logoH = $logo_size[1];

        $picture_size = getimagesize($picture);

        ImageCopy($photoImage, $logoImage, $picture_size[0]-$logoW-$this->padding, $picture_size[1]-$logoH-$this->padding, 0, 0, $logoW, $logoH);

        ImageJPEG($photoImage,$savePath,$this->default_qulity); // output to browser or file

        ImageDestroy($photoImage);

        ImageDestroy($logoImage);

        echo "success...";

    }

    public
function buildWaterText($picture,$text="",$savePath="demo.jpg"){

        //需要判断

        $photoImage = $this->getImage($picture);

        ImageAlphaBlending($photoImage, true);

        $picture_size = getimagesize($picture);

        $textcolor = imagecolorallocate($photoImage, 255,255, 255);

        //解决乱码问题

        //$text = iconv("GB2312", "UTF-8", $text); //将中文字转换为UTF8

        imagettftext($photoImage, 20, 0, $this->padding, $picture_size[1]-($this->padding*4), $textcolor, $this->font, $text);//将文字写到图片中

        //imagestring($photoImage, 5, 0, 0,$text, $textcolor);

        ImageJPEG($photoImage,$savePath,$this->default_qulity); // output to browser

        ImageDestroy($photoImage);

    }

}

?>

上面代码有一些纰漏:

1,水印生成的位置,比如说可以是中间,右上角,右下角,等等。需要完善。

2,如果是加文字水印,文字水印的位置怎么计算?计算每个文字的宽度和高度?这个还有待完善。

3,异常的处理。php里面处理异常,我貌似还没有接触过。

上面代码调用方式:





1

2

3

4

5

6

<?php

include_once "water.class.php";

$water=new
WaterGener();

$water->buildWaterImage("girl.jpg","logo.gif","demo1.jpg");

$water->buildWaterText("girl.jpg","开源中国");

?>

时间: 2024-08-29 01:00:49

php 给图片加水印的相关文章

thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印

今天分享一下thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印.博主是新手,在这里卡住了很久(>_<) thinkphp 3.2.3整合ueditor 1.4 下载地址:https://github.com/Nintendov/Ueditor-thinkphp 下载下来,看着配置就可以了. 下面就是给上传图片加水印: (在做这步前,请确保ueditor已经正常工作) 我的工程目录如下: fonts里面的fz.fft为水印字体 images里面的logo.png为水印图片

java 图片加水印,设置透明度。说明很详细

package com.yidao.common; import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.Out

文件上传,图片加水印

文件上传: 所用控件:FileUpload 使用时的思路: 1.判断用户是否选中了文件 FileUpload.FileName获取选中的文件名,判断长度,如果长度大于零就代表已经选择了文件 JS端:通过ID获取控件,然后控件的value获取选中的文件名 2.将文件保存到服务器上 FileUpload.SaveAs("绝对路径"); 3.获得绝对路径 先编写相对路径:比如"UpLoads/abc.txt" 再把路径映射成绝对路径:Server.MapPath(&quo

利用PHP为图片加水印

1.为图片加风格字体 <?php header('Content-type:image/jpeg');//告诉服务器用JPEG图像格式输出 $img=imagecreatefromjpeg('images/girl.jpg');//建立背景图像 $color=imagecolorallocate($img,100,100,100);//分配给字体的颜色 $width=imagesx($img);//输出背景图像的总宽度 $height=imagesy($img);//输出背景图像的总长度 $po

iOS图片加水印效果的实现并保存至相冊

图片加水印效果的实现并保存至相冊 实现效果如图: project下载:githubproject下载链接 代码: - (void)viewDidLoad { [super viewDidLoad]; UIImage *image = [UIImage imageNamed:@"pushu.jpg"]; UIImage *waterImage = [self waterMarkImage:image withText:@"朴树水印測试"]; UIImageWriteT

PHP给图片加水印具体实现

给图片加水印实现方法如下: 1 class Mark 2 { 3 public function __construct() 4 { 5 6 } 7 8 /** 9 * 加水印 10 * @param file $srcImg 要加水印的图片 11 * @param file $waterImg 水印图片 12 * @param integer $position 水印图片放置位置 1:左上,2:右上,3:居中,4:左下,5:右下 13 * @param integer $alpha 水印图片透

java 图片加水印,设置透明度。说明非常具体

package com.yidao.common; import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.Out

图片加水印 图片验证码

图片加水印 1   获取这张图片 System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent); 2 给图片加水印 Graphics g = Graphics.FromImage(img); string s = "WWW.ITNBA.COM";         s 给图片加的水印文字 Font f = new Font("微软雅黑", 30); Brush b

给图片加水印

public void DoAddMark(object ParameterInfo) { #region 拆分参数 List<object> listParameter = (List<object>)ParameterInfo; ConnParameter connpar = (ConnParameter)listParameter[0]; //连接数据库参数 string[] strImgPath = (string[])listParameter[1]; //图片源路径 s