`
yushine
  • 浏览: 197019 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

PHP生成水印

    博客分类:
  • PHP
阅读更多
<?php
class resizeimage
{
//图片类型
var $type;
//实际宽度
var $width;
//实际高度
var $height;
//改变后的宽度
var $resize_width;
//改变后的高度
var $resize_height;
//源图象
var $srcimg;
//目标图象地址
var $dstimg;
//临时创建的图象
var $im;

function resizeimage($img, $wid, $hei,$thumbwid,$thumbhei,$c=0)
{
$this->srcimg = $img;
//echo $img.$wid.$hei.$c;
$this->$img = $img;
$this->resize_width = $wid;
$this->resize_height = $hei;
$this->thumb_wid = $thumbwid;
$this->thumb_hei = $thumbhei;
$this->cut = $c;
//图片的类型
$this->type = substr(strrchr($this->srcimg,"."),1);
//初始化图象
$this->initi_img();
//目标图象地址
$this -> dst_img();
//imagesx imagesy 取得图像 宽、高
$this->width = imagesx($this->im);
$this->height = imagesy($this->im);
}
//初始化图象
function uploadpic($filename,$wartercontent,$wartercontent2,$state='pic')
{
$img=getimagesize($filename);
echo "<pre>";
print_r($img);
echo "</pre>";
$f_type = $img[2];
switch($img[2])
{
case 1:
$fileImage =ImageCreateFromGIF($filename); //载入目标图片
break;
case 2:
$fileImage =ImageCreateFromJPEG($filename);
break;
case 3:
$fileImage =ImageCreateFromPNG($filename);
break;
}
if($state=='pic')//载入logo图片
{
$img=getimagesize($wartercontent);
switch($img[2])
{
case 1:
$logoImage =ImageCreateFromGIF($wartercontent);
break;
case 2:
$logoImage =ImageCreateFromJPEG($wartercontent);
break;
case 3:
$logoImage =ImageCreateFromPNG($wartercontent);
break;
}

$img2=getimagesize($wartercontent2);
switch($img2[2])
{
case 1:
$logoImage2 =ImageCreateFromGIF($wartercontent2);
break;
case 2:
$logoImage2 =ImageCreateFromJPEG($wartercontent2);
break;
case 3:
$logoImage2 =ImageCreateFromPNG($wartercontent2);
break;
}

ImageAlphaBlending($fileImage, true);

$logoW = ImageSX($logoImage);
$logoH = ImageSY($logoImage);

$logoW2 = ImageSX($logoImage2);
$logoH2 = ImageSY($logoImage2);
}
$resize_ratio = ($this->resize_width)/($this->resize_height);//改变后的大小
$thumb_ratio = ($this->thumb_wid)/($this->thumb_hei);
$ratio = ($this->width)/($this->height);//实际大小
if($ratio>=$resize_ratio)
{
$newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
imagecopyresampled($newimg, $fileImage, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
if($state=='pic')
{
echo $watermarkwidth = $this->resize_width/2-100;
echo $watermarkheight = ($this->resize_width)/$ratio/2-100;
ImageCopy($newimg, $logoImage, 0, 0, 0, 0, $logoW, $logoH);
ImageCopy($newimg, $logoImage2, $watermarkwidth, $watermarkheight, 0, 0, $logoW2, $logoH2);
}
else
{
$black = imagecolorallocate($newimg,-50,-50,-50);
$str=iconv("gbk","utf-8",$wartercontent);
imagettftext($newimg,12,0,20,20,$black,'simhei.ttf',$str);
}
$thumb = imagecreatetruecolor($this->thumb_wid,($this->thumb_wid)/$ratio);
imagecopyresampled($thumb,$fileImage,0,0,0,0,$this->thumb_wid,($this->thumb_wid)/$ratio,$this->width,$this->height);
if($f_type==3)
{
imagepng($newimg,$filename);
imagepng($thumb,$this->dstimg);
}
else
{
ImageJpeg($newimg,$filename);
ImageJpeg($thumb,$this->dstimg);
}
}
if($ratio<$resize_ratio)
{
$newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
imagecopyresampled($newimg, $fileImage, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
if($state=='pic')
{
echo $watermarkwidth = $this->resize_height*$ratio/2-100;
echo $watermarkheight = $this->resize_height/2-100;
ImageCopy($newimg, $logoImage, 0, 0, 0, 0, $logoW, $logoH);
ImageCopy($newimg, $logoImage2, $watermarkwidth, $watermarkheight, 0, 0, $logoW2, $logoH2);
}
else
{
$black = imagecolorallocate($newimg,-50,-50,-50);
//$wartercontent=iconv("gbk","utf8",$wartercontent);
imagettftext($newimg,12,0,20,20,$black,'simhei.ttf',$wartercontent);
}
$thumb = imagecreatetruecolor(($this->thumb_hei)*$ratio,$this->thumb_hei);
imagecopyresampled($thumb,$fileImage,0,0,0,0,($this->thumb_hei)*$ratio,$this->thumb_hei,$this->width,$this->height);
if($f_type==3)
{
imagepng($newimg,$filename);
imagepng($thumb,$this->dstimg);
}
else
{
ImageJpeg($newimg,$filename);
ImageJpeg($thumb,$this->dstimg);
}
}
ImageDestroy($fileImage);
if($state=='pic')
{
ImageDestroy($logoImage);
}
ImageDestroy($this->im);
}
function initi_img()
{
if($this->type=="jpg")
{
$this->im = imagecreatefromjpeg($this->srcimg);
}
if($this->type=="gif")
{
$this->im = imagecreatefromgif($this->srcimg);
}
if($this->type=="png")
{
$this->im = imagecreatefrompng($this->srcimg);
}
}
//图象目标地址
function dst_img()
{
$full_length  = strlen($this->srcimg);
$type_length  = strlen($this->type);
$name_length  = $full_length-$type_length;
$name         = substr($this->srcimg,0,$name_length-1);
$this->dstimg = $name."_thumb.".$this->type;
}
}
?>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics