Building a CLI for image manipulation allows developers to automate and enhance their PHP projects efficiently.
<?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";
?>
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?