PHP生成图片水印和文字水印

 文本水印

我们使用函数watermark_text()来生成文本水印,你必须先指定字体源文件、字体大小和字体文本,具体代码如下:

PHP代码
  1. $font_path = "GILSANUB.TTF"// Font file  
  2. $font_size = 30; // in pixcels  
  3. $water_mark_text_2 = "phpfuns"// Watermark Text  
  4.   
  5. function watermark_text($oldimage_name$new_image_name)  
  6. {  
  7. global $font_path$font_size$water_mark_text_2;  
  8. list($owidth,$oheight) = getimagesize($oldimage_name);  
  9. $width = $height = 300;  
  10. $image = imagecreatetruecolor($width$height);  
  11. $image_src = imagecreatefromjpeg($oldimage_name);  
  12. imagecopyresampled($image$image_src, 0, 0, 0, 0, $width$height$owidth$oheight);  
  13. $blue = imagecolorallocate($image, 79, 166, 185);  
  14. imagettftext($image$font_size, 0, 68, 190, $blue$font_path$water_mark_text_2);  
  15. imagejpeg($image$new_image_name, 100);  
  16. imagedestroy($image);  
  17. unlink($oldimage_name);  
  18. return true;  
  19. }  

图片水印
我们使用函数watermark_image()来生成图片水印,你必须先水银图片的源文件。具体代码如下:

PHP代码
  1. $image_path = "phpfuns.png";  
  2.   
  3. function watermark_image($oldimage_name$new_image_name)  
  4. /{  
  5. global $image_path;  
  6. list($owidth,$oheight) = getimagesize($oldimage_name);  
  7. $width = $height = 300;  
  8. $im = imagecreatetruecolor($width$height);  
  9. $img_src = imagecreatefromjpeg($oldimage_name);  
  10. imagecopyresampled($im$img_src, 0, 0, 0, 0, $width$height$owidth$oheight);  
  11. $watermark = imagecreatefrompng($image_path);  
  12. list($w_width$w_height) = getimagesize($image_path);  
  13. $pos_x = $width - $w_width;  
  14. $pos_y = $height - $w_height;  
  15. imagecopy($im$watermark$pos_x$pos_y, 0, 0, $w_width$w_height);  
  16. imagejpeg($im$new_image_name, 100);  
  17. imagedestroy($im);  
  18. unlink($oldimage_name);  
  19. return true;  
  20. }  

上传图片表单
我们使用下面的表单来上传图片:

PHP代码
  1. <?php  
  2. $demo_image"";  
  3. if(isset($_POST['createmark']) and $_POST['createmark'] == "Submit")  
  4. {  
  5. $path = "uploads/";  
  6. $valid_formats = array("jpg""bmp","jpeg");  
  7. $name = $_FILES['imgfile']['name'];  
  8. if(strlen($name))  
  9. {  
  10. list($txt$ext) = explode("."$name);  
  11. if(in_array($ext,$valid_formats) && $_FILES['imgfile']['size'] <= 256*1024)  
  12. {  
  13. $upload_status = move_uploaded_file($_FILES['imgfile']['tmp_name'], $path.$_FILES['imgfile']['name']);  
  14. if($upload_status){  
  15. $new_name = $path.time().".jpg";  
  16. // Here you have to user functins watermark_text or watermark_image  
  17. if(watermark_text($path.$_FILES['imgfile']['name'], $new_name))  
  18. $demo_image = $new_name;  
  19. }  
  20. }  
  21. else  
  22. $msg="File size Max 256 KB or Invalid file format.";  
  23. }  
  24. }  
  25. ?>  
  26. // HTML Code  
  27. <form name="imageUpload" method="post" enctype="multipart/form-data" >  
  28. Upload Image  
  29. Image :<input type="file" name="imgfile" /><br />  
  30. <input type="submit" name="createmark" value="Submit" />  
  31. <?php  
  32. if(!emptyempty($demo_image))  
  33. echo '<img src="'.$demo_image.'" />';  
  34. ?>  
  35. </form>  


上一篇: PHP字符串大小写字母转换的几个常用函数
下一篇: php连接mssql的一些方法总结
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: php
相关日志:
评论: 0 | 引用: 0 | 查看次数: 2460
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭

 广告位

↑返回顶部↑