From 3cd6a7b61e83647c44bd0c36b4acc9cb2562a5fa Mon Sep 17 00:00:00 2001 From: Sean Madsen Date: Fri, 21 Jul 2017 10:15:58 -0600 Subject: [PATCH] CRM-20821 - Move resizeImage() to Utils class - Move resizeImage() from CRM_Contribute_Form_ManagePremiums to CRM_Utils_File - Make it public static - Don't change anything about how it works --- CRM/Contribute/Form/ManagePremiums.php | 55 +------------------------- CRM/Utils/File.php | 49 +++++++++++++++++++++++ 2 files changed, 51 insertions(+), 53 deletions(-) diff --git a/CRM/Contribute/Form/ManagePremiums.php b/CRM/Contribute/Form/ManagePremiums.php index 2cd9d1a781..47a7a7608c 100644 --- a/CRM/Contribute/Form/ManagePremiums.php +++ b/CRM/Contribute/Form/ManagePremiums.php @@ -296,8 +296,8 @@ class CRM_Contribute_Form_ManagePremiums extends CRM_Contribute_Form { if ($gdSupport) { if ($imageFile) { $error = FALSE; - $params['image'] = $this->_resizeImage($imageFile, "_full", 200, 200); - $params['thumbnail'] = $this->_resizeImage($imageFile, "_thumb", 50, 50); + $params['image'] = CRM_Utils_File::resizeImage($imageFile, "_full", 200, 200); + $params['thumbnail'] = CRM_Utils_File::resizeImage($imageFile, "_thumb", 50, 50); } } else { @@ -344,55 +344,4 @@ class CRM_Contribute_Form_ManagePremiums extends CRM_Contribute_Form { } } - /** - * Resize a premium image to a different size. - * - * - * @param string $filename - * @param string $resizedName - * @param $width - * @param $height - * - * @return string - * Path to image - */ - private function _resizeImage($filename, $resizedName, $width, $height) { - // figure out the new filename - $pathParts = pathinfo($filename); - $newFilename = $pathParts['dirname'] . "/" . $pathParts['filename'] . $resizedName . "." . $pathParts['extension']; - - // get image about original image - $imageInfo = getimagesize($filename); - $widthOrig = $imageInfo[0]; - $heightOrig = $imageInfo[1]; - $image = imagecreatetruecolor($width, $height); - if ($imageInfo['mime'] == 'image/gif') { - $source = imagecreatefromgif($filename); - } - elseif ($imageInfo['mime'] == 'image/png') { - $source = imagecreatefrompng($filename); - } - else { - $source = imagecreatefromjpeg($filename); - } - - // resize - imagecopyresized($image, $source, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig); - - // save the resized image - $fp = fopen($newFilename, 'w+'); - ob_start(); - imagejpeg($image); - $image_buffer = ob_get_contents(); - ob_end_clean(); - imagedestroy($image); - fwrite($fp, $image_buffer); - rewind($fp); - fclose($fp); - - // return the URL to link to - $config = CRM_Core_Config::singleton(); - return $config->imageUploadURL . basename($newFilename); - } - } diff --git a/CRM/Utils/File.php b/CRM/Utils/File.php index f377b742cd..9c94497370 100644 --- a/CRM/Utils/File.php +++ b/CRM/Utils/File.php @@ -916,6 +916,55 @@ HTACCESS; return self::getFileURL($path, $mimeType); } + /** + * Resize a premium image to a different size. + * + * @param string $filename + * @param string $resizedName + * @param $width + * @param $height + * + * @return string + * Path to image + */ + public static function resizeImage($filename, $resizedName, $width, $height) { + // figure out the new filename + $pathParts = pathinfo($filename); + $newFilename = $pathParts['dirname'] . "/" . $pathParts['filename'] . $resizedName . "." . $pathParts['extension']; + + // get image about original image + $imageInfo = getimagesize($filename); + $widthOrig = $imageInfo[0]; + $heightOrig = $imageInfo[1]; + $image = imagecreatetruecolor($width, $height); + if ($imageInfo['mime'] == 'image/gif') { + $source = imagecreatefromgif($filename); + } + elseif ($imageInfo['mime'] == 'image/png') { + $source = imagecreatefrompng($filename); + } + else { + $source = imagecreatefromjpeg($filename); + } + + // resize + imagecopyresized($image, $source, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig); + + // save the resized image + $fp = fopen($newFilename, 'w+'); + ob_start(); + imagejpeg($image); + $image_buffer = ob_get_contents(); + ob_end_clean(); + imagedestroy($image); + fwrite($fp, $image_buffer); + rewind($fp); + fclose($fp); + + // return the URL to link to + $config = CRM_Core_Config::singleton(); + return $config->imageUploadURL . basename($newFilename); + } /** * Get file icon class for specific MIME Type -- 2.25.1