From 7a7f7e002e8367efde6c171afdc1d199725bfb6c Mon Sep 17 00:00:00 2001 From: Hossein Mansouri Date: Wed, 17 Jan 2024 14:17:01 +0330 Subject: [PATCH 1/2] The changeQuality function is defined. --- src/pixel.php | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/src/pixel.php b/src/pixel.php index 9d46d08..d15746d 100644 --- a/src/pixel.php +++ b/src/pixel.php @@ -55,4 +55,77 @@ public static function resizeImage(string $imagePath, string $resizedImagePath, return true; } + + /** + * Change the quality of a JPEG or PNG image and save the result to a new file. + * + * @param string $imagePath Path to the original image. + * @param string $newImagePath Path to save the new image. + * @param int $quality Image quality (0-100) for JPEG format, compression level (0-9) for PNG format. + * @param bool $createPathIfNotExists Whether to create the directory if it doesn't exist (default is false). + * + * @return bool Returns true on successful quality change. + * @throws \ErrorException If an error occurs during the process. + */ + public static function changeQuality(string $imagePath, string $newImagePath, int $quality, bool $createPathIfNotExists = false): bool + { + // Validate quality level + if (($quality < 0 || $quality > 100) || (pathinfo($imagePath, PATHINFO_EXTENSION) == 'png' && ($quality < 0 || $quality > 9))) { + throw new \ErrorException('Invalid quality level. The quality parameter should be between 0 and 100 for JPEG, and between 0 and 9 for PNG.'); + } + + try { + // Check image path type + if (filter_var($imagePath, FILTER_VALIDATE_URL)) { + $originalImage = imagecreatefromstring(file_get_contents($imagePath)); + } else { + if (!file_exists($imagePath)) { + throw new \ErrorException('Original image file not found.'); + } + + $imageExtension = strtolower(pathinfo($imagePath, PATHINFO_EXTENSION)); + switch ($imageExtension) { + case 'jpg': + case 'jpeg': + $originalImage = imagecreatefromjpeg($imagePath); + break; + case 'png': + $originalImage = imagecreatefrompng($imagePath); + break; + default: + throw new \ErrorException('Unsupported image format.'); + } + } + + if (!$originalImage) { + throw new \ErrorException('Failed to create image from the original file or URL.'); + } + + // Check and create the new image directory if needed + $newImageDirectory = dirname($newImagePath); + if (!is_dir($newImageDirectory) && $createPathIfNotExists) { + mkdir($newImageDirectory, 0777, true); + } + + // Save the new image based on its type + switch (strtolower(pathinfo($newImagePath, PATHINFO_EXTENSION))) { + case 'jpg': + case 'jpeg': + imagejpeg($originalImage, $newImagePath, $quality); + break; + case 'png': + imagepng($originalImage, $newImagePath, $quality); + break; + default: + throw new \ErrorException('Unsupported image format for saving the new image.'); + } + + // Free up memory by destroying the image resources + imagedestroy($originalImage); + } catch (\ErrorException $error) { + throw $error; + } + + return true; + } } \ No newline at end of file From f9351448b881463f0cb2688bf180019cd3ae5dea Mon Sep 17 00:00:00 2001 From: Hossein Mansouri Date: Wed, 17 Jan 2024 14:20:35 +0330 Subject: [PATCH 2/2] create change image quality tool section --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index f3d0940..4c86d26 100644 --- a/README.md +++ b/README.md @@ -21,4 +21,17 @@ Pixel::resizeImage(imagePath,resizedImagePath,newWidth, newHeight, createPathIfN - Resize images with specified dimensions. - Support for both local file paths and URLs. - Option to create the destination directory if it doesn't exist. +- Error handling with detailed exceptions. + +## Change image quality tool + +```php +Pixel::changeQuality(imagePath,resizedImagePath,quality, createPathIfNotExists = false) +``` + +### Features + +- Change the quality of JPEG and PNG images. +- Support for both local file paths and URLs. +- Option to create the destination directory if it doesn't exist. - Error handling with detailed exceptions. \ No newline at end of file