In PHP image manipulation, how do I build a CLI?

Building a CLI for image manipulation allows developers to automate and enhance their PHP projects efficiently.

Keywords: PHP, CLI, image manipulation, GD Library, resize image, PHP image processing
Description: This PHP CLI example demonstrates how to manipulate images using the GD Library, allowing you to resize images directly from the terminal.
<?php // Check if the command line arguments are provided if ($argc != 4) { echo "Usage: php resize_image.php \n"; exit(1); } // Get parameters $sourceImage = $argv[1]; $destinationImage = $argv[2]; $newWidth = (int)$argv[3]; // Create a new image from file list($width, $height) = getimagesize($sourceImage); $src = imagecreatefromjpeg($sourceImage); // Calculate new height $newHeight = ($height / $width) * $newWidth; // Create a new temporary image $temp = imagecreatetruecolor($newWidth, $newHeight); // Resize the image imagecopyresampled($temp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); // Save the resized image imagejpeg($temp, $destinationImage, 90); // Clean up imagedestroy($src); imagedestroy($temp); echo "Image resized successfully to $newWidth x $newHeight and saved as $destinationImage\n"; ?>

Keywords: PHP CLI image manipulation GD Library resize image PHP image processing