mirror of
https://github.com/Geckon01/simple-image-compressor.git
synced 2026-07-12 02:12:08 +00:00
Small code style imporvements
This commit is contained in:
@@ -57,7 +57,7 @@ class CompressedImage
|
|||||||
imagejpeg($this->imageObject, null, $this->qualityRate);
|
imagejpeg($this->imageObject, null, $this->qualityRate);
|
||||||
break;
|
break;
|
||||||
case "image/png":
|
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;
|
break;
|
||||||
case "image/gif":
|
case "image/gif":
|
||||||
imagegif($this->imageObject);
|
imagegif($this->imageObject);
|
||||||
@@ -81,7 +81,7 @@ class CompressedImage
|
|||||||
imagejpeg($this->imageObject, $filePath.".jpg", $this->qualityRate);
|
imagejpeg($this->imageObject, $filePath.".jpg", $this->qualityRate);
|
||||||
break;
|
break;
|
||||||
case "image/png":
|
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;
|
break;
|
||||||
case "image/gif":
|
case "image/gif":
|
||||||
imagegif($this->imageObject, $filePath.".gif");
|
imagegif($this->imageObject, $filePath.".gif");
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -82,9 +86,9 @@ class SimpleImageCompressor
|
|||||||
|
|
||||||
if (substr($this->imageData, 0, 2) === "\xFF\xD8") {
|
if (substr($this->imageData, 0, 2) === "\xFF\xD8") {
|
||||||
$filetype = 'image/jpeg';
|
$filetype = 'image/jpeg';
|
||||||
} elseif (substr($this->imageData, 0, 3) === "\x89\x50\x4E") {
|
} else if (substr($this->imageData, 0, 3) === "\x89\x50\x4E") {
|
||||||
$filetype = 'image/png';
|
$filetype = 'image/png';
|
||||||
} elseif (substr($this->imageData, 0, 4) === "\x47\x49\x46\x38") {
|
} else if (substr($this->imageData, 0, 4) === "\x47\x49\x46\x38") {
|
||||||
$filetype = 'image/gif';
|
$filetype = 'image/gif';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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);
|
||||||
}
|
}
|
||||||
@@ -150,4 +162,4 @@ class SimpleImageCompressor
|
|||||||
|
|
||||||
return $compressorObject;
|
return $compressorObject;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user