php image and Chinese watermarking code sharing

  • 2020-05-26 07:59:19
  • OfStack

Case 1

 
<?php 
Header("Content-type: image/png"); /* Notification browser , To output an image */ 
$im = imagecreate(400 , 300); /* Define the size of the image */ 
$gray = ImageColorAllocate($im , 235 , 235 , 235); 
$pink = ImageColorAllocate($im, 255 , 128 , 255); 
$fontfile = "simkai.ttf"; 
/* $fontfile  Font path , It depends on the operating system , Can be  simhei.ttf( blackbody ) , SIMKAI.TTF( Regular script ) , SIMFANG.TTF( Imitation song dynasty style typeface ) ,SIMSUN.TTC( Song typeface & The new song typeface )  Etc.  GD  Supported Chinese fonts */ 
$str = iconv('GB2312','UTF-8',' Chinese watermark '); /* will  gb2312  The character set is converted into  UTF-8  The character of */ 
ImageTTFText($im, 30, 0, 100, 200, $pink , $fontfile , $str); 
/*  Add Chinese watermark  */ 
Imagepng($im); 
ImageDestroy($im); 
?> 

Case 2
 
<?php 
// **************************************** // 
//  function : Add text to the picture  
//  parameter : $img  Image file name  
// $new_img  Save the image file name , If is empty means does not save the picture  
// $text  String content  
// text_size  String size  
// text_angle  Type string output Angle  
// text_x  String output  x  coordinates  
// text_y  String output  y  coordinates  
// $text_font  Font file name  
// $r,$g,$b  String color RGB value  
// **************************************** // 
function img_text($img, $new_img, $text, $text_size, $text_angle, $text_x, $text_y, $text_font, $r, $g, $b){ 
$text=iconv("gb2312","UTF-8",$text); 
Header("Content-type: image/gif"); 
$im = @imagecreatefromstring(file_get_contents($img)) or die (" Failed to open picture !"); 
$color = ImageColorAllocate($im, $r,$g,$b); 
//ImageTTFText(int im, int size, int angle, int x, int y, int col, string fontfile, string text) :  
// This function will be  TTF (TrueType Fonts)  Type text to the picture.  
// parameter : size  Is the size of the glyph;  
// angle  Is the Angle of font, calculated clockwise, 0  For horizontal, ( From left to right ) . 90  The degree is the text from the bottom to the top;  
// x,y 2 The parameter is the coordinate value of the text  ( The origin is the upper left corner ) ;  
// col  Is the color of the word;  
// fontfile  Is the font file name;  
// text  It's the string content.  
ImageTTFText($im, $text_size, $text_angle, $text_x, $text_y, $color, $text_font, $text); 
if ($new_img==""): 
ImageGif($im); //  Don't save the picture , Display only  
else: 
ImageGif($im,$new_img); //  Save the picture , But don't show  
endif; 
ImageDestroy($im); // Close the graph and free the memory space  
} 
?> 

Example 3
 
<?php 
/* 
*  Function: PHP Image watermarking  ( Watermarking supports images or text ) 
*  Parameters:  
* $groundImage  Background image, that is, the need to add watermark image, only support GIF,JPG,PNG Format;  
* $waterPos  Watermark location, yes 10 Kind of state, 0 Is a random position;  
* 1 Is the top on the left, 2 Is the top centered, 3 Is the top in the right;  
* 4 To the left of the middle, 5 Is centered in the middle, 6 To the right of the middle;  
* 7 Is the bottom on the left, 8 Is the bottom center, 9 Is the bottom to the right;  
* $waterImage  Image watermarking, that is, as a watermark of the image, only support GIF,JPG,PNG Format;  
* $waterText  Text watermark, that is, text as a watermark, support ASCII Code, do not support Chinese;  
* $textFont  Text size, the value is 1 , 2 , 3 , 4 or 5 By default, 5 ;  
* $textColor  Text color, the value is 106 Base color value, default is #FF0000( red ) ;  
* 
*  Note: Support GD 2.0 . Support FreeType , GIF Read , GIF Create , JPG  , PNG 
* $waterImage  and  $waterText  It's best not to use both. Choose one or the other 1 Can be, preferential use  $waterImage .  
*  when $waterImage When valid, parameter $waterString , $stringFont , $stringColor None is effective.  
*  Watermarked image file name and  $groundImage 1 The sample.  
*  The author: longware @ 2004-11-3 14:15:13 
*/ 
function imageWaterMark($groundImage,$waterPos=0,$waterImage= " ",$waterText= " ",$textFont=5,$textColor= " #FF0000 " ) 
{ 
$isWaterImage = FALSE; 
$formatMsg =  "This file format is not currently supported, please use the image processing software to convert the image to GIF , JPG , PNG Format." ; 
// Read watermark file  
if(!emptyempty($waterImage) && file_exists($waterImage)) 
{ 
$isWaterImage = TRUE; 
$water_info = getimagesize($waterImage); 
$water_w = $water_info[0];// Gets the width of the watermark image  
$water_h = $water_info[1];// Get the watermark image high  
switch($water_info[2])// Get the watermark image format  
{ 
case 1:$water_im = imagecreatefromgif($waterImage);break; 
case 2:$water_im = imagecreatefromjpeg($waterImage);break; 
case 3:$water_im = imagecreatefrompng($waterImage);break; 
default:die($formatMsg); 
} 
} 
// Read background picture  
if(!emptyempty($groundImage) && file_exists($groundImage)) 
{ 
$ground_info = getimagesize($groundImage); 
$ground_w = $ground_info[0];// Gets the width of the background image  
$ground_h = $ground_info[1];// Get the height of the background image  
switch($ground_info[2])// Gets the format of the background image  
{ 
case 1:$ground_im = imagecreatefromgif($groundImage);break; 
case 2:$ground_im = imagecreatefromjpeg($groundImage);break; 
case 3:$ground_im = imagecreatefrompng($groundImage);break; 
default:die($formatMsg); 
} 
} 
else 
{ 
die( "The image that needs to be watermarked does not exist!" ); 
} 
// The watermark position  
if($isWaterImage)// Image watermarking  
{ 
$w = $water_w; 
$h = $water_h; 
$label =  "Pictorial" ; 
} 
else// Text watermarking  
{ 
$temp = imagettfbbox(ceil($textFont*5),0, " ./cour.ttf " ,$waterText);// Obtained using  TrueType  The range of text in a font  
$w = $temp[2] - $temp[6]; 
$h = $temp[3] - $temp[7]; 
unset($temp); 
$label =  "Text area" ; 
} 
if( ($ground_w<$w) || ($ground_h<$h) ) 
{ 
echo  "The image to be watermarked is longer or wider than the watermark" .$label. "Too small to generate a watermark!" ; 
return; 
} 
switch($waterPos) 
{ 
case 0:// random  
$posX = rand(0,($ground_w - $w)); 
$posY = rand(0,($ground_h - $h)); 
break; 
case 1://1 The top is on the left  
$posX = 0; 
$posY = 0; 
break; 
case 2://2 Is the top centered  
$posX = ($ground_w - $w) / 2; 
$posY = 0; 
break; 
case 3://3 The top is on the right  
$posX = $ground_w - $w; 
$posY = 0; 
break; 
case 4://4 The center is on the left  
$posX = 0; 
$posY = ($ground_h - $h) / 2; 
break; 
case 5://5 It is centered in the middle  
$posX = ($ground_w - $w) / 2; 
$posY = ($ground_h - $h) / 2; 
break; 
case 6://6 Center to the right  
$posX = $ground_w - $w; 
$posY = ($ground_h - $h) / 2; 
break; 
case 7://7 Is the bottom in the left  
$posX = 0; 
$posY = $ground_h - $h; 
break; 
case 8://8 Is the bottom center  
$posX = ($ground_w - $w) / 2; 
$posY = $ground_h - $h; 
break; 
case 9://9 Is the bottom in the right  
$posX = $ground_w - $w; 
$posY = $ground_h - $h; 
break; 
default:// random  
$posX = rand(0,($ground_w - $w)); 
$posY = rand(0,($ground_h - $h)); 
break; 
} 
// Set the color mixing mode of the image  
imagealphablending($ground_im, true); 
if($isWaterImage)// Image watermarking  
{ 
imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);// Copy the watermark to the target file  
} 
else// Text watermarking  
{ 
if( !emptyempty($textColor) && (strlen($textColor)==7) ) 
{ 
$R = hexdec(substr($textColor,1,2)); 
$G = hexdec(substr($textColor,3,2)); 
$B = hexdec(substr($textColor,5)); 
} 
else 
{ 
die( "Watermark text color format is incorrect!" ); 
} 
imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B)); 
} 
// Generate the watermarked image  
@unlink($groundImage); 
switch($ground_info[2])// Gets the format of the background image  
{ 
case 1:imagegif($ground_im,$groundImage);break; 
case 2:imagejpeg($ground_im,$groundImage);break; 
case 3:imagepng($ground_im,$groundImage);break; 
default:die($errorMsg); 
} 
// Free memory  
if(isset($water_info)) unset($water_info); 
if(isset($water_im)) imagedestroy($water_im); 
unset($ground_info); 
imagedestroy($ground_im); 
} 
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -  
$id=$_REQUEST['id']; 
$num = count($_FILES['userfile']['name']); 
print_r($_FILES['userfile']); 
print_r($_FILES['userfile']['name']); 
echo $num; 
echo  " <bR> " ; 
if(isset($id)){ 
for($i=0;$i<$id;$i++){ 
if(isset($_FILES) && !emptyempty($_FILES['userfile']) && $_FILES['userfile']['size']>0) 
{ 
$uploadfile =  " ./ " .time(). " _ " .$_FILES['userfile'][name][$i]; 
echo  " <br> " ; 
echo $uploadfile; 
if (copy($_FILES['userfile']['tmp_name'][$i], $uploadfile)) 
{ 
echo  " OK<br> " ; 
// Text watermarking  
//imageWaterMark($uploadfile,5, " ", " HTTP://www.lvye.info " ,5, " #cccccc " ); 
// Image watermarking  
$waterImage= " logo_ok1.gif " ;// Watermark image path  
imageWaterMark($uploadfile,9,$waterImage); 
echo  " <img src= " ".$uploadfile. ""  border= " 0 " > " ; 
} 
else 
{ 
echo  " Fail<br> " ; 
} 
} 
} 
} 
?> 
<form enctype= " multipart/form-data "  method= " POST " > 
<?php 
for($a=0;$a<$id;$a++){ 
echo  "File : <input name= " userfile[] "  type= " file " ><br> " ; 
} 
?> 
<input type= " submit "  value= "Upload" > 
</form> 

Code 4
 
 Add Chinese watermark  
<?php 
/*------------------------------------------------------------- 
** Description: this is a custom class for adding a bottom watermark (not occupying the image display area) to a specified image  
** Create: 2007-10-09 
** Update: 2007-10-09 
** Description: 1 , need gd Library support, required iconv Support ( php5 Already contains no load)  
2 , is only suitable for 3 Types of pictures, jpg/jpeg/gif/png , other types are not handled  
3 Note that the properties of the directory in which the image is located must be writable  
4 , call example:  
$objImg = new MyWaterDownChinese(); 
$objImg->Path = "images/"; 
$objImg->FileName = "1.jpg"; 
$objImg->Text = "HAHAKONGJIAN[url]HTTP://HI.BAIDU.COM/LYSONCN[/url]"; 
$objImg->Font = "./font/simhei.ttf"; 
$objImg->Run(); 
** Member functions:  
----------------------------------------------------------------*/ 
class MyWaterDownChinese{ 
var $Path = "./"; // The directory in which the image is located relative to the page calling this class  
var $FileName = ""; // The name of the picture, such as" 1.jpg "  
var $Text = ""; // Image to add watermark text, support Chinese  
var $TextColor = "#ffffff"; // The color of the text, gif The font color should only be black  
var $TextBgColor = "#000000"; // The color of the background bar of the text  
var $Font = "c://windows//fonts//simhei.ttf"; // Font storage directory, relative path  
var $OverFlag = true; // Whether to overwrite the original image, the default is to overwrite, not to overwrite, automatically after the original image file name +"_water_down" , such as" 1.jpg " => "1_water_down.jpg" 
var $BaseWidth = 200; // The width of the image should be at least >=200 , will add watermark text.  
//------------------------------------------------------------------ 
// Function: constructor of a class (php5.0 The above form ) 
// Parameters: no  
// Returns: no  
function __construct(){;} 
//------------------------------------------------------------------ 
// Function: destructor of a class (php5.0 The above form ) 
// Parameters: no  
// Returns: no  
function __destruct(){;} 
//------------------------------------------------------------------ 
//------------------------------------ 
// Function: object running function, add watermark to the picture  
// Parameters: no  
// Returns: no  
function Run() 
{ 
if($this->FileName == "" || $this->Text == "") 
return; 
// Check whether to install GD library  
if(false == function_exists("gd_info")) 
{ 
echo " System not installed GD Library, cannot add watermark to the picture ."; 
return; 
} 
// Set the input and output image pathnames  
$arr_in_name = explode(".",$this->FileName); 
// 
$inImg = $this->Path.$this->FileName; 
$outImg = $inImg; 
$tmpImg = $this->Path.$arr_in_name[0]."_tmp.".$arr_in_name[1]; // Temporary processing of the image, very important  
if(!$this->OverFlag) 
$outImg = $this->Path.$arr_in_name[0]."_water_down.".$arr_in_name[1]; 
// Check if the image exists  
if(!file_exists($inImg)) 
return ; 
// Get the properties of the image  
$groundImageType = @getimagesize($inImg); 
$imgWidth = $groundImageType[0]; 
$imgHeight = $groundImageType[1]; 
$imgType = $groundImageType[2]; 
if($imgWidth < $this->BaseWidth) // Less than the base width, not processed  
return; 
// The picture is not jpg/jpeg/gif/png Do not handle  
switch($imgType) 
{ 
case 1: 
$image = imagecreatefromgif($inImg); 
$this->TextBgColor = "#ffffff"; //gif The image font can only be black, so the background color is set to white  
break; 
case 2: 
$image = imagecreatefromjpeg($inImg); 
break; 
case 3: 
$image = imagecreatefrompng($inImg); 
break; 
default: 
return; 
break; 
} 
// Create a color  
$color = @imagecolorallocate($image,hexdec(substr($this->TextColor,1,2)),hexdec(substr($this->TextColor,3,2)),hexdec(substr($this->TextColor,5,2))); // Text color  
// generate 1 An empty image whose height increases the watermark height at the bottom  
$newHeight = $imgHeight+20; 
$objTmpImg = @imagecreatetruecolor($imgWidth,$newHeight); 
$colorBg = @imagecolorallocate($objTmpImg,hexdec(substr($this->TextBgColor,1,2)),hexdec(substr($this->TextBgColor,3,2)),hexdec(substr($this->TextBgColor,5,2))); // The background color  
// Fill in the background color of the image  
@imagefill ($objTmpImg,0,0,$colorBg); 
// The original image copy Go to the temporary image  
@imagecopy($objTmpImg,$image,0,0,0,0,$imgWidth,$imgHeight); 
// Creates a watermark text object to write to  
$objText = $this->createText($this->Text); 
// Calculates the location of the watermark text to write  
$x = 5; 
$y = $newHeight-5; 
// Write text watermark  
@imagettftext($objTmpImg,10,0,$x,$y,$color,$this->Font,$objText); 
// Generate new image, temporary image  
switch($imgType) 
{ 
case 1: 
imagegif($objTmpImg,$tmpImg); 
break; 
case 2: 
imagejpeg($objTmpImg,$tmpImg); 
break; 
case 3: 
imagepng($objTmpImg,$tmpImg); 
break; 
default: 
return; 
break; 
} 
// Release resources  
@imagedestroy($objTmpImg); 
@imagedestroy($image); 
// Rename file  
if($this->OverFlag) 
{ 
// Cover the original image  
@unlink($inImg); 
@rename($tmpImg,$outImg); 
} 
else 
{ 
// Do not overwrite the original image  
@rename($tmpImg,$outImg); 
} 
} 
//-------------------------------------- 
// Function: create watermark text object  
// Parameters: no  
// Return: watermark text object created  
function createText($instring) 
{ 
$outstring=""; 
$max=strlen($instring); 
for($i=0;$i<$max;$i++) 
{ 
$h=ord($instring[$i]); 
if($h>=160 && $i<$max-1) 
{ 
$outstring .= "&#".base_convert(bin2hex(iconv("gb2312","ucs-2",substr($instring,$i,2))),16,10).";"; 
$i++; 
} 
else 
{ 
$outstring .= $instring[$i]; 
} 
} 
return $outstring; 
} 
}//class 
?> 


Related articles: