Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Image
- {
- const MaxWH = 2048;
- private static $ext;
- private static $image;
- private static $newImage;
- private static $origWidth;
- private static $origHeight;
- private static $resizeWidth;
- private static $resizeHeight;
- private static $newWidth;
- private static $newHeight;
- public static function thin($path, $wAh = [])
- {
- if (!file_exists($path)) {
- return false;
- }
- list($dirname, $basename, $extension, $filename) = array_values(pathinfo($path));
- $size = getimagesize($path);
- self::$ext = $size['mime'];
- self::detectType($path);
- self::$origWidth = imagesx(self::$image);
- self::$origHeight = imagesy(self::$image);
- self::resizeTo($wAh);
- if ($wAh['width'] || $wAh['height']) {
- $dir = $dirname . '/' . self::$resizeWidth . 'x' . self::$resizeHeight . '/';
- @mkdir($dir, 0777, true);
- } else {
- $dir = $dirname . '/';
- }
- if (self::save($dir . $basename, 85)) {
- return true;
- } else {
- return false;
- }
- }
- private static function detectType($path)
- {
- switch (self::$ext) {
- case 'image/jpg':
- case 'image/jpeg':
- self::$image = imagecreatefromjpeg($path);
- break;
- case 'image/gif':
- self::$image = @imagecreatefromgif($path);
- break;
- case 'image/png':
- self::$image = @imagecreatefrompng($path);
- break;
- default:
- return false;
- }
- }
- private static function resizeTo($args)
- {
- $width = self::detectWaH($args['width'], self::$origWidth);
- $height = self::detectWaH($args['height'], self::$origHeight);
- $option = ($args['option']) ?: 'default';
- $crop_x = 0;
- $crop_y = 0;
- switch (strtolower($option)) {
- case 'exact':
- self::$resizeWidth = $width;
- self::$resizeHeight = $height;
- $original_aspect = self::$origWidth / self::$origHeight;
- $thumb_aspect = $width / $height;
- if ($original_aspect >= $thumb_aspect) {
- $new_width = (self::$origWidth / (self::$origHeight / $height));
- self::$newWidth = $new_width;
- self::$newHeight = $height;
- } else {
- $new_height = (self::$origHeight / (self::$origWidth / $width));
- self::$newWidth = $width;
- self::$newHeight = $new_height;
- }
- $crop_x = 0 - (self::$newWidth - $width) / 2;
- $crop_y = 0 - (self::$newHeight - $height) / 2;
- break;
- case 'maxwidth':
- self::$resizeWidth = $width;
- self::$resizeHeight = self::resizeHeightByWidth($width);
- break;
- case 'maxheight':
- self::$resizeWidth = self::resizeWidthByHeight($height);
- self::$resizeHeight = $height;
- break;
- default:
- if ((self::$origWidth > $width) || (self::$origHeight > $height)) {
- if (self::$origWidth > self::$origHeight) {
- self::$resizeHeight = self::resizeHeightByWidth($width);
- self::$resizeWidth = $width;
- } elseif (self::$origWidth < self::$origHeight) {
- self::$resizeWidth = self::resizeWidthByHeight($height);
- self::$resizeHeight = $height;
- } else {
- self::$resizeWidth = $width;
- self::$resizeHeight = $height;
- }
- } else {
- self::$resizeWidth = $width;
- self::$resizeHeight = $height;
- }
- break;
- }
- self::$newImage = imagecreatetruecolor(self::$resizeWidth, self::$resizeHeight);
- imagealphablending(self::$newImage, false);
- imagesavealpha(self::$newImage, true);
- $transparent = imagecolorallocatealpha(self::$newImage, 255, 255, 255, 127);
- imagefilledrectangle(self::$newImage, 0, 0, self::$resizeWidth, self::$resizeHeight, $transparent);
- imagecopyresampled(
- self::$newImage,
- self::$image,
- $crop_x,
- $crop_y,
- 0,
- 0,
- (isset(self::$newWidth)) ? self::$newWidth : self::$resizeWidth,
- (isset(self::$newHeight)) ? self::$newHeight : self::$resizeHeight,
- self::$origWidth,
- self::$origHeight
- );
- }
- private static function detectWaH($new, $orig)
- {
- if (!$new) {
- if ($orig > self::MaxWH) {
- return self::MaxWH;
- } else {
- return $orig;
- }
- } else {
- if ($new > self::MaxWH) {
- return self::MaxWH;
- } else {
- return $new;
- }
- }
- }
- private static function resizeHeightByWidth($width)
- {
- return floor((self::$origHeight / self::$origWidth) * $width);
- }
- private static function resizeWidthByHeight($height)
- {
- return floor((self::$origWidth / self::$origHeight) * $height);
- }
- private static function save($savePath, $imageQuality = '100')
- {
- $status = false;
- switch (self::$ext) {
- case 'image/jpg':
- case 'image/jpeg':
- if (imagetypes() & IMG_JPG) {
- imagejpeg(self::$newImage, $savePath, $imageQuality);
- $status = true;
- }
- break;
- case 'image/gif':
- if (imagetypes() & IMG_GIF) {
- imagegif(self::$newImage, $savePath);
- $status = true;
- }
- break;
- case 'image/png':
- $invertScaleQuality = 9 - round(($imageQuality / 100) * 9);
- if (imagetypes() & IMG_PNG) {
- imagepng(self::$newImage, $savePath, $invertScaleQuality);
- $status = true;
- }
- break;
- }
- imagedestroy(self::$newImage);
- return $status;
- }
- }
Add Comment
Please, Sign In to add comment