How does the browser handle image size retrieval when fetching images from external servers in PHP?

When fetching images from external servers in PHP, the browser does not automatically handle resizing the images to fit the display. To ensure the image size is appropriate for the display, you can use PHP to fetch the image, resize it using libraries like GD or Imagick, and then output the resized image to the browser.

// Fetch image from external server
$image_url = 'https://example.com/image.jpg';
$image = file_get_contents($image_url);

// Resize image
$original_image = imagecreatefromstring($image);
$width = imagesx($original_image);
$height = imagesy($original_image);
$new_width = 300; // Set desired width
$new_height = ($height / $width) * $new_width;
$resized_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($resized_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output resized image to browser
header('Content-Type: image/jpeg');
imagejpeg($resized_image);
imagedestroy($original_image);
imagedestroy($resized_image);