Work needs, write a simple PHP image watermark function. & Lt ;? Php & nbsp;/* & nbsp; Image watermarking function & nbsp; $ imag_url indicates the path of the image to be watermarked & nbsp; $ str indicates the string to be added to the image & nbsp; $ route: the storage path after adding a watermark & nbsp; */& nbsp; function & nbsp required for work. a simple function is to add a watermark to a PHP image.
-
- /*
- Image watermarking function
- $ Imag_url is the path of the image to be watermarked
- $ Str: string to be added to the image
- $ Path for storing the route watermark
- */
- Function watermark ($ imag_url, $ str, $ route ){
- $ Type_array = explode (., $ imag_url );
- $ Imag_type = $ type_array [count ($ type_array)-1];
- Switch ($ imag_type ){
- Case gif: $ img = imagecreatefromgif ($ imag_url); break;
- Case jpeg: $ img = imagecreatefromjpeg ($ imag_url); break;
- Case jpg: $ img = imagecreatefromjpeg ($ imag_url); break;
- Case png: $ img = imagecreatefrompng ($ imag_url); break;
- Default: $ img = imagecreatefromgif ($ imag_url); break;
- }
- // Font color
- $ Gray = imagecolorallocate ($ img, 235,235,235 );
- $ Pink = imagecolorallocate ($ img, 255,128,255 );
- /* $ Fontfile font path, depending on the operating system, can be simhei. ttf (), SIMKAI. TTF (entity body), SIMFANG. TTF (Imitation Song), SIMSUN. chinese fonts supported by GD, such as TTC (& */
- $ Fontfile = "C: WINDOWSFontsSIMHEI. TTF ";
- $ Str = iconv (GB2312, UTF-8, $ str);/* convert the character set of gb2312 to the character set of the UTF-8 */
- Imagettftext ($ img, 30, 0, 0,200, $ pink, $ fontfile, $ str );
- /* Add a Chinese watermark */
- Imagepng ($ img, $ route );
- Imagedestroy ($ img );
- }
- ?>
The above function is expected to benefit everyone.