$image_id = 123; // ID of the image to retrieve // Retrieve image information based on ID $image_info = getimagesize('path/to/image.jpg'); // Create image resource based on file type switch($image_info[2]) { case IMAGETYPE_JPEG: $image = imagecreatefromjpeg('path/to/image.jpg'); break; case IMAGETYPE_PNG: $image = imagecreatefrompng('path/to/image.png'); break; case IMAGETYPE_GIF: $image = imagecreatefromgif('path/to/image.gif'); break; default: echo "Unsupported image format."; exit; } // Display image on webpage header('Content-Type: image/jpeg'); // Set header to image/jpeg for jpeg format imagejpeg($image);
$image_id = 123; // ID of the image to crop // Retrieve image information based on ID $image_info = getimagesize('path/to/image.jpg'); // Create image resource based on file type switch($image_info[2]) { case IMAGETYPE_JPEG: $image = imagecreatefromjpeg('path/to/image.jpg'); break; case IMAGETYPE_PNG: $image = imagecreatefrompng('path/to/image.png'); break; case IMAGETYPE_GIF: $image = imagecreatefromgif('path/to/image.gif'); break; default: echo "Unsupported image format."; exit; } // Crop the image to a specific width and height $cropped = imagecrop($image, ['x' => 0, 'y' => 0, 'width' => 200, 'height' => 200]); // Save the cropped image to a new file imagejpeg($cropped, 'path/to/new-image.jpg');The package library used in these examples is the PHP GD library, which provides a set of functions for image manipulation.