PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/3.1/modules/themeroller/helpers/themeroller.php

http://github.com/gallery/gallery3-contrib
PHP | 204 lines | 88 code | 6 blank | 110 comment | 21 complexity | d543f0341ebd84d702e2e2f35f78ece0 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1
  1. <?php defined("SYSPATH") or die("No direct script access.");/**
  2. * Gallery - a web based photo album viewer and editor
  3. * Copyright (C) 2000-2011 Bharat Mediratta
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. class themeroller {
  20. static function extract_zip_file($zipfile) {
  21. $extract_path = VARPATH . trim($zipfile, "/") . ".d";
  22. if (extension_loaded("zip")) {
  23. $zip = new ZipArchive();
  24. if ($zip->open($zipfile) === true) {
  25. Session::instance()->set("theme_extract_path", $extract_path);
  26. $zip->extractTo($extract_path);
  27. $zip->close();
  28. return $extract_path;
  29. }
  30. } else if (extension_loaded("zlib")) {
  31. require_once(MODPATH . "themeroller/libraries/pclzip.lib.php");
  32. $archive = new PclZip($zipfile);
  33. $list = $archive->extract(PCLZIP_OPT_PATH, $extract_path);
  34. if (!empty($list)) {
  35. Session::instance()->set("theme_extract_path", $extract_path);
  36. return $extract_path;
  37. }
  38. }
  39. return false;
  40. }
  41. static function recursive_directory_delete($path) {
  42. if (is_dir($path)) {
  43. $objects = scandir($path);
  44. foreach ($objects as $object) {
  45. if ($object[0] != ".") {
  46. $object_path = "$path/$object";
  47. if (filetype($object_path) == "dir") {
  48. self::recursive_directory_delete($object_path);
  49. } else {
  50. unlink($object_path);
  51. }
  52. }
  53. }
  54. }
  55. }
  56. static function get_theme_name($extract_path) {
  57. $theme_name = null;
  58. if ($handle = opendir($extract_path . "/css")) {
  59. while (false !== ($file = readdir($handle))) {
  60. if ($file[0] !== ".") {
  61. $theme_name = basename($file);
  62. break;
  63. }
  64. }
  65. if (empty($theme_name)) {
  66. Kohana_Log::add("error", "zip file: no theme name");
  67. $post->add_error($field, "invalid zipfile");
  68. }
  69. closedir($handle);
  70. }
  71. return $theme_name;
  72. }
  73. static function get_theme_parameters($original_name, $css_path, $is_admin) {
  74. $parameters = array();
  75. $css_files = glob("$css_path/css/$original_name/jquery*.css");
  76. $css_contents = file_get_contents($css_files[0]);
  77. $parameters["colors"] = $parameters["icons"] = array();
  78. if (preg_match("/[?|&](.*)/", $css_contents, $matches)) {
  79. if (preg_match_all("/&{0,1}(\w+)=([a-zA-Z0-9\-_\%\.,]*)/", $matches[1], $colors, PREG_SET_ORDER)) {
  80. foreach ($colors as $color) {
  81. $parameters["colors"][$color[1]] = $color[2];
  82. if (strpos($color[1], "icon") === 0) {
  83. $parameters["icons"][] = $color[2];
  84. }
  85. }
  86. }
  87. }
  88. if (empty($parameters["colors"]["bgColorOverlay"])) {
  89. $parameters["colors"]["bgColorOverlay"] = $parameters["colors"]["bgColorDefault"];
  90. /* @todo go find the .ui-widget-overlay { background: #aaaaaa */
  91. }
  92. // The jquery themeroller has no warning style so lets generate the appropriate colors.
  93. // We'll do this by averaging the color components of highlight and error colors
  94. foreach (array("borderColor", "fc", "bgColor", "iconColor") as $type) {
  95. $highlight = self::_rgb(hexdec($parameters["colors"]["{$type}Highlight"]));
  96. $error = self::_rgb(hexdec($parameters["colors"]["{$type}Error"]));
  97. $warning = 0;
  98. foreach (array("red", "green", "blue") as $color) {
  99. $warning = ($warning << 8) | (int)floor(($highlight[$color] + $error[$color]) / 2);
  100. }
  101. $parameters["colors"]["{$type}Warning"] = dechex($warning);
  102. if ($type == "iconColor") {
  103. $parameters["icons"][] = $parameters["colors"]["{$type}Warning"];
  104. }
  105. }
  106. $parameters["js"] = $is_admin ? glob(MODPATH . "themeroller/data/js/admin_*.js") :
  107. glob(MODPATH . "themeroller/data/js/site_*.js");
  108. $parameters["standard_css"] = glob(MODPATH . "themeroller/data/css/*.css");
  109. $parameters["masks"] = glob(MODPATH . "themeroller/data/masks/images/*.png");
  110. $parameters["icon_mask"] = MODPATH . "themeroller/data/masks/css/themeroller/ui-icons_mask_256x240.png";
  111. $parameters["views"] = $is_admin ? glob(MODPATH . "themeroller/data/admin_views/*.html.php") :
  112. glob(MODPATH . "themeroller/data/views/*.html.php");
  113. $parameters["css_files"] = $css_files;
  114. $parameters["gifs"] = glob(MODPATH . "themeroller/data/images/*.gif");
  115. $parameters["images"] =
  116. glob("$css_path/development-bundle/themes/$original_name/images/ui-bg*.png");
  117. $thumb_dir = $is_admin ? "admin_thumbnail" : "site_thumbnail";
  118. $parameters["thumbnail"] = MODPATH . "themeroller/data/masks/$thumb_dir/thumbnail.png";
  119. $parts = glob(MODPATH . "themeroller/data/masks/$thumb_dir/thumbnail_*.png");
  120. $parameters["thumbnail_parts"] = array();
  121. foreach ($parts as $thumb_file) {
  122. if (preg_match("/thumbnail_(.*)\.png$/", $thumb_file, $matches)) {
  123. $parameters["thumbnail_parts"][] = array("file" => $thumb_file,
  124. "color" => $parameters["colors"][$matches[1]]);
  125. }
  126. }
  127. return $parameters;
  128. }
  129. static function generate_image($mask_file, $output, $color) {
  130. $mask = imagecreatefrompng($mask_file);
  131. $image = imagecreatetruecolor(imagesx($mask), imagesy($mask));
  132. $icon_color = self::_rgb(hexdec($color));
  133. $transparent = imagecolorallocatealpha($image,
  134. $icon_color['red'], $icon_color['green'], $icon_color['blue'], 127);
  135. imagefill($image, 0, 0, $transparent);
  136. for ($y=0; $y < imagesy($mask); $y++) {
  137. for ($x=0; $x < imagesx($mask); $x++) {
  138. $pixel_color = imagecolorsforindex($mask, imagecolorat($mask, $x, $y));
  139. $mask_color = self::_grayscale_pixel($pixel_color);
  140. $mask_alpha = 127 - floor($mask_color["red"] * 127 / 256);
  141. $new_color = imagecolorallocatealpha($image,
  142. $icon_color['red'], $icon_color['green'], $icon_color['blue'], $mask_alpha);
  143. imagesetpixel($image, $x, $y, $new_color);
  144. }
  145. }
  146. imagesavealpha($image, true);
  147. imagealphablending($image, false);
  148. imagepng($image, $output);
  149. imagedestroy($image);
  150. imagedestroy($mask);
  151. }
  152. static function generate_thumbnail($base, $parts, $target) {
  153. $image = imagecreatefrompng($base);
  154. $transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
  155. imagefill($image, 0, 0, $transparent);
  156. $width = imagesx($image);
  157. $height = imagesy($image);
  158. foreach ($parts as $thumb_part) {
  159. $color = self::_rgb(hexdec($thumb_part["color"]));
  160. $image_part = imagecreatefrompng($thumb_part["file"]);
  161. for ($y=0; $y < imagesy($image_part); $y++) {
  162. for ($x=0; $x < imagesx($image_part); $x++) {
  163. $pixel_color = imagecolorsforindex($image_part, imagecolorat($image_part, $x, $y));
  164. $new_color = imagecolorallocatealpha($image,
  165. $color['red'], $color['green'], $color['blue'], $pixel_color["alpha"]);
  166. imagesetpixel($image, $x, $y, $new_color);
  167. }
  168. }
  169. imagedestroy($image_part);
  170. }
  171. imagesavealpha($image, true);
  172. imagealphablending($image, false);
  173. imagepng($image, $target);
  174. imagedestroy($image);
  175. }
  176. private static function _rgb($color) {
  177. $r = ($color >> 16) & 0xff;
  178. $g = ($color >> 8) & 0xff;
  179. $b = $color & 0xff;
  180. return array("red" => $r, "green" => $g, "blue" => $b, "alpha" => 0);
  181. }
  182. private static function _grayscale_pixel($color) {
  183. $gray = round(($color['red'] * 0.299) + ($color['green'] * 0.587) + ($color['blue'] * 0.114));
  184. return array("red" => $gray, "green" => $gray, "blue" => $gray, "alpha" => 0);
  185. }
  186. }