Linux下测试成功.- <?php
- Class Gimage{
- var $src_image_name = ""; //输入图片的文件名(必须包含路径名)
- var $jpeg_quality = 100; //jpeg图片质量
- var $save_image_file = ''; //输出文件名
- var $wm_image_name = ""; //水印图片的文件名(必须包含路径名)
- var $wm_image_pos = 4; //水印图片放置的位置
- // 0 = middle
- // 1 = top left
- // 2 = top right
- // 3 = bottom right
- // 4 = bottom left
- var $wm_image_transition = 100; //水印图片与原图片的融合度 (1=100)
- var $wm_text = ""; //水印文字(支持中文)
- var $wm_text_size = 10; //水印文字大小
- var $wm_text_angle = 0; //水印文字角度
- var $wm_text_pos = 0; //水印文字放置位置
- var $wm_text_font = ""; //水印文字的字体
- function create($filename="")
- {
- if ($filename) $this->src_image_name = trim($filename);
- $src_image_type = $this->get_type($this->src_image_name);
- $src_image = $this->createImage($src_image_type,$this->src_image_name);
- if (!$src_image) return;
- $src_image_w=ImageSX($src_image);
- $src_image_h=ImageSY($src_image);
- if ($this->wm_image_name){
- $this->wm_image_name = trim($this->wm_image_name);
- $wm_image_type = $this->get_type($this->wm_image_name);
- $wm_image = $this->createImage($wm_image_type,$this->wm_image_name);
- $wm_image_w=ImageSX($wm_image);
- $wm_image_h=ImageSY($wm_image);
- $temp_wm_image = $this->getPos($src_image_w,$src_image_h,$this->wm_image_pos,$wm_image);
- $wm_image_x = $temp_wm_image["dest_x"];
- $wm_image_y = $temp_wm_image["dest_y"];
- imageCopyMerge($src_image, $wm_image,$wm_image_x,$wm_image_y,0,0,$wm_image_w,$wm_image_h,$this->wm_image_transition);
- }
- if ($this->wm_text){
- $temp_wm_text = $this->getPos($src_image_w,$src_image_h,$this->wm_text_pos);
- $wm_text_x = $temp_wm_text["dest_x"];
- $wm_text_y = $temp_wm_text["dest_y"];
- $wm_text_color = imagecolorallocate($src_image, 255,255,255);
- imagettftext($src_image, $this->wm_text_size, $this->wm_angle, $wm_text_x, $wm_text_y, $wm_text_color, ($this->wm_text_font), $this->wm_text);
- }
- if ($this->save_file)
- {
- switch ($this->output_type){
- case 'gif':$src_img=ImagePNG($src_image, $this->save_file); break;
- case 'jpeg':$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;
- case 'png':$src_img=ImagePNG($src_image, $this->save_file); break;
- default:$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;
- }
- }
- else
- {
- if ($src_image_type = "jpg") $src_image_type="jpeg";
- header("Content-type: image/{$src_image_type}");
- switch ($src_image_type){
- case 'gif':$src_img=ImagePNG($src_image); break;
- case 'jpg':$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;
- case 'png':$src_img=ImagePNG($src_image);break;
- default:$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;
- }
- }
- imagedestroy($src_image);
- }
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- /*
- createImage 根据文件名和类型创建图片
- 内部函数
- $type: 图片的类型,包括gif,jpg,png
- $img_name: 图片文件名,包括路径名,例如 " ./mouse.jpg"
- */
- function createImage($type,$img_name){
- if (!$type){
- $type = $this->get_type($img_name);
- }
- switch ($type){
- case 'gif':
- if (function_exists('imagecreatefromgif'))
- $tmp_img=@imagecreatefromgif($img_name);
- break;
- case 'jpg':
- $tmp_img=imagecreatefromjpeg($img_name);
- break;
- case 'png':
- $tmp_img=imageceatefrompng($img_name);
- break;
- default:
- $tmp_img=imagecreatefromstring($img_name);
- break;
- }
- return $tmp_img;
- }
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- getPos 根据源图像的长、宽,位置代码,水印图片id来生成把水印放置到源图像中的位置
- 内部函数
- $sourcefile_width: 源图像的宽
- $sourcefile_height: 原图像的高
- $pos: 位置代码
- // 0 = middle
- // 1 = top left
- // 2 = top right
- // 3 = bottom right
- // 4 = bottom left
- $wm_image: 水印图片ID
- */
- function getPos($sourcefile_width,$sourcefile_height,$pos,$wm_image=""){
- if ($wm_image){
- $insertfile_width = ImageSx($wm_image);
- $insertfile_height = ImageSy($wm_image);
- }else {
- $insertfile_width = 0;
- $insertfile_height = 0;
- }
- if( $pos == 0 )
- {
- $dest_x = ( $sourcefile_width / 2 ) - ( $insertfile_width / 2 );
- $dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );
- }
- //top left
- if( $pos == 1 )
- {
- $dest_x = 0;
- $dest_y = 0;
- }
- //top right
- if( $pos == 2 )
- {
- $dest_x = $sourcefile_width - $insertfile_width;
- $dest_y = 0;
- }
- //bottom right
- if( $pos == 3 )
- {
- $dest_x = $sourcefile_width - $insertfile_width;
- $dest_y = $sourcefile_height - $insertfile_height;
- }
- //bottom left
- if( $pos == 4 )
- {
- $dest_x = 0;
- $dest_y = $sourcefile_height - $insertfile_height;
- }
- return array("dest_x"=>$dest_x,"dest_y"=>$dest_y);
- }
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- get_type 获得图片的格式,包括jpg,png,gif
- 内部函数
- $img_name: 图片文件名,可以包括路径名
- */
- function get_type($img_name)//获取图像文件类型
- {
- $name_array = explode(".",$img_name);
- if (preg_match("/\.(jpg|jpeg|gif|png)$/", $img_name, $matches))
- {
- $type = $matches[1];
- }
- else
- {
- $type = "string";
- }
- return $type;
- }
- }
- ?>
- 2、把需要的字体文件放到"c:\winnt\fonts"目录中,如果没有这个目录就建立一个,否则无法使用字体。至于unix类的服务器则不需要
- 3、打开/include/post.php
- 在第二行添加
- 复制代码
- require $discuz_root.'./include/imgmark.php';
- 找到
- 复制代码
- if(!$attach_saved && @is_readable($attach)) {
- @$fp = fopen($attach, 'rb');
- @flock($fp, 2);
- @$attachedfile = fread($fp, $attach_size);
- @fclose($fp);
- @$fp = fopen($target, 'wb');
- @flock($fp, 2);
- if(@fwrite($fp, $attachedfile)) {
- $attach_saved = true;
- }
- @fclose($fp);
- }
- 在下面增加
- 复制代码
- if(in_array($extension, array('jpg','gif','png'))) {
- echo $discuz_root;
- $tmp_image = new Gimage();
- //$tmp_image->wm_text="Upload By SzBar.com";//要显示在图片中的文字信息
- $tmp_image->wm_image_name= "[img]http://www.verysz.com/image/water.jpg[/img]";//要显示在图片中的水印图片名称,包括路径名
- $tmp_image->wm_text_font = 'arial';//水印文字的字体,也就是在c:/winnt/fonts中的字体文件名
- $tmp_image->save_file=$target;
- $tmp_image->create($target);
- }
复制代码 完成!
说明:
如果要用文字水印请把这么代码前面的//去掉
--------------------------------------------------------------------------------
//$tmp_image->wm_text="Upload By SzBar.com";
然后在这段代码前面加上//
如果系统找不到你所设置的字体,则自动用“宋体”代替。
添加水印的位置是可以修改的,请打开imgmark.php按提示修改就可以了! |