// Load the image $image = imagecreatefromjpeg('image.jpg'); // Get the image dimensions $width = imagesx($image); $height = imagesy($image); // Calculate the new width and height $newWidth = $width / 2; $newHeight = $height / 2; // Create a new image with the new dimensions $newImage = imagecreatetruecolor($newWidth, $newHeight); // Resize the original image to the new dimensions imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); // Save the new image to a file imagejpeg($newImage, 'resized_image.jpg', 90); // Free up memory imagedestroy($image); imagedestroy($newImage);
// Load the image $image = imagecreatefromjpeg('image.jpg'); // Define the text and font $text = 'Hello World!'; $font = 'arial.ttf'; // Define the font size and color $size = 20; $color = imagecolorallocate($image, 255, 255, 255); // Get the image dimensions $width = imagesx($image); $height = imagesy($image); // Calculate the text box dimensions $textBox = imagettfbbox($size, 0, $font, $text); $textWidth = $textBox[2] - $textBox[0]; $textHeight = $textBox[7] - $textBox[1]; // Calculate the text position $x = $width / 2 - $textWidth / 2; $y = $height / 2 + $textHeight / 2; // Add the text to the image imagettftext($image, $size, 0, $x, $y, $color, $font, $text); // Save the new image to a file imagejpeg($image, 'text_image.jpg', 90); // Free up memory imagedestroy($image);This example uses the GD library to add text to an image. It first loads the image using `imagecreatefromjpeg`, and defines the text, font, and font size. It then calculates the dimensions of the text box using `imagettfbbox`, and calculates the position of the text. Finally, it adds the text to the image using `imagettftext`, and saves the new image to a file using `imagejpeg`.