Advertisement
Guest User

Untitled

a guest
Mar 18th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.12 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @package SP Page Builder
  5.  * @author JoomShaper http://www.joomshaper.com
  6.  * @copyright Copyright (c) 2010 - 2023 JoomShaper
  7.  * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
  8.  */
  9. //no direct access
  10. defined('_JEXEC') or die('Restricted access');
  11.  
  12. class SppagebuilderHelperImage
  13. {
  14.  
  15.     public $height;
  16.     public $width;
  17.     private $src;
  18.  
  19.     public function __construct($src = '')
  20.     {
  21.         $this->src = $src;
  22.         list($this->width, $this->height) = getimagesize($src);
  23.     }
  24.  
  25.     public function getDimension()
  26.     {
  27.         return [$this->width, $this->height];
  28.     }
  29.  
  30.     /**
  31.      * Create thumb image with specifice height and width and with their ratio
  32.      */
  33.     public function createThumb($size, $destination, $base_name, $ext, $quality = 100)
  34.     {
  35.  
  36.         $img = $this->createImageFromType($ext);
  37.  
  38.         if (count((array) $size) && $img != null)
  39.         {
  40.             $targetWidth = $size[0];
  41.             $targetHeight = $size[1];
  42.  
  43.             $ratio_thumb = $targetWidth / $targetHeight;
  44.             $ratio_original = $this->width / $this->height;
  45.  
  46.             if ($ratio_original >= $ratio_thumb)
  47.             {
  48.                 $height = $this->height;
  49.                 $width = ceil(($height * $targetWidth) / $targetHeight);
  50.                 $x = ceil(($this->width - $width) / 2);
  51.                 $y = 0;
  52.             }
  53.             else
  54.             {
  55.                 $width = $this->width;
  56.                 $height = ceil(($width * $targetHeight) / $targetWidth);
  57.                 $y = ceil(($this->height - $height) / 2);
  58.                 $x = 0;
  59.             }
  60.  
  61.             $targetWidth = (int) $targetWidth;
  62.             $targetHeight = (int) $targetHeight;
  63.  
  64.             $new = imagecreatetruecolor($targetWidth, $targetHeight);
  65.  
  66.             if ($ext == "gif" or $ext == "png")
  67.             {
  68.                 imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
  69.                 imagealphablending($new, false);
  70.                 imagesavealpha($new, true);
  71.             }
  72.  
  73.             imagecopyresampled($new, $img, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height);
  74.  
  75.             $dest = $destination . '/' . $base_name . '.' . $ext;
  76.  
  77.             $this->cloneImage($ext, $new, $dest, $quality);
  78.  
  79.             return true;
  80.         }
  81.  
  82.         return false;
  83.     }
  84.  
  85.     /**
  86.      * Clone Image from original to destination source based on extension
  87.      */
  88.     private function cloneImage($ext, $new, $dest, $quality)
  89.     {
  90.         switch ($ext)
  91.         {
  92.             case 'bmp':
  93.                 imagewbmp($new, $dest);
  94.                 break;
  95.             case 'webp':
  96.                 imagewebp($new, $dest, $quality);
  97.                 break;
  98.             case 'gif':
  99.                 imagegif($new, $dest);
  100.                 break;
  101.             case 'jpg':
  102.                 imagejpeg($new, $dest, $quality);
  103.                 break;
  104.             case 'jpeg':
  105.                 imagejpeg($new, $dest, $quality);
  106.                 break;
  107.             case 'png':
  108.                 imagepng($new, $dest, floor($quality / 11));
  109.                 break;
  110.         }
  111.     }
  112.  
  113.     /**
  114.      * Create Image from their specific extension
  115.      */
  116.     private function createImageFromType($ext)
  117.     {
  118.         switch ($ext)
  119.         {
  120.             case 'bmp':
  121.                 $img = imagecreatefromwbmp($this->src);
  122.                 break;
  123.             case 'webp':
  124.                 $img = imagecreatefromwebp($this->src);
  125.                 break;
  126.             case 'gif':
  127.                 $img = imagecreatefromgif($this->src);
  128.                 break;
  129.             case 'jpg':
  130.                 $img = imagecreatefromjpeg($this->src);
  131.                 break;
  132.             case 'jpeg':
  133.                 $img = imagecreatefromjpeg($this->src);
  134.                 break;
  135.             case 'png':
  136.                 $img = imagecreatefrompng($this->src);
  137.                 break;
  138.         }
  139.         return $img;
  140.     }
  141. }
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement