8 Commits

Author SHA1 Message Date
DESKTOP-H825VC6\geckon01 1baf7ae2ca Small code style improvements 2023-10-17 02:14:37 +03:00
DESKTOP-H825VC6\geckon01 a7461536bc Bump package version 2023-10-17 02:03:17 +03:00
DESKTOP-H825VC6\geckon01 f9b21bcf05 Added support for webp and bmp 2023-10-17 02:00:50 +03:00
DESKTOP-H825VC6\geckon01 de877f3b6a Better image type recognition with exif 2023-10-17 02:00:17 +03:00
Andrew 10338aec18 Update README.md - fix some typo 2023-06-09 20:21:32 +03:00
Andrew 1e0f13a7a6 Update README.md 2023-03-30 10:17:48 +03:00
geckon01 a538362343 Small code style imporvements 2023-03-30 01:17:41 +03:00
geckon01 a6f42255c8 Updated readme.md 2023-03-30 00:45:07 +03:00
4 changed files with 101 additions and 35 deletions
+15 -6
View File
@@ -2,12 +2,23 @@
# SimpleImageCompressor
[![Software License][ico-license]](LICENSE.md)
**SimpleImageCompressor** - is a tiny simple PHP image compressor lib which allows you to compress any image easily on the fly.
![Version](https://img.shields.io/packagist/v/geckon01/simple-image-compressor)
![Lecense](https://img.shields.io/badge/license-MIT-green)
![Downloads](https://img.shields.io/packagist/dt/geckon01/simple-image-compressor)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/d773335a657d467faaa0ebb12bc2abe1)](https://app.codacy.com/gh/Geckon01/simple-image-compressor/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
**SimpleImageCompressor** - is a tiny simple PHP image resizer/compressor lib which allows you to resize and compress any image easily on the fly.
## Installation
### With composer
```bash
composer require "geckon01/simple-image-compressor"
```
### Without composer
1. Download latest release [here](https://github.com/Geckon01/simple-image-compressor/releases).
2. Unpack archive to any folder of your project you wish.
3. Load main lib files:
@@ -22,10 +33,10 @@ use geckon01\SimpleImageCompressor\SimpleImageCompressor;
To resize and compress your image you can use next code:
```php
$resulutionTargetPercent = 50;
$resolutionTargetPercent = 50;
$targetQuality = 50;
$compressor = SimpleImageCompressor::load("image.png");
$compressedImage = $compressor->resizeAndCompress($resulutionTargetPercent, $targetQuality);
$compressedImage = $compressor->resizeAndCompress($resolutionTargetPercent, $targetQuality);
$compressedImage->toFile("image");
```
load method supports loading from local file, or you can specify any valid URL image link like this:
@@ -65,5 +76,3 @@ As example, if we have original image 1920x1080 which we want to get 50% of orig
## License
This software is licensed under the MIT License. [View the license](LICENSE.md).
[ico-license]: https://img.shields.io/badge/license-MIT-green
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "geckon01/simple-image-compressor",
"description": "SimpleImageCompressor is a tiny simple PHP image compressor lib which allows you to compress any image easily on the fly",
"version": "0.3.1",
"version": "0.3.10.17",
"keywords": [
"php",
"images",
+14 -2
View File
@@ -57,11 +57,17 @@ class CompressedImage
imagejpeg($this->imageObject, null, $this->qualityRate);
break;
case "image/png":
imagepng( $this->imageObject, null, self::mapValue($this->qualityRate, 0, 100, 0, 9));
imagepng($this->imageObject, null, self::mapValue($this->qualityRate, 0, 100, 0, 9));
break;
case "image/gif":
imagegif($this->imageObject);
break;
case "image/webp":
imagewebp($this->imageObject);
break;
case "image/bmp":
imagebmp($this->imageObject);
break;
}
$base64_output = ob_get_contents();
ob_end_clean();
@@ -81,11 +87,17 @@ class CompressedImage
imagejpeg($this->imageObject, $filePath.".jpg", $this->qualityRate);
break;
case "image/png":
imagepng( $this->imageObject, $filePath.".png", self::mapValue($this->qualityRate, 0, 100, 0, 9));
imagepng($this->imageObject, $filePath.".png", self::mapValue($this->qualityRate, 0, 100, 0, 9));
break;
case "image/gif":
imagegif($this->imageObject, $filePath.".gif");
break;
case "image/webp":
imagewebp($this->imageObject, $filePath.".webp");
break;
case "image/bmp":
imagebmp($this->imageObject, $filePath.".bmp");
break;
}
}
+71 -26
View File
@@ -8,13 +8,15 @@
namespace geckon01\SimpleImageCompressor;
use http\Exception\BadUrlException;
/**
* Class SimpleImageCompressor
* @package geckon01\SimpleImageCompressor
*/
class SimpleImageCompressor
{
private const ALLOWED_IMAGE_FORMAT = "image/jpeg,image/png,image/gif";
private const ALLOWED_IMAGE_FORMAT = "image/jpeg,image/png,image/gif,image/webp,image/bmp";
private string $imageResourceUrl;
private string $imageData;
private string $imageType;
@@ -22,15 +24,20 @@ class SimpleImageCompressor
private int $approxMinimumHeight = 90;
private int $approxMinimumWidth = 90;
private bool $exifLoaded = false;
/**
* SimpleImageCompressor constructor.
* @param $url
* @param $url url or path from where to load file
*/
private function __construct($url)
{
$this->imageResourceUrl = $url;
$this->exifLoaded = in_array("exif", get_loaded_extensions());
}
/**
* Returns current loaded image type
* @return string
@@ -78,17 +85,47 @@ class SimpleImageCompressor
* Determine current loaded image type
*/
private function loadImageType() {
$filetype = '';
$this->imageType = "image";
if($this->exifLoaded) {
switch (exif_imagetype($this->imageResourceUrl))
{
case IMAGETYPE_GIF:
$this->imageType = 'image/gif';
break;
case IMAGETYPE_JPEG:
$this->imageType = 'image/jpeg';
break;
case IMAGETYPE_PNG:
$this->imageType = 'image/png';
break;
case IMAGETYPE_BMP:
$this->imageType = 'image/bmp';
break;
case IMAGETYPE_WEBP:
$this->imageType = 'image/webp';
break;
case IMAGETYPE_ICO:
$this->imageType = 'image/ico';
break;
default:
break;
}
return;
}
// Fallback to bytes recognition
trigger_error("simple-image-compressor: Exif extension not found. Image MIME type recognition may be inaccurate.");
if (substr($this->imageData, 0, 2) === "\xFF\xD8") {
$filetype = 'image/jpeg';
} elseif (substr($this->imageData, 0, 3) === "\x89\x50\x4E") {
$filetype = 'image/png';
} elseif (substr($this->imageData, 0, 4) === "\x47\x49\x46\x38") {
$filetype = 'image/gif';
$this->imageType = 'image/jpeg';
}
if (substr($this->imageData, 0, 3) === "\x89\x50\x4E") {
$this->imageType = 'image/png';
}
if (substr($this->imageData, 0, 4) === "\x47\x49\x46\x38") {
$this->imageType = 'image/gif';
}
$this->imageType = $filetype;
}
/**
@@ -96,38 +133,46 @@ class SimpleImageCompressor
*/
private function readImageToString(): void {
$imageData = file_get_contents($this->imageResourceUrl);
if($imageData === false)
throw new BadUrlException("Cannot load image from provided resource: ".$this->imageResourceUrl);
$this->imageData = $imageData;
}
/**
* Resizes and compressing image.
* Note that gif compression not supported
* @param int $resolutionReductionPercent percent shows how much image resolution to original will be. The greater percent, the lower resolution
* @param int $reductionPercent percent shows how much image resolution to original will be. The greater percent, the lower resolution
* @param int $quality
* @return CompressedImage
*/
public function resizeAndCompress($resolutionReductionPercent = 5, $quality = 90): CompressedImage
public function resizeAndCompress($reductionPercent = 5, $quality = 90): CompressedImage
{
$im = imagecreatefromstring($this->imageData);
$width = imagesx($im);
$height = imagesy($im);
$originImage = imagecreatefromstring($this->imageData);
if($originImage === false)
throw new \Exception("Can not read provided file");
$width = imagesx($originImage);
$height = imagesy($originImage);
$totalPixelCount = $width * $height;
$minimumPixelCount = $this->approxMinimumWidth * $this->approxMinimumHeight;
$maximumResolutionReductionPercent = round(abs(100 - ($minimumPixelCount / $totalPixelCount * 100)));
$maxReductionPercent = round(abs(100 - ($minimumPixelCount / $totalPixelCount * 100)));
//Due to saving proportion we can't guarantee that width and height be equals max and min
//As example, if we have original image 1920*1080 which we want to get 50% of original resolution
//If we want to save 16*9 aspect ration it must be 960*540
//So, we override $maximumResolutionReductionPercent to value which satisfy origin aspect ratio
if($maximumResolutionReductionPercent < $resolutionReductionPercent)
$resolutionReductionPercent = $maximumResolutionReductionPercent;
// Due to saving proportion we can't guarantee that width and height be equals max and min
// As example, if we have original image 1920*1080 which we want to get 50% of original resolution
// If we want to save 16*9 aspect ration it must be 960*540
// So, we override $maxReductionPercent to value which satisfy origin aspect ratio
if($maxReductionPercent < $reductionPercent)
$reductionPercent = $maxReductionPercent;
$newWidth = round($width - ($width * $resolutionReductionPercent) / 100);
$newHeight = round($height - ($height * $resolutionReductionPercent) / 100);
$newWidth = round($width - ($width * $reductionPercent) / 100);
$newHeight = round($height - ($height * $reductionPercent) / 100);
$thumb = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($thumb, $im, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagecopyresized($thumb, $originImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
return new CompressedImage($quality, $this->imageType,$thumb);
}
@@ -150,4 +195,4 @@ class SimpleImageCompressor
return $compressorObject;
}
}
}