-
-
$img='22.jpg'; - $arr=getimagesize($img);
- //print_r($arr); //$arr[0] is the width of the image, $arr[1] is the height of the image, $arr[2] is the type of the image, that is, the extension of the image,
- switch($arr[2]){
- case 1:
- $imgn = imagecreatefromgif ($img);
- break;
- case 2:
- $imgn = imagecreatefromjpeg($img);
- break;
- case 3:
- $imgn = imagecreatefrompng($img);
- break;
- case 6:
- $imgn = imagecreatefromwbmp($img);
- break;
- default:
- die("Unsupported file type");
- exit;
- }
//Start watermarking. The text to be printed (php text watermark)
- $str = "Text to be printed";
- $str1 = "Text to be printed";
//$str = iconv("gb2312 ","utf-8","Text to be printed"); //(The last description of this statement);
- //Then use the palette to set the color of the text:
- //$dest=imagecreatetruecolor(100,100);
- $bg = imagecolorallocate($imgn,255,255,255);
//$bg = imagecolorallocate($imgn,0,0,0 );
- //php5 cannot know the font of the text, so Load the text font, here load the simhei.ttf boldface that comes with Windows (copy the font file to the project folder before loading) as follows:
- imagettftext($imgn,10,0,20,10 ,$bg,'simhei.ttf',$str); //This will type the $str text onto $imgn
- imagettftext($imgn,10,0,20,46,$bg,'simhei.ttf', $str1);
- header('content-type:image/jpeg');
- $uploaddir = './image/';
- $thumb_path = './image/'.date("Ymd").'/' ;
- if(!is_dir($uploaddir)){
- mkdir($uploaddir,0777);
- }
- if(!is_dir($thumb_path)){
- mkdir($thumb_path,0777);
- }
- $imgs = $ thumb_path.time().'.jpg';
- imagejpeg($imgn,$imgs,'80');
- echo "";
- //: GetImageSize, $ str = iconv("gbk","utf-8","text to be typed"); indicates changing the page's default encoding attribute gbk to utf-8 international standard encoding. $bg = imagecolorallocate() sets the foreground text of the image to white, where 0, 0, 0 respectively represent the red green blue color components, the imagettftext function 12 represents the font size 9 indicates the tilt 10, 10 is the pixel position of the watermark color header('content-type:image/jpeg'); imagejpeg($imgn); indicates that the file type output by the php5 page is an image type.
- ?>
Copy code
|