How does PHP interact with website design elements like images that may cause visual disruptions?

When using PHP to interact with website design elements like images that may cause visual disruptions, it's important to ensure that the images are properly sized and formatted to fit within the layout of the website. One way to solve this issue is by using PHP to dynamically resize images based on the specific dimensions required by the website design.

<?php
// Specify the desired width and height for the image
$desired_width = 300;
$desired_height = 200;

// Get the original image dimensions
list($width, $height) = getimagesize("image.jpg");

// Calculate the new dimensions while maintaining aspect ratio
if ($width > $height) {
    $new_width = $desired_width;
    $new_height = floor($height * ($desired_width / $width));
} else {
    $new_height = $desired_height;
    $new_width = floor($width * ($desired_height / $height));
}

// Create a new image with the desired dimensions
$new_image = imagecreatetruecolor($new_width, $new_height);
$original_image = imagecreatefromjpeg("image.jpg");
imagecopyresized($new_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

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

// Free up memory
imagedestroy($new_image);
imagedestroy($original_image);
?>