In web development, it's often necessary to add text or watermarks to images to increase their informational value and interactivity. This article will teach you how to use PHP to generate images with random sentences from Baidu Wenxin Yiyan API and add watermarks using the GD library.
First, we need to obtain random sentences from Wenxin Yiyan. Baidu offers an open API for Wenxin Yiyan, and we can fetch random sentence data through an HTTP request. Here's an example of how to get a random sentence using PHP:
$url = 'https://api.btstu.cn/yan/api.php';
$response = file_get_contents($url);
$data = json_decode($response, true);
$random_sentence = $data['text'];
The code above uses the `file_get_contents` function to send an HTTP request and fetch a random sentence from Wenxin Yiyan. It then decodes the returned JSON data into a PHP array and extracts the `text` field to get the random sentence.
Next, we'll use PHP's GD library to generate an image. GD is a powerful image manipulation library in PHP that provides various functions for image creation, text drawing, and color manipulation. Here's a simple example of how to use the GD library to create an image and draw the random sentence on it:
$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);
The above code creates a blank image with a specified size and sets the background and text colors. It then uses the `imagettftext` function to draw the random sentence on the image. Finally, the `imagepng` function saves the generated image to the specified path.
If you want to add a watermark to the generated image, the GD library provides an easy way to do so. You can use the `ImageCopyMerge` function to overlay the watermark image onto the original image. Here's an example of adding a watermark:
$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);
In the code above, we first load the source image and watermark image, get their dimensions, and then use `ImageCopyMerge` to overlay the watermark on the source image at a specified position. Finally, we save the processed image using `imagepng`.
By using PHP's GD library and the Baidu Wenxin Yiyan API, we can easily implement image generation and watermark processing. The combination of random sentences and watermarks adds interactivity and fun to images, making your website more engaging and visually appealing.