The content of this article is about how to use ImageMagick to generate base64 images (code) in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
In recent PHP projects, drawing and picture splicing effects need to be used. Here are some points used in the development process and some pitfalls that have been encountered. Generate base64 image format through ImageMagick for use by the front end.
Some required knowledge points
PHP converts images to base64 encoding and converts base64 images to images and saves the code
Convert images to base64 encoding
1 2 3 4 5 6 7 8 9 10 11 12 |
|
base64 pictures are converted to pictures and saved
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
base64
Base64 is a method of representing arbitrary binary data using 64 characters.
The principle of Base64 is very simple. First, prepare an array containing 64 characters:
['A', 'B', 'C', ... 'a', ' b', 'c', ... '0', '1', ... ' ', '/']
Then, the binary data is processed, each group of 3 bytes, The total is 3x8=24bit, divided into 4 groups, each group has exactly 6 bits
What if the binary data to be encoded is not a multiple of 3, and there will be 1 or 2 bytes left in the end? After Base64 pads the end with x00 bytes, then adds 1 or 2 = signs at the end of the encoding to indicate how many bytes are padded, which will be automatically removed during decoding.
Use jpg pictures to be smaller than png
Use PHP's Imagick class to operate images
Imagick specific operations
(1). Create a base map, A picture with a width of 750px, a height of 1046px, a white background, and a jpg format
1 2 3 |
|
(2). Add the required picture to the base map
provided that we already know the link address of the picture that needs to be merged
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
(3). Splicing text on the picture
Writing text Take adding store text as an example, and gradually complete the writing of text.
1 2 3 4 5 6 7 8 9 10 11 |
|
Detailed interpretation:
1. Instantiate the ImagickDraw class:
1 |
|
2. Set the font color
1 |
|
3. Set the font size
1 |
|
4. Set the font format
1 |
|
5. Set the font direction
1 |
|
ps: Imagick::ALIGN_RIGHT to the right Imagick::ALIGN_LEFT to the left Imagick::ALIGN_CENTER in the middle
6. Set the font encoding
1 |
|
7. Draw the text
1 |
|
8. Write the font on the base map
1 |
|
Writing text Some pitfalls in this place :
When the font format is not set, Chinese characters will be parsed incorrectly
(no problem with English)
(Chinese character parsing Failure)
(Set the font format to display normally)
The final image our group needs to be passed to the front end in base64 format. Perform the following operations to convert and output the final spliced image to base64.
1 2 3 4 5 6 7 8 9 |
|
$base64_image
is a picture in base64 format.
It should be noted that the base64 data obtained by the front end contains the '\r\n'
carriage return character, which requires special processing to display the image correctly.
The final merged image will be obtained. You can adjust the size of the spliced image to obtain different images.
Related recommendations:
Upload images and use ImageMagick to generate thumbnails
php to upload images and use ImageMagick generates thumbnails,
The above is the detailed content of How to use ImageMagick to generate base64 images in php (code). For more information, please follow other related articles on the PHP Chinese website!