thinkphp image watermark and text watermark

Source: Internet
Author: User

1.Thinkphp image Processing

In the TP frame, we often use the image upload, I recently wrote a lot about the picture upload article, thinkphp picture upload +validate form verification + picture Trojan detection + thumbnail generation, and so on, today write about the picture after the successful upload to the image watermark text or add a picture watermark,

  

1.1 Picture processing classes and libraries

First, there are image processing features in the Thinkphp\think\image class that support the GD library and the Imagick library, including support for GIF image processing.

  

1.2 Instantiating a class library  

$image New

    After instantiating the image, the default is to use the GD library for imaging operations, and if you need to use the Imagick library operation, you need to write the instantiation:

$image new \think\image (\think\image:://  or using $imagenew \think\ Image (' Imagick ');

    If the GD library or Imageick library do not understand, you can refer to http://php.net/manual/zh/book.image.php, the GD library and Imageick Library has a detailed explanation, and is the Chinese version, especially easy to understand!

    1.3 Opening a picture resource

  If we want to process the image, the first step is to open the image resource first:

Open the image file using the opening method to do the related operation:

$image New  $image->open ('./1.jpg ');        // point to the picture resource you want to open

    Cross the image, point the open path to the upload file you want to open, and then work on the image.

   

1.4    Get image information

  After the open image file, we want to get the information of the image:

$image=New\think\image ();$image->open ($file);//the path points to the picture you uploaded$width=$image->width ();//returns the width of the picture$height=$image->height ();//returns the height of the picture$type=$image->type ();//return the type of picture$mime=$image->mime ();//returns the MIME type of a picture$size=$image->size ();//returns the size of the picture array 0 picture width 1 picture height

    So we can get the information of the picture, then we can save some information in the database or continue to process the image.

   1.5 Add a picture watermark

Next, we can add watermarks to the images we upload.

$image New  //  Add a watermark in the upper left corner of the picture (the watermark file is located in./logo.png) and saved as Water.jpg$image->open ('./1.jpg ') Water ('./logo.png ', \think\image::image_water_center)->save ("water.jpg");

    , is to put Logo.png Watermark to 1.jpg of 5 (centered) position

   

The location of the image watermark is determined according to the following code, we think the picture watermark where, can change according to their own needs

 image_water_northwest = 1; //  Image_water_north = 2; //  Image_water_northeast = 3; //  Image_water_west = 4; //  Image_water_center = 5; //  center watermark  Image_water_east = 6; //  right center watermark  Image_water_southwest = 7; //  lower left corner watermark  Image_water_south = 8; //  Image_water_southeast = 9; //  

    You can also support the transparency of the watermark image (0~100, the default value is 80)

$image New  //  Add a watermark in the upper left corner of the picture (the watermark file is located in./logo.png) The opacity of the watermark picture is 50 and saved as water.jpg$image

    1.6 Adding a text watermark to a picture

$image New  //  in the lower right corner of the picture, add the watermark text thinkphp and save it as New.jpg$image

Detailed parameters: Text (' to watermark text ', font, font size, font color, ' Text to Watermark location ');

   

    Here, the production will appear when the watermark text garbled problem:

    Solution: 1. Garbled or appear box, because you choose the font reason, some fonts do not support Chinese, when we want to watermark Chinese will appear garbled problem, this time, we want to download support Chinese font, for example: Microsoft ya black, song body ...

2. Most of the fonts are supported in English, so we should try to watermark the English alphabet as much as possible, so it is not easy to make mistakes.

3. We want to watermark some strange words, such as Korean, Japanese ..., we need to download the corresponding font.

4. If our font is only for Windows, we can choose the font we want to use in the C:\Windows\Fonts (not very recommended, we want to use, to download the latest fonts, more comprehensive, and support the generation of more words).

   

thinkphp image watermark and text watermark

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.