From a99a839998db8d54a3b8f1db4621eb27b59c9d5c Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Mon, 4 Aug 2014 15:33:19 +0000 Subject: [PATCH] Add support for ".png" buttons Fixes #48 --- .../Mibew/Controller/ButtonCodeController.php | 8 +++-- .../Mibew/Controller/ButtonController.php | 10 +++++-- src/mibew/libs/common/misc.php | 29 ++++++++++++------- src/mibew/libs/getcode.php | 4 +-- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php b/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php index b418dbff..9ea14060 100644 --- a/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php +++ b/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php @@ -101,8 +101,12 @@ class ButtonCodeController extends AbstractController : $image_locales[0]; } - $file = MIBEW_FS_ROOT . '/locales/${lang}/button/${image}_on.gif'; - $size = get_gifimage_size($file); + $file = MIBEW_FS_ROOT . '/locales/${lang}/button/${image}_on.png'; + if (!is_readable($file)) { + // Fallback to .gif image + $file = MIBEW_FS_ROOT . '/locales/${lang}/button/${image}_on.gif'; + } + $size = get_image_size($file); $image_link_args = array( 'i' => $image, diff --git a/src/mibew/libs/classes/Mibew/Controller/ButtonController.php b/src/mibew/libs/classes/Mibew/Controller/ButtonController.php index 181406a5..7999e8d4 100644 --- a/src/mibew/libs/classes/Mibew/Controller/ButtonController.php +++ b/src/mibew/libs/classes/Mibew/Controller/ButtonController.php @@ -80,7 +80,13 @@ class ButtonController extends AbstractController // Get image file content $image_postfix = has_online_operators($group_id) ? "on" : "off"; - $file_name = "locales/${lang}/button/${image}_${image_postfix}.gif"; + $file_name = "locales/${lang}/button/${image}_${image_postfix}.png"; + $content_type = 'image/png'; + if (!is_readable($file_name)) { + // Fall back to .gif image + $file_name = "locales/${lang}/button/${image}_${image_postfix}.gif"; + $content_type = 'image/gif'; + } $fh = fopen($file_name, 'rb'); if ($fh) { @@ -91,7 +97,7 @@ class ButtonController extends AbstractController $response = new Response($content, 200); // Set correct content info - $response->headers->set('Content-Type', 'image/gif'); + $response->headers->set('Content-Type', $content_type); $response->headers->set('Content-Length', $file_size); } else { $response = new Response('Not found', 404); diff --git a/src/mibew/libs/common/misc.php b/src/mibew/libs/common/misc.php index 89747c91..6a4b722c 100644 --- a/src/mibew/libs/common/misc.php +++ b/src/mibew/libs/common/misc.php @@ -15,19 +15,26 @@ * limitations under the License. */ -function get_gifimage_size($filename) +function get_image_size($filename) { - if (function_exists('gd_info')) { - $info = gd_info(); - if (isset($info['GIF Read Support']) && $info['GIF Read Support']) { - $img = @imagecreatefromgif($filename); - if ($img) { - $height = imagesy($img); - $width = imagesx($img); - imagedestroy($img); + $ext = pathinfo($filename, PATHINFO_EXTENSION); - return array($width, $height); - } + if (function_exists('gd_info') && ($ext == 'gif' || $ext == 'png')) { + $info = gd_info(); + + $img = false; + if ($ext == 'gif' && !empty($info['GIF Read Support'])) { + $img = @imagecreatefromgif($filename); + } elseif ($ext == 'png' && !empty($info['PNG Support'])) { + $img = @imagecreatefrompng($filename); + } + + if ($img) { + $height = imagesy($img); + $width = imagesx($img); + imagedestroy($img); + + return array($width, $height); } } diff --git a/src/mibew/libs/getcode.php b/src/mibew/libs/getcode.php index f5d75cc8..0189b9dd 100644 --- a/src/mibew/libs/getcode.php +++ b/src/mibew/libs/getcode.php @@ -190,8 +190,8 @@ function get_image_locales_map($locales_dir) $images_dir = "$locales_dir/$curr/button"; if ($handle = @opendir($images_dir)) { while (false !== ($file = readdir($handle))) { - $both_files_exist = preg_match("/^(\w+)_on.gif$/", $file, $matches) - && is_file("$images_dir/" . $matches[1] . "_off.gif"); + $both_files_exist = preg_match("/^(\w+)_on\.(gif|png)$/", $file, $matches) + && is_file("$images_dir/" . $matches[1] . "_off." . $matches[2]); if ($both_files_exist) { $image = $matches[1]; if (!isset($image_locales[$image])) {