How can server-side image processing functions like imagecreatefromjpeg() be utilized effectively for dynamic image generation in PHP?

Server-side image processing functions like imagecreatefromjpeg() can be utilized effectively for dynamic image generation in PHP by allowing you to manipulate and create images on the server before serving them to the client. This can be useful for tasks such as generating thumbnails, adding watermarks, or dynamically creating charts and graphs.

// Example of utilizing imagecreatefromjpeg() for dynamic image generation
$width = 200;
$height = 200;

// Create a blank image with the specified dimensions
$image = imagecreatetruecolor($width, $height);

// Load a JPEG image from a file
$jpeg_image = imagecreatefromjpeg('example.jpg');

// Resize the JPEG image and copy it onto the blank image
imagecopyresampled($image, $jpeg_image, 0, 0, 0, 0, $width, $height, imagesx($jpeg_image), imagesy($jpeg_image));

// Output the image to the browser
header('Content-Type: image/jpeg');
imagejpeg($image);

// Free up memory
imagedestroy($image);
imagedestroy($jpeg_image);