Resizing and changing the format of an image using PHP’s GD library involves loading the image, creating a new true color image with the desired dimensions, and then saving it in the desired format. Let’s walk through a detailed example.
<?php class ImageProcessor { private $sourcePath; private $destinationPath; private $newWidth; private $newHeight; public function __construct($sourcePath, $destinationPath, $newWidth, $newHeight) { $this->sourcePath = $sourcePath; $this->destinationPath = $destinationPath; $this->newWidth = $newWidth; $this->newHeight = $newHeight; } // Load an image from a file private function loadImage($filePath) { $imageInfo = getimagesize($filePath); switch ($imageInfo[2]) { case IMAGETYPE_JPEG: return imagecreatefromjpeg($filePath); case IMAGETYPE_PNG: return imagecreatefrompng($filePath); case IMAGETYPE_GIF: return imagecreatefromgif($filePath); default: throw new Exception("Unsupported image type."); } } // Resize and change image format public function resizeAndChangeFormat($newFormat = 'jpeg') { // Load the original image $sourceImage = $this->loadImage($this->sourcePath); // Create a new true color image with the specified dimensions $resizedImage = imagecreatetruecolor($this->newWidth, $this->newHeight); // Get original dimensions $originalWidth = imagesx($sourceImage); $originalHeight = imagesy($sourceImage); // Resize the original image into the new image imagecopyresampled($resizedImage, $sourceImage, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $originalWidth, $originalHeight); // Output the image in the desired format switch (strtolower($newFormat)) { case 'jpeg': case 'jpg': imagejpeg($resizedImage, $this->destinationPath); break; case 'png': imagepng($resizedImage, $this->destinationPath); break; case 'gif': imagegif($resizedImage, $this->destinationPath); break; default: throw new Exception("Unsupported output format."); } // Free up memory imagedestroy($sourceImage); imagedestroy($resizedImage); } } // Usage example try { $source = '/path/to/source/image.jpg'; // Path to source image $destination = '/path/to/destination/image.png'; // Path to save the new image $width = 200; // New width $height = 100; // New height $format = 'png'; // The desired output format $processor = new ImageProcessor($source, $destination, $width, $height); $processor->resizeAndChangeFormat($format); echo "Image resized and format changed successfully!"; } catch (Exception $e) { echo "Error: " . $e->getMessage(); }
ImageProcessor
class is initialized with source and destination paths, and the desired width and height for the new image.loadImage()
method checks the image type using getimagesize()
and loads it with the appropriate GD function: imagecreatefromjpeg()
, imagecreatefrompng()
, or imagecreatefromgif()
.imagecreatetruecolor()
for the target dimensions.imagesx()
and imagesy()
functions retrieve the original dimensions of the source image.imagecopyresampled()
handles the actual resizing, maintaining quality by using a resampling algorithm.imagejpeg()
, imagepng()
, or imagegif()
.imagedestroy()
is called for both the original and resized images to free up resources.loadImage()
and resizeAndChangeFormat()
.WordPress development has evolved significantly, and modern tooling plays a crucial role in creating efficient…
I. Project Overview The goal is to automate the process of notifying search engines (like…
1. Database Structure (MySQL) We'll need a database table to store information about our website's…
This explanation aims to provide a solid foundation for understanding the process and implementing your…
Okay, here's a comprehensive guide on building a real-time website chat script using PHP, HTML,…
Comprehensive guide on creating a simple website analytics system using PHP, HTML, CSS, JavaScript, and…