Skip to content

Commit

Permalink
Merge pull request #5 from Hsnmsri/quality_tool
Browse files Browse the repository at this point in the history
Quality tool
  • Loading branch information
Hsnmsri authored Jan 17, 2024
2 parents 087ff19 + f935144 commit beff1e4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
73 changes: 73 additions & 0 deletions src/pixel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit beff1e4

Please sign in to comment.