国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development PHP Tutorial PHP image watermark code, add text watermark and image watermark to PHP images

PHP image watermark code, add text watermark and image watermark to PHP images

Jul 25, 2016 am 08:51 AM

  1. /**

  2. * Add watermark class, support transparency setting of text and picture watermarks, and transparent background of watermark pictures.
  3. * Usage: (php text watermark effect)
  4. * $obj = new WaterMask($imgFileName); //Instantiate the object
  5. * $obj->$waterType = 1; //Type: 0 is text watermark, 1 is Image watermark
  6. * $obj->$transparent = 45; //Watermark transparency
  7. * $obj->$waterStr = 'bbs.it-home.org'; //Watermark text
  8. * $obj->$ fontSize = 16; //Text font size
  9. * $obj->$fontColor = array(255,0255); //Watermark text color (RGB)
  10. * $obj->$fontFile = = 'AHGBold.ttf' ; //Font file
  11. * $obj->output(); //The output watermark image file overwrites the input image file
  12. */
  13. class WaterMask{
  14. public $waterType = 1; //Watermark type: 0 is text watermark, 1 is picture watermark
  15. public $pos = 0; //Watermark position
  16. public $transparent = 45; //Watermark transparency
  17. public $waterStr = 'bbs.it-home.org'; //Watermark text
  18. public $fontSize = 16; //Text font Size
  19. public $fontColor = array(255,0,255); //Watermark text color (RGB)
  20. public $fontFile = 'AHGBold.ttf'; //Font file
  21. public $waterImg = 'logo.png'; //Watermark Picture
  22. private $srcImg = ''; //Picture that needs to be watermarked
  23. private $im = ''; //Picture handle
  24. private $water_im = ''; //Watermark picture handle
  25. private $srcImg_info = ''; / /Picture information
  26. private $waterImg_info = ''; //Watermark picture information
  27. private $str_w = ''; //Watermark text width
  28. private $str_h = ''; //Watermark text height
  29. private $x = ''; //Watermark $img) ? $img : die('"'.$img.'" The source file does not exist!');
  30. }
  31. private function imginfo() { //Get the information of the image that needs to be watermarked and load the image .
  32. $this->srcImg_info = getimagesize($this->srcImg);
  33. switch ($this->srcImg_info[2]) {
  34. case 3:
  35. $this->im = imagecreatefrompng($this-> ;srcImg);
  36. break 1;
  37. case 2:
  38. $this->im = imagecreatefromjpeg($this->srcImg);
  39. break 1;
  40. case 1:
  41. $this->im = imagecreatefromgif($this ->srcImg);
  42. break 1;
  43. default:
  44. die('The format of the original image ('.$this->srcImg.') is wrong, only PNG, JPEG, and GIF are supported.');
  45. }
  46. }
  47. private function waterimginfo() { //Get the information of the watermark image and load the image.
  48. $this->waterImg_info = getimagesize($this->waterImg);
  49. switch ($this->waterImg_info[2]) {
  50. case 3:
  51. $this->water_im = imagecreatefrompng($this-> ;waterImg);
  52. break 1;
  53. case 2:
  54. $this->water_im = imagecreatefromjpeg($this->waterImg);
  55. break 1;
  56. case 1:
  57. $this->water_im = imagecreatefromgif($this ->waterImg);
  58. break 1;
  59. default:
  60. die('The watermark image ('.$this->srcImg.') is in the wrong format, only supports PNG, JPEG, and GIF.');
  61. }
  62. }
  63. private function waterpos() { //水印位置算法
  64. switch ($this->pos) {
  65. case 0: //隨機(jī)位置
  66. $this->x = rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]);
  67. $this->y = rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]);
  68. break 1;
  69. case 1: //上左
  70. $this->x = 0;
  71. $this->y = 0;
  72. break 1;
  73. case 2: //上中
  74. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  75. $this->y = 0;
  76. break 1;
  77. case 3: //上右
  78. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  79. $this->y = 0;
  80. break 1;
  81. case 4: //中左
  82. $this->x = 0;
  83. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  84. break 1;
  85. case 5: //中中
  86. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  87. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  88. break 1;
  89. case 6: //中右
  90. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  91. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  92. break 1;
  93. case 7: //下左
  94. $this->x = 0;
  95. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  96. break 1;
  97. case 8: //下中
  98. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  99. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  100. break 1;
  101. default: //下右
  102. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  103. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  104. break 1;
  105. }
  106. }
  107. private function waterimg() {
  108. if ($this->srcImg_info[0] <= $this->waterImg_info[0] || $this->srcImg_info[1] <= $this->waterImg_info[1]){
  109. die('水印比原圖大!');
  110. }
  111. $this->waterpos();
  112. $cut = imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]);
  113. imagecopy($cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1]);
  114. $pct = $this->transparent;
  115. imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]);
  116. imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[1],$pct);
  117. }
  118. private function waterstr() {
  119. $rect = imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr);
  120. $w = abs($rect[2]-$rect[6]);
  121. $h = abs($rect[3]-$rect[7]);
  122. $fontHeight = $this->fontSize;
  123. $this->water_im = imagecreatetruecolor($w, $h);
  124. imagealphablending($this->water_im,false);
  125. imagesavealpha($this->water_im,true);
  126. $white_alpha = imagecolorallocatealpha($this->water_im,255,255,255,127);
  127. imagefill($this->water_im,0,0,$white_alpha);
  128. $color = imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
  129. imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr);
  130. $this->waterImg_info = array(0=>$w,1=>$h);
  131. $this->waterimg();
  132. }
  133. function output() {
  134. $this->imginfo();
  135. if ($this->waterType == 0) {
  136. $this->waterstr();
  137. }else {
  138. $this->waterimginfo();
  139. $this->waterimg();
  140. }
  141. switch ($this->srcImg_info[2]) {
  142. case 3:
  143. imagepng($this->im,$this->srcImg);
  144. break 1;
  145. case 2:
  146. imagejpeg($this->im,$this->srcImg);
  147. break 1;
  148. case 1:
  149. imagegif($this->im,$this->srcImg);
  150. break 1;
  151. default:
  152. die('添加水印失??!');
  153. break;
  154. }
  155. imagedestroy($this->im);
  156. imagedestroy($this->water_im);
  157. }
  158. }
  159. ?>

復(fù)制代碼


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I implement authentication and authorization in PHP? How do I implement authentication and authorization in PHP? Jun 20, 2025 am 01:03 AM

TosecurelyhandleauthenticationandauthorizationinPHP,followthesesteps:1.Alwayshashpasswordswithpassword_hash()andverifyusingpassword_verify(),usepreparedstatementstopreventSQLinjection,andstoreuserdatain$_SESSIONafterlogin.2.Implementrole-basedaccessc

How can you handle file uploads securely in PHP? How can you handle file uploads securely in PHP? Jun 19, 2025 am 01:05 AM

To safely handle file uploads in PHP, the core is to verify file types, rename files, and restrict permissions. 1. Use finfo_file() to check the real MIME type, and only specific types such as image/jpeg are allowed; 2. Use uniqid() to generate random file names and store them in non-Web root directory; 3. Limit file size through php.ini and HTML forms, and set directory permissions to 0755; 4. Use ClamAV to scan malware to enhance security. These steps effectively prevent security vulnerabilities and ensure that the file upload process is safe and reliable.

What are the differences between == (loose comparison) and === (strict comparison) in PHP? What are the differences between == (loose comparison) and === (strict comparison) in PHP? Jun 19, 2025 am 01:07 AM

In PHP, the main difference between == and == is the strictness of type checking. ==Type conversion will be performed before comparison, for example, 5=="5" returns true, and ===Request that the value and type are the same before true will be returned, for example, 5==="5" returns false. In usage scenarios, === is more secure and should be used first, and == is only used when type conversion is required.

How do I perform arithmetic operations in PHP ( , -, *, /, %)? How do I perform arithmetic operations in PHP ( , -, *, /, %)? Jun 19, 2025 pm 05:13 PM

The methods of using basic mathematical operations in PHP are as follows: 1. Addition signs support integers and floating-point numbers, and can also be used for variables. String numbers will be automatically converted but not recommended to dependencies; 2. Subtraction signs use - signs, variables are the same, and type conversion is also applicable; 3. Multiplication signs use * signs, which are suitable for numbers and similar strings; 4. Division uses / signs, which need to avoid dividing by zero, and note that the result may be floating-point numbers; 5. Taking the modulus signs can be used to judge odd and even numbers, and when processing negative numbers, the remainder signs are consistent with the dividend. The key to using these operators correctly is to ensure that the data types are clear and the boundary situation is handled well.

How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? Jun 19, 2025 am 01:07 AM

Yes, PHP can interact with NoSQL databases like MongoDB and Redis through specific extensions or libraries. First, use the MongoDBPHP driver (installed through PECL or Composer) to create client instances and operate databases and collections, supporting insertion, query, aggregation and other operations; second, use the Predis library or phpredis extension to connect to Redis, perform key-value settings and acquisitions, and recommend phpredis for high-performance scenarios, while Predis is convenient for rapid deployment; both are suitable for production environments and are well-documented.

How do I stay up-to-date with the latest PHP developments and best practices? How do I stay up-to-date with the latest PHP developments and best practices? Jun 23, 2025 am 12:56 AM

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

What is PHP, and why is it used for web development? What is PHP, and why is it used for web development? Jun 23, 2025 am 12:55 AM

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

How to set PHP time zone? How to set PHP time zone? Jun 25, 2025 am 01:00 AM

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez

See all articles