mirror of
https://github.com/Geckon01/simple-image-compressor.git
synced 2026-07-12 03:02:05 +00:00
Small code style imporvements
This commit is contained in:
@@ -8,6 +8,8 @@
|
|||||||
|
|
||||||
namespace geckon01\SimpleImageCompressor;
|
namespace geckon01\SimpleImageCompressor;
|
||||||
|
|
||||||
|
use http\Exception\BadUrlException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class SimpleImageCompressor
|
* Class SimpleImageCompressor
|
||||||
* @package geckon01\SimpleImageCompressor
|
* @package geckon01\SimpleImageCompressor
|
||||||
@@ -22,6 +24,7 @@ class SimpleImageCompressor
|
|||||||
private int $approxMinimumHeight = 90;
|
private int $approxMinimumHeight = 90;
|
||||||
private int $approxMinimumWidth = 90;
|
private int $approxMinimumWidth = 90;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SimpleImageCompressor constructor.
|
* SimpleImageCompressor constructor.
|
||||||
* @param $url
|
* @param $url
|
||||||
@@ -31,6 +34,7 @@ class SimpleImageCompressor
|
|||||||
$this->imageResourceUrl = $url;
|
$this->imageResourceUrl = $url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns current loaded image type
|
* Returns current loaded image type
|
||||||
* @return string
|
* @return string
|
||||||
@@ -96,38 +100,46 @@ class SimpleImageCompressor
|
|||||||
*/
|
*/
|
||||||
private function readImageToString(): void {
|
private function readImageToString(): void {
|
||||||
$imageData = file_get_contents($this->imageResourceUrl);
|
$imageData = file_get_contents($this->imageResourceUrl);
|
||||||
|
|
||||||
|
if($imageData === false)
|
||||||
|
throw new BadUrlException("Cannot load image from provided resource: ".$this->imageResourceUrl);
|
||||||
|
|
||||||
$this->imageData = $imageData;
|
$this->imageData = $imageData;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resizes and compressing image.
|
* Resizes and compressing image.
|
||||||
* Note that gif compression not supported
|
* 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
|
* @param int $quality
|
||||||
* @return CompressedImage
|
* @return CompressedImage
|
||||||
*/
|
*/
|
||||||
public function resizeAndCompress($resolutionReductionPercent = 5, $quality = 90): CompressedImage
|
public function resizeAndCompress($reductionPercent = 5, $quality = 90): CompressedImage
|
||||||
{
|
{
|
||||||
$im = imagecreatefromstring($this->imageData);
|
$originImage = imagecreatefromstring($this->imageData);
|
||||||
$width = imagesx($im);
|
|
||||||
$height = imagesy($im);
|
if($originImage === false)
|
||||||
|
throw new \Exception("Can not read provided file");
|
||||||
|
|
||||||
|
$width = imagesx($originImage);
|
||||||
|
$height = imagesy($originImage);
|
||||||
|
|
||||||
$totalPixelCount = $width * $height;
|
$totalPixelCount = $width * $height;
|
||||||
$minimumPixelCount = $this->approxMinimumWidth * $this->approxMinimumHeight;
|
$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
|
// 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
|
// 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
|
// 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
|
// So, we override $maxReductionPercent to value which satisfy origin aspect ratio
|
||||||
if($maximumResolutionReductionPercent < $resolutionReductionPercent)
|
if($maxReductionPercent < $reductionPercent)
|
||||||
$resolutionReductionPercent = $maximumResolutionReductionPercent;
|
$reductionPercent = $maxReductionPercent;
|
||||||
|
|
||||||
$newWidth = round($width - ($width * $resolutionReductionPercent) / 100);
|
$newWidth = round($width - ($width * $reductionPercent) / 100);
|
||||||
$newHeight = round($height - ($height * $resolutionReductionPercent) / 100);
|
$newHeight = round($height - ($height * $reductionPercent) / 100);
|
||||||
|
|
||||||
$thumb = imagecreatetruecolor($newWidth, $newHeight);
|
$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);
|
return new CompressedImage($quality, $this->imageType,$thumb);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user