這款程序給圖片加文字水印時是調用 了C://WINDOWS//Fonts////SIMHEI.TTF字體,給圖片加水印時就可以自定圖片,實例代碼如下:
- $image->wprint_img();//執行圖片水印
- $image->wprint_string();//執行文字水印
- class editimage{
- private $imagefile;//圖片文件
- private $smallimg;//水印圖片
- private $string;//水印文字
- private $position;//存放位置
- private $dst_x=600;//原始圖片打水印x坐標
- private $dst_y=0;//原始圖片打水印y坐標
- private $str_x=450;
- private $str_y=200;
- private $font="c:windows ontssimhei.ttf";//原始圖片打水印字體路徑
- private $imgej;// imagecolorallocate后的變量
- function __get($value){
- return $this->$value;
- }
- function __set($property,$value){
- $this->$property=$value;
- }
- /**
- * 構造函數初始化
- *
- * @param string $imagefile 被上水印的文件
- * @param string $smallimg 水印文件
- * @param string $string 水印文字
- * @param string $position 存放位置
- * @param int $dst_x 被上水印的圖片x
- * @param int $dst_y 被上水印的圖片y
- */
- function __construct($imagefile,$smallimg='',$string=''){//,$position='',$dst_x=0,$dst_y=0
- $this->imagefile=$imagefile;
- $this->smallimg=$smallimg;
- $this->string=$string;
- $this->imgej=$this->imagecreatef($this->imagefile);
- }
- function get_extname($file){//獲取文件的后綴名
- if (file_exists($this->imagefile)) {
- $img=getimagesize($file);
- switch ($img[2]){
- case "1":
- return "gif";
- case "2":
- return "jpg";
- case "3":
- return "png";
- }
- }else{
- return false;
- }
- }
- function getsize($file,$wh){//獲取圖大小. $wh:w獲得寬,h獲得高
- $image=getimagesize($file);
- if ($wh) {
- switch ($wh){
- case "w":
- return $image[0];
- case "h":
- return $image[1];
- }
- }else{
- return false;
- }
- }
- function imagecreatef($file){//創建類型
- if ($this->get_extname($file)) {
- switch($this->get_extname($file)){
- case "gif":
- return imagecreatefromgif($file);
- case "jpg":
- return imagecreatefromjpeg($file);
- case "png":
- return imagecreatefrompng($file);
- }
- }else{
- echo "文件不存在";
- }
- }
- //水印圖片處理
- function wprint_img(){
- if($this->smallimg){
- imagecopy($this->imgej,$this->imagecreatef($this->smallimg),$this->dst_x,$this->dst_y,0,0,$this->getsize($this->smallimg,"w"),$this->getsize($this->smallimg,"h"));
- }else{
- return "水印圖片不存在!";
- }
- }
- //水印文字處理
- function wprint_string(){
- return imagettftext($this->imgej,20,0,$this->str_x,$this->str_y,imagecolorallocate($this->imgej,200,200,200),$this->font,iconv("gb2312","utf-8",$this->string));
- }
- function choose_imgouttype(){//輸出
- if($this->position){
- $this->get_extname($this->imagefile);
- switch ($this->get_extname($this->imagefile)){
- case "gif":
- return imagegif($this->imgej,$position);
- case "jpg":
- return imagejpeg($this->imgej,$this->position);
- case "jpeg":
- return imagejpeg($this->imgej,$this->position);
- case "png":
- return imagepng($this->imgej,$position);
- }
- }else{
- switch ($this->get_extname($this->imagefile)){
- case "gif":
- return imagegif($this->imgej);
- case "jpg":
- return imagejpeg($this->imgej);
- case "jpeg":
- return imagejpeg($this->imgej);
- case "png":
- return imagepng($this->imgej);
- }
- }
- }
- }
- //使用方法如下:
- $image=new editimage("d90.gif","hknmtt.png","我的d90");
- $image->wprint_img();//執行圖片水印
- $image->wprint_string();//執行文字水印,開源代碼Vevb.com
- $image->choose_imgouttype();
新聞熱點
疑難解答