Changeset View
Changeset View
Standalone View
Standalone View
branches/5.2.x/core/units/helpers/image_helper.php
Show First 20 Lines • Show All 106 Lines • ▼ Show 20 Line(s) | |||||
if ((!$src_image || !file_exists($src_image)) && array_key_exists('default', $params) && !(defined('DBG_IMAGE_RECOVERY') && DBG_IMAGE_RECOVERY)) { | if ((!$src_image || !file_exists($src_image)) && array_key_exists('default', $params) && !(defined('DBG_IMAGE_RECOVERY') && DBG_IMAGE_RECOVERY)) { | ||||
$src_image = $params['default']; | $src_image = $params['default']; | ||||
} | } | ||||
if ( !strlen($src_image) || !file_exists($src_image) ) { | if ( !strlen($src_image) || !file_exists($src_image) ) { | ||||
throw new RuntimeException(sprintf('Image "%s" doesn\'t exist', $src_image)); | throw new RuntimeException(sprintf('Image "%s" doesn\'t exist', $src_image)); | ||||
} | } | ||||
if ($params['max_width'] > 0 || $params['max_height'] > 0) { | if ( !$this->isSVG($src_image) && ($params['max_width'] > 0 || $params['max_height'] > 0) ) { | ||||
list ($params['target_width'], $params['target_height'], $needs_resize) = $this->GetImageDimensions($src_image, $params['max_width'], $params['max_height'], $params); | list ($params['target_width'], $params['target_height'], $needs_resize) = $this->GetImageDimensions($src_image, $params['max_width'], $params['max_height'], $params); | ||||
if (!is_numeric($params['max_width'])) { | if (!is_numeric($params['max_width'])) { | ||||
$params['max_width'] = $params['target_width']; | $params['max_width'] = $params['target_width']; | ||||
} | } | ||||
if (!is_numeric($params['max_height'])) { | if (!is_numeric($params['max_height'])) { | ||||
$params['max_height'] = $params['target_height']; | $params['max_height'] = $params['target_height']; | ||||
Show All 29 Lines | |||||
} | } | ||||
} | } | ||||
// resize/watermarking ok | // resize/watermarking ok | ||||
$src_image = $dst_image; | $src_image = $dst_image; | ||||
} | } | ||||
} | } | ||||
if ($image_size) { | if ( $image_size ) { | ||||
// return only image size (resized or not) | if ( $this->isSVG($src_image) ) { | ||||
return 'width="' . $params['max_width'] . '" height="' . $params['max_height'] . '"'; | |||||
} | |||||
// Return only image size (resized or not). | |||||
$image_info = $this->getImageInfo($src_image); | $image_info = $this->getImageInfo($src_image); | ||||
return $image_info ? $image_info[3] : ''; | return $image_info ? $image_info[3] : ''; | ||||
} | } | ||||
return $this->fileHelper->pathToUrl($src_image); | return $this->fileHelper->pathToUrl($src_image); | ||||
} | } | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 267 Lines • ▼ Show 20 Line(s) | |||||
* @param string $src_image full path to source image (already existing) | * @param string $src_image full path to source image (already existing) | ||||
* @param int $dst_width destination image width (in pixels) | * @param int $dst_width destination image width (in pixels) | ||||
* @param int $dst_height destination image height (in pixels) | * @param int $dst_height destination image height (in pixels) | ||||
* @param Array $params | * @param Array $params | ||||
* @return Array resized image dimensions (0 - width, 1 - height) | * @return Array resized image dimensions (0 - width, 1 - height) | ||||
*/ | */ | ||||
function GetImageDimensions($src_image, $dst_width, $dst_height, $params) | function GetImageDimensions($src_image, $dst_width, $dst_height, $params) | ||||
{ | { | ||||
// The SVG file is in vector format and can scale to any size. | |||||
if ( $this->isSVG($src_image) ) { | |||||
return array($dst_width, $dst_height, false); | |||||
} | |||||
$image_info = $this->getImageInfo($src_image); | $image_info = $this->getImageInfo($src_image); | ||||
if (!$image_info) { | if (!$image_info) { | ||||
return false; | return false; | ||||
} | } | ||||
$orig_width = $image_info[0]; | $orig_width = $image_info[0]; | ||||
$orig_height = $image_info[1]; | $orig_height = $image_info[1]; | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Line(s) | |||||
/** | /** | ||||
* Returns image dimensions + checks if given file is existing image | * Returns image dimensions + checks if given file is existing image | ||||
* | * | ||||
* @param string $src_image full path to source image (already existing) | * @param string $src_image full path to source image (already existing) | ||||
* @return mixed | * @return mixed | ||||
*/ | */ | ||||
function getImageInfo($src_image) | function getImageInfo($src_image) | ||||
{ | { | ||||
if (!file_exists($src_image)) { | if ( !file_exists($src_image) || $this->isSVG($src_image) ) { | ||||
return false; | return false; | ||||
} | } | ||||
$image_info = @getimagesize($src_image); | $image_info = @getimagesize($src_image); | ||||
if (!$image_info) { | if (!$image_info) { | ||||
trigger_error('Image <b>'.$src_image.'</b> <span class="debug_error">missing or invalid</span>', E_USER_WARNING); | trigger_error('Image <b>'.$src_image.'</b> <span class="debug_error">missing or invalid</span>', E_USER_WARNING); | ||||
return false; | return false; | ||||
} | } | ||||
return $image_info; | return $image_info; | ||||
} | } | ||||
/** | /** | ||||
* Checks if image is an SVG file. | |||||
* | |||||
* @param string $src_image Full path to source image (already existing). | |||||
* | |||||
* @return boolean | |||||
*/ | |||||
protected function isSVG($src_image) | |||||
{ | |||||
return pathinfo($src_image, PATHINFO_EXTENSION) == 'svg'; | |||||
} | |||||
/** | |||||
* Returns maximal image size (width & height) among fields specified | * Returns maximal image size (width & height) among fields specified | ||||
* | * | ||||
* @param kDBItem $object | * @param kDBItem $object | ||||
* @param string $fields | * @param string $fields | ||||
* @param string $format any format, that returns full url (e.g. files_resized:WxH, resize:WxH, full_url, full_urls) | * @param string $format any format, that returns full url (e.g. files_resized:WxH, resize:WxH, full_url, full_urls) | ||||
* @return string | * @return string | ||||
*/ | */ | ||||
function MaxImageSize(&$object, $fields, $format = null) | function MaxImageSize(&$object, $fields, $format = null) | ||||
▲ Show 20 Lines • Show All 194 Lines • Show Last 20 Lines |