Powerful and easy to use cms to build websites, blogs or ecommerce stores.
HTML backendblogblog-engineblog-platformbloggingcmscontent-managementcontent-management-systemecommerceecommerce-platformno-codepage-builderphpphp-cmsphp7php8self-hostedshopping-cartweb-applicationwebsite-builder
Fixed image crop and resize to maintain ratio
givanz307a79015a75
diff --git a/system/media/gd.php b/system/media/gd.phpold mode 100755new mode 100644index 47159a7..d981749--- a/system/media/gd.php+++ b/system/media/gd.php@@ -94,16 +94,18 @@ class Image {if (is_object($this->image) || is_resource($this->image)) {if ($extension == 'jpeg' || $extension == 'jpg') {- $result = imagejpeg($this->image, $file, $quality);+ $result = @imagejpeg($this->image, $file, $quality);} elseif ($extension == 'png') {- $result = imagepng($this->image, $file, 8, PNG_ALL_FILTERS);+ $result = @imagepng($this->image, $file, 8, PNG_ALL_FILTERS);} elseif ($extension == 'gif') {- $result = imagegif($this->image, $file);+ $result = @imagegif($this->image, $file);} elseif ($extension == 'webp') {- $result = imagewebp($this->image, $file, $quality);+ $result = @imagewebp($this->image, $file, $quality);}- imagedestroy($this->image);+ if (PHP_MAJOR_VERSION < 8) {+ imagedestroy($this->image);+ }return $result;}@@ -112,13 +114,13 @@ class Image {public function resize($width, $height = 0, $method = 's') {switch ($method) {case 's':- return $this->stretch($width, $height);+ return $this->stretch($width, $height);case 'c':- return $this->crop($width, $height);+ return $this->crop($width, $height);case 'cs':- return $this->cropsize($width, $height);+ return $this->cropsize($width, $height);}}@@ -127,6 +129,16 @@ class Image {return;}+ $ratio = $newRatio = $this->width / $this->height;++ if ($width && $height) {+ $newRatio = $width / $height;+ }++ if (! $height) {+ $height = $width * $newRatio;+ }+if ($width && $height) {$scaleW = $width / $this->width;$scaleH = $height / $this->height;@@ -171,28 +183,40 @@ class Image {return;}- $width = $width ?: $height;- $height = $height ?: $width;+ $ratio = $newRatio = $this->width / $this->height;- $newRatio = $width / $height;- $ratio = $this->width / $this->height;+ if ($width && $height) {+ $newRatio = $width / $height;+ }++ if (! $height) {+ $height = $width / $newRatio;+ }if ($newRatio > $ratio) {$newWidth = $width;$newHeight = floor($width / $this->width * $this->height);$crop_x = 0;$crop_y = intval(($newHeight - $height) / 2);- } else {+ } else if ($newRatio < $ratio) {$newWidth = floor($height / $this->height * $this->width);- $newHeight = $height;+ $newHeight = intval($height);$crop_x = intval(($newWidth - $width) / 2);$crop_y = 0;+ } else {+ $newWidth = intval($width);+ $newHeight = intval($height);}- $image = imagescale($this->image, $newWidth, $newHeight, IMG_BICUBIC_FIXED);+ $image = false;+ if ($this->width != $newWidth && $this->height != $newHeight) {+ $image = imagescale($this->image, $newWidth, $newHeight);+ }if ($image) {- $image = imagecrop($image, ['x' => 0, 'y' => 0, 'width' => $width, 'height' => $height]);+ if ($width != $newWidth || $newHeight != $height) {+ $image = imagecrop($image, ['x' => 0, 'y' => 0, 'width' => $width, 'height' => $height]);+ }if ($image) {$this->image = $image;diff --git a/system/media/imagick.php b/system/media/imagick.phpold mode 100755new mode 100644index 3239883..798d002--- a/system/media/imagick.php+++ b/system/media/imagick.php@@ -41,6 +41,14 @@ class Image {$this->width = $this->image->getImageWidth();$this->height = $this->image->getImageHeight();$this->mime = $this->image->getFormat();+ //if (STRIP_EXIF) {+ $profiles = $this->image->getImageProfiles('icc', true);+ $this->image->stripImage();++ if (! empty($profiles)) {+ $this->image->profileImage('icc', $profiles['icc']);+ }+ //}} else {//throw new \Exception("Could not load image $file");}@@ -88,13 +96,13 @@ class Image {switch ($method) {case 's':- return $this->stretch($width, $height);+ return $this->stretch($width, $height);case 'c':- return $this->crop($width, $height);+ return $this->crop($width, $height);case 'cs':- return $this->cropsize($width, $height);+ return $this->cropsize($width, $height);}}@@ -102,6 +110,16 @@ class Image {//$this->width = $this->image->getImageWidth();//$this->height = $this->image->getImageHeight();+ $ratio = $newRatio = $this->width / $this->height;++ if ($width && $height) {+ $newRatio = $width / $height;+ }++ if (! $height) {+ $height = $width / $newRatio;+ }+if ($width && $height) {$scaleW = $width / $this->width;$scaleH = $height / $this->height;@@ -136,27 +154,40 @@ class Image {}public function cropsize($width, $height = 0) {- $width = $width ?: $height;- $height = $height ?: $width;+ $ratio = $newRatio = $this->width / $this->height;++ if ($width && $height) {+ $newRatio = $width / $height;+ }++ if (! $height) {+ $height = $width / $newRatio;+ }- $newRatio = $width / $height;$ratio = $this->width / $this->height;if ($newRatio > $ratio) {- $new_width = $width;- $new_height = floor($width / $this->width * $this->height);+ $newWidth = $width;+ $newHeight = floor($width / $this->width * $this->height);$crop_x = 0;- $crop_y = intval(($new_height - $height) / 2);- } else {- $new_width = floor($height / $this->height * $this->width);- $new_height = $height;- $crop_x = intval(($new_width - $width) / 2);+ $crop_y = intval(($newHeight - $height) / 2);+ } else if ($newRatio < $ratio) {+ $newWidth = floor($height / $this->height * $this->width);+ $newHeight = $height;+ $crop_x = intval(($newWidth - $width) / 2);$crop_y = 0;+ } else {+ $newWidth = intval($width);+ $newHeight = intval($height);}- $this->image->resizeImage($new_width, $new_height, \Imagick::FILTER_LANCZOS, 1, true);+ if ($this->width != $newWidth && $this->height != $newHeight) {+ $this->image->resizeImage($newWidth, $newHeight, \Imagick::FILTER_LANCZOS, 1, true);+ }- $this->image->cropImage($width, $height, $crop_x, $crop_y);+ if ($width != $newWidth || $newHeight != $height) {+ $this->image->cropImage($width, $height, $crop_x, $crop_y);+ }}public static function formats($format = false) {