Skip to content

Commit

Permalink
Merge pull request #6 from Hsnmsri/compress_tool
Browse files Browse the repository at this point in the history
Compress tool
  • Loading branch information
Hsnmsri authored Jan 17, 2024
2 parents beff1e4 + 6302f26 commit 4363f50
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ composer require hsnmsri/pixel
```

## Resize image tool
Pixel PHP is a lightweight image resizing library for PHP, designed to simplify the process of resizing images while providing flexibility and ease of use.

```php
Pixel::resizeImage(imagePath,resizedImagePath,newWidth, newHeight, createPathIfNotExists = false)
Expand All @@ -24,6 +25,7 @@ Pixel::resizeImage(imagePath,resizedImagePath,newWidth, newHeight, createPathIfN
- Error handling with detailed exceptions.

## Change image quality tool
Pixel PHP is a versatile image processing library for PHP, designed to simplify common image operations. The `changeQuality` function allows you to modify the quality of both JPEG and PNG images.

```php
Pixel::changeQuality(imagePath,resizedImagePath,quality, createPathIfNotExists = false)
Expand All @@ -34,4 +36,19 @@ Pixel::changeQuality(imagePath,resizedImagePath,quality, createPathIfNotExists =
- 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.

## Compress Image tool
Pixel PHP is a versatile image processing library for PHP, designed to simplify common image operations. The `compressImage` function allows you to compress and reduce the file size of both JPEG and PNG images without significantly lowering the quality.

```php
Pixel::changeQuality(imagePath,resizedImagePath,compressionLevel = 9, createPathIfNotExists = false)
```

### Features

- Compress and lower the file size of JPEG and PNG images.
- Lossless compression to maintain image quality.
- Support for both local file paths and URLs.
- Option to create the destination directory if it doesn't exist.
- Error handling with detailed exceptions.
76 changes: 76 additions & 0 deletions src/pixel.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,80 @@ public static function changeQuality(string $imagePath, string $newImagePath, in

return true;
}

/**
* Compress and lower the file size of a JPEG or PNG image without lowering the quality and save the result to a new file.
*
* @param string $imagePath Path to the original image.
* @param string $compressedImagePath Path to save the compressed image.
* @param int $compressionLevel Image 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 compression.
* @throws \ErrorException If an error occurs during the process.
*/
public static function compressImage(string $imagePath, string $compressedImagePath, int $compressionLevel = 9, bool $createPathIfNotExists = false): bool
{
// Validate compression level for PNG
if (strtolower(pathinfo($imagePath, PATHINFO_EXTENSION)) == 'png' && ($compressionLevel < 0 || $compressionLevel > 9)) {
throw new \ErrorException('Invalid compression level. The compression parameter should be 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 compressed image directory if needed
$compressedImageDirectory = dirname($compressedImagePath);
if (!is_dir($compressedImageDirectory) && $createPathIfNotExists) {
mkdir($compressedImageDirectory, 0777, true);
}

// Save the compressed image based on its type
switch (strtolower(pathinfo($compressedImagePath, PATHINFO_EXTENSION))) {
case 'jpg':
case 'jpeg':
// For JPEG, use quality 100 for lossless compression
imagejpeg($originalImage, $compressedImagePath, 100);
break;
case 'png':
// For PNG, use the specified compression level (0-9) for lossless compression
imagepng($originalImage, $compressedImagePath, $compressionLevel);
break;
default:
throw new \ErrorException('Unsupported image format for saving the compressed image.');
}

// Free up memory by destroying the image resources
imagedestroy($originalImage);
} catch (\ErrorException $error) {
throw $error;
}

return true;
}

}

0 comments on commit 4363f50

Please sign in to comment.