| Current Path : /home/digilove/public_html/41423/ |
| Current File : //home/digilove/public_html/41423/Image.php.tar |
home/digilove/public_html/libraries/src/Image/Image.php 0000644 00000004014 15234615375 0017132 0 ustar 00 <?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Image;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Log\Log;
/**
* Class to manipulate an image.
*
* @since 1.7.3
*/
class Image extends \Joomla\Image\Image
{
/**
* Class constructor.
*
* @param mixed $source Either a file path for a source image or a GD resource handler for an image.
*
* @since 1.7.3
* @throws \RuntimeException
*/
public function __construct($source = null)
{
// Inject the PSR-3 compatible logger in for forward compatibility
$this->setLogger(Log::createDelegatedLogger());
parent::__construct($source);
}
/**
* Method to get an image filter instance of a specified type.
*
* @param string $type The image filter type to get.
*
* @return ImageFilter
*
* @since 1.7.3
* @throws \RuntimeException
*/
protected function getFilterInstance($type)
{
try
{
return parent::getFilterInstance($type);
}
catch (\RuntimeException $e)
{
// Ignore, filter is probably not namespaced
}
// Sanitize the filter type.
$type = strtolower(preg_replace('#[^A-Z0-9_]#i', '', $type));
// Verify that the filter type exists.
$className = 'JImageFilter' . ucfirst($type);
if (!class_exists($className))
{
Log::add('The ' . ucfirst($type) . ' image filter is not available.', Log::ERROR);
throw new \RuntimeException('The ' . ucfirst($type) . ' image filter is not available.');
}
// Instantiate the filter object.
$instance = new $className($this->getHandle());
// Verify that the filter type is valid.
if (!($instance instanceof ImageFilter))
{
// @codeCoverageIgnoreStart
Log::add('The ' . ucfirst($type) . ' image filter is not valid.', Log::ERROR);
throw new \RuntimeException('The ' . ucfirst($type) . ' image filter is not valid.');
// @codeCoverageIgnoreEnd
}
return $instance;
}
}
home/digilove/public_html/libraries/regularlabs/src/Image.php 0000644 00000026325 15234626047 0020422 0 ustar 00 <?php
/**
* @package Regular Labs Library
* @version 23.7.24631
*
* @author Peter van Westen <info@regularlabs.com>
* @link https://regularlabs.com
* @copyright Copyright © 2023 Regular Labs All Rights Reserved
* @license GNU General Public License version 2 or later
*/
namespace RegularLabs\Library;
defined('_JEXEC') or die;
use Exception;
use Joomla\CMS\Filesystem\Folder as JFolder;
use Joomla\CMS\Uri\Uri as JUri;
use Joomla\Image\Image as JImage;
class Image
{
public static function cleanPath($source)
{
$source = ltrim(str_replace(JUri::root(), '', $source), '/');
$source = strtok($source, '?');
return $source;
}
public static function getDimensions($source)
{
$empty = (object) [
'width' => 0,
'height' => 0,
];
$image = self::getImageObject(JPATH_SITE . '/' . $source);
if (is_null($image))
{
return $empty;
}
return (object) [
'width' => $image->getWidth(),
'height' => $image->getHeight(),
];
}
public static function getHeight($source)
{
$dimensions = self::getDimensions($source);
return $dimensions->height;
}
public static function getImageObject($source)
{
if (File::isExternal($source))
{
return null;
}
if ( ! file_exists($source))
{
return null;
}
if ( ! getimagesize($source))
{
return null;
}
try
{
$image = new JImage($source);
}
catch (Exception $e)
{
return null;
}
if ( ! ($image instanceof JImage))
{
return null;
}
if ( ! $image->isLoaded())
{
return null;
}
return $image;
}
public static function getJpgQuality($quality = 'medium')
{
switch ($quality)
{
case 'low':
return 50;
case 'high':
return 90;
case 'medium':
default:
return 70;
}
}
public static function getNewPath($source, $width, $height, $destination_folder = '')
{
$clean_source = self::cleanPath($source);
$source_parts = pathinfo($clean_source);
$destination_folder = ltrim($destination_folder ?: File::getDirName($clean_source));
$destination_file = File::getFileName($clean_source) . '_' . $width . 'x' . $height . '.' . $source_parts['extension'];
JFolder::create(JPATH_SITE . '/' . $destination_folder);
return ltrim($destination_folder . '/' . $destination_file);
}
public static function getQuality($type, $quality = 'medium')
{
switch ($type)
{
case IMAGETYPE_JPEG:
return min(max(self::getJpgQuality($quality), 0), 100);
case IMAGETYPE_PNG:
return 9;
default:
return '';
}
}
public static function getResize($source, $width, $height, $folder = 'resized', $resize = true, $quality = 'medium')
{
$destination_folder = File::getDirName($source) . '/' . $folder;
$override = File::getDirName($source) . '/' . $folder . '/' . File::getBaseName($source);
if (file_exists(JPATH_SITE . '/' . $override))
{
$source = $override;
}
if ( ! self::setNewDimensions($source, $width, $height))
{
return $source;
}
if ( ! $width && ! $height)
{
return $source;
}
$destination = self::getNewPath(
$source,
$width,
$height,
$destination_folder
);
if ( ! file_exists(JPATH_SITE . '/' . $destination) && $resize)
{
// Create new resized image
$destination = self::resize(
$source,
$width,
$height,
$destination_folder,
$quality
);
}
if ( ! file_exists(JPATH_SITE . '/' . $destination))
{
return $source;
}
return $destination;
}
public static function getUrls($source, $width, $height, $folder = 'resized', $resize = true, $quality = 'medium', $possible_suffix = '')
{
$image = self::isResized($source, $folder, $possible_suffix);
if ($image)
{
$source = $image;
}
$original = $source;
$resized = self::getResize($source, $width, $height, $folder, $resize, $quality);
return (object) compact('original', 'resized');
}
public static function getWidth($source)
{
$dimensions = self::getDimensions($source);
return $dimensions->width;
}
public static function isResized($file, $folder = 'resized', $possible_suffix = '')
{
if (File::isExternal($file))
{
return false;
}
if ( ! file_exists($file))
{
return false;
}
$main_image = self::isResizedWithFolder($file, $folder);
if ($main_image)
{
return $main_image;
}
if ( ! $possible_suffix)
{
return false;
}
return (bool) self::isResizedWithSuffix($file, $possible_suffix);
}
public static function isResizedWithSuffix($file, $suffix = '_t')
{
// Remove the suffix from the file
// image_t.jpg => image.jpg
$main_file = RegEx::replace(
RegEx::quote($suffix) . '(\.[^.]+)$',
'\1',
$file
);
// Nothing removed, so not a resized image
if ($main_file == $file)
{
return false;
}
if ( ! file_exists(JPATH_SITE . '/' . utf8_decode($main_file)))
{
return false;
}
return $main_file;
}
public static function resize($source, &$width, &$height, $destination_folder = '', $quality = 'medium', $overwrite = false)
{
if (File::isExternal($source))
{
return $source;
}
$clean_source = self::cleanPath($source);
$source_path = JPATH_SITE . '/' . $clean_source;
$destination_folder = ltrim($destination_folder ?: File::getDirName($clean_source));
$destination_folder = self::cleanPath($destination_folder);
$image = self::getImageObject($source_path);
if (is_null($image))
{
return $source;
}
self::fixRotation($image);
if ( ! self::setNewDimensionsByImageObject($image, $width, $height))
{
return $source;
}
if ( ! $width && ! $height)
{
return $source;
}
$destination = self::getNewPath($source, $width, $height, $destination_folder);
$destination_path = JPATH_SITE . '/' . $destination;
if (file_exists($destination_path) && ! $overwrite)
{
return $destination;
}
JFolder::create(JPATH_SITE . '/' . $destination_folder);
try
{
$info = JImage::getImageFileProperties($source_path);
$options = ['quality' => self::getQuality($info->type, $quality)];
$image->cropResize($width, $height, false)
->toFile($destination_path, $info->type, $options);
return $destination;
}
catch (Exception $e)
{
return $source;
}
}
private static function fixRotation(&$image)
{
if ( ! function_exists('exif_read_data'))
{
return;
}
$exif = exif_read_data($image->getPath());
if (empty($exif['Orientation']))
{
return;
}
switch ($exif['Orientation'])
{
case 2:
$image->flip(IMG_FLIP_HORIZONTAL, false);
break;
case 3:
$image->rotate(180, -1, false);
break;
case 4:
$image->rotate(180, -1, false)->flip(IMG_FLIP_HORIZONTAL, false);
break;
case 5:
$image->rotate(270, -1, false)->flip(IMG_FLIP_HORIZONTAL, false);
break;
case 6:
$image->rotate(270, -1, false);
break;
case 7:
$image->rotate(90, -1, false)->flip(IMG_FLIP_HORIZONTAL, false);
break;
case 8:
$image->rotate(90, -1, false);
break;
default:
break;
}
}
public static function setNewDimensions($source, &$width, &$height)
{
if ( ! $width && ! $height)
{
return false;
}
if (File::isExternal($source))
{
return false;
}
$clean_source = self::cleanPath($source);
$source_path = JPATH_SITE . '/' . $clean_source;
$image = self::getImageObject($source_path);
if (is_null($image))
{
return false;
}
return self::setNewDimensionsByImageObject($image, $width, $height);
}
public static function setNewDimensionsByImageObject($image, &$width, &$height)
{
if ( ! ($image instanceof JImage) || ! $image->isLoaded())
{
return false;
}
if ( ! $width && ! $height)
{
return false;
}
try
{
$original_width = $image->getWidth();
$original_height = $image->getHeight();
$width = $width ?: round($original_width / $original_height * $height);
$height = $height ?: round($original_height / $original_width * $width);
}
catch (Exception $e)
{
return false;
}
if ($width == $original_width && $height == $original_height)
{
return false;
}
return true;
}
private static function isResizedWithFolder($file, $resize_folder = 'resized')
{
$folder = File::getDirName($file);
$file = File::getBaseName($file);
$parent_folder_name = File::getBaseName($folder);
$parent_folder = File::getDirName($folder);
// Image is not inside the resize folder
if ($parent_folder_name != $resize_folder)
{
return false;
}
// Check if image with same name exists in parent folder
if (file_exists(JPATH_SITE . '/' . $parent_folder . '/' . utf8_decode($file)))
{
return $parent_folder . '/' . $file;
}
// Remove any dimensions from the file
// image_300x200.jpg => image.jpg
$file = RegEx::replace(
'_[0-9]+x[0-9]*(\.[^.]+)$',
'\1',
$file
);
// Check again if image with same name (but without dimensions) exists in parent folder
if (file_exists(JPATH_SITE . '/' . $parent_folder . '/' . utf8_decode($file)))
{
return $parent_folder . '/' . $file;
}
return false;
}
}