Home > Backend Development > PHP Tutorial > How to implement image generation and watermark processing of Baidu Wenxin Yiyan random sentences in PHP development?

How to implement image generation and watermark processing of Baidu Wenxin Yiyan random sentences in PHP development?

王林
Release: 2023-08-26 20:42:01
Original
1173 people have browsed it

How to implement image generation and watermark processing of Baidu Wenxin Yiyan random sentences in PHP development?

How to implement image generation and watermark processing of Baidu Wenxin Yiyan random statements in PHP development?

In web development, it is often necessary to add some text or watermarks to pictures to increase the information content and interactivity of the pictures. This article will introduce how to use PHP development to implement a simple image generation and watermark processing function, in which Baidu Himor will be our random statement data source.

First, we need to get Wen Xinyiyan’s random sentences. Baidu Wenxin Yiyan provides an open API that can obtain random statement data by sending HTTP requests. The following is an example of using PHP code to obtain random statements:

$url = 'https://api.btstu.cn/yan/api.php';  
$response = file_get_contents($url);
$data = json_decode($response, true);
$random_sentence = $data['text'];
Copy after login

The above code sends an HTTP request through the file_get_contents function, obtains Wen Xinyiyan's random statements, and parses the returned JSON data into an associative array. We can take out the text field from the array and get the content of the random statement.

Next, we will use PHP's GD library to generate images. The GD library is an extension library for PHP that provides a series of functions for processing images. By calling these functions, we can draw text on the picture and set the style, size and color of the text. The following is a simple example of using the GD library to generate images:

$width = 500;
$height = 200;
$image = imagecreatetruecolor($width, $height);
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
$font = 'path/to/font.ttf';
imagettftext($image, 30, 0, 50, 100, $text_color, $font, $random_sentence);
imagepng($image, 'path/to/image.png');
imagedestroy($image);
Copy after login

The above code first creates a blank image of a specified size, and sets the background color and text color. Then, use the imagettftext function to draw text on the picture, specifying the size, angle, position and color of the text. Finally, save the generated image to the specified path through the imagepng function.

Finally, if we want to add a watermark to the generated image, we can use the ImageCopyMerge function provided by the GD library. The following is a simple example of using the GD library to add a watermark to an image:

$source_image = imagecreatefrompng('path/to/source_image.png');
$watermark_image = imagecreatefrompng('path/to/watermark.png');
$watermark_width = imagesx($watermark_image);
$watermark_height = imagesy($watermark_image);
$source_width = imagesx($source_image);
$source_height = imagesy($source_image);
$pos_x = $source_width - $watermark_width - 10;
$pos_y = $source_height - $watermark_height - 10;
imagecopymerge($source_image, $watermark_image, $pos_x, $pos_y, 0, 0, $watermark_width, $watermark_height, 50);
imagepng($source_image, 'path/to/output_image.png');
imagedestroy($source_image);
Copy after login

The above code first loads the original image and the watermark image, and obtains their width and height. Then, by adjusting the position of the watermark image, use the ImageCopyMerge function to merge the watermark image onto the original image. Finally, save the processed image to the specified path through the imagepng function.

Through the above example code, we can easily implement the image generation and watermark processing functions in PHP development, and use the random statements provided by Baidu Wenxinyiyan to add more interaction and interest to the images.

The above is the detailed content of How to implement image generation and watermark processing of Baidu Wenxin Yiyan random sentences in PHP development?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template