PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/gallery/helpers/photo.php

https://github.com/ShakeNBake/gallery3
PHP | 210 lines | 130 code | 23 blank | 57 comment | 19 complexity | 10699f46c8f43153434ae17211ac504c MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php defined("SYSPATH") or die("No direct script access.");
  2. /**
  3. * Gallery - a web based photo album viewer and editor
  4. * Copyright (C) 2000-2009 Bharat Mediratta
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /**
  21. * This is the API for handling photos.
  22. *
  23. * Note: by design, this class does not do any permission checking.
  24. */
  25. class photo_Core {
  26. /**
  27. * Create a new photo.
  28. * @param integer $parent_id id of parent album
  29. * @param string $filename path to the photo file on disk
  30. * @param string $name the filename to use for this photo in the album
  31. * @param integer $title the title of the new photo
  32. * @param string $description (optional) the longer description of this photo
  33. * @param string $slug (optional) the url component for this photo
  34. * @return Item_Model
  35. */
  36. static function create($parent, $filename, $name, $title,
  37. $description=null, $owner_id=null, $slug=null) {
  38. if (!$parent->loaded || !$parent->is_album()) {
  39. throw new Exception("@todo INVALID_PARENT");
  40. }
  41. if (!is_file($filename)) {
  42. throw new Exception("@todo MISSING_IMAGE_FILE");
  43. }
  44. if (strpos($name, "/")) {
  45. throw new Exception("@todo NAME_CANNOT_CONTAIN_SLASH");
  46. }
  47. // We don't allow trailing periods as a security measure
  48. // ref: http://dev.kohanaphp.com/issues/684
  49. if (rtrim($name, ".") != $name) {
  50. throw new Exception("@todo NAME_CANNOT_END_IN_PERIOD");
  51. }
  52. if (filesize($filename) == 0) {
  53. throw new Exception("@todo EMPTY_INPUT_FILE");
  54. }
  55. $image_info = getimagesize($filename);
  56. // Force an extension onto the name
  57. $pi = pathinfo($filename);
  58. if (empty($pi["extension"])) {
  59. $pi["extension"] = image_type_to_extension($image_info[2], false);
  60. $name .= "." . $pi["extension"];
  61. }
  62. if (empty($slug)) {
  63. $slug = item::convert_filename_to_slug($name);
  64. }
  65. $photo = ORM::factory("item");
  66. $photo->type = "photo";
  67. $photo->title = $title;
  68. $photo->description = $description;
  69. $photo->name = $name;
  70. $photo->owner_id = $owner_id ? $owner_id : user::active();
  71. $photo->width = $image_info[0];
  72. $photo->height = $image_info[1];
  73. $photo->mime_type = empty($image_info['mime']) ? "application/unknown" : $image_info['mime'];
  74. $photo->thumb_dirty = 1;
  75. $photo->resize_dirty = 1;
  76. $photo->sort_column = "weight";
  77. $photo->slug = $slug;
  78. $photo->rand_key = ((float)mt_rand()) / (float)mt_getrandmax();
  79. // Randomize the name or slug if there's a conflict
  80. // @todo Improve this. Random numbers are not user friendly
  81. while (ORM::factory("item")
  82. ->where("parent_id", $parent->id)
  83. ->open_paren()
  84. ->where("name", $photo->name)
  85. ->orwhere("slug", $photo->slug)
  86. ->close_paren()
  87. ->find()->id) {
  88. $rand = rand();
  89. $photo->name = "{$name}.$rand.{$pi['extension']}";
  90. $photo->slug = "{$slug}-$rand";
  91. }
  92. // This saves the photo
  93. $photo->add_to_parent($parent);
  94. /*
  95. * If the thumb or resize already exists then rename it. We need to do this after the save
  96. * because the resize_path and thumb_path both call relative_path which caches the
  97. * path. Before add_to_parent the relative path will be incorrect.
  98. */
  99. if (file_exists($photo->resize_path()) ||
  100. file_exists($photo->thumb_path())) {
  101. $photo->name = $pi["filename"] . "-" . rand() . "." . $pi["extension"];
  102. $photo->save();
  103. }
  104. copy($filename, $photo->file_path());
  105. // @todo: publish this from inside Item_Model::save() when we refactor to the point where
  106. // there's only one save() happening here.
  107. module::event("item_created", $photo);
  108. // Build our thumbnail/resizes. If we fail to build thumbnail/resize we assume that the image
  109. // is bad in some way and discard it.
  110. try {
  111. graphics::generate($photo);
  112. } catch (Exception $e) {
  113. $photo->delete();
  114. throw $e;
  115. }
  116. // If the parent has no cover item, make this it.
  117. if (access::can("edit", $parent) && $parent->album_cover_item_id == null) {
  118. item::make_album_cover($photo);
  119. }
  120. return $photo;
  121. }
  122. static function get_add_form($parent) {
  123. $form = new Forge("albums/{$parent->id}", "", "post", array("id" => "gAddPhotoForm"));
  124. $group = $form->group("add_photo")->label(
  125. t("Add Photo to %album_title", array("album_title" => $parent->title)));
  126. $group->input("title")->label(t("Title"));
  127. $group->textarea("description")->label(t("Description"));
  128. $group->input("name")->label(t("Filename"));
  129. $group->input("slug")->label(t("Internet Address"))->value($photo->slug)
  130. ->callback("item::validate_url_safe")
  131. ->error_messages(
  132. "not_url_safe",
  133. t("The internet address should contain only letters, numbers, hyphens and underscores"));
  134. $group->upload("file")->label(t("File"))->rules("required|allow[jpg,png,gif,flv,mp4]");
  135. $group->hidden("type")->value("photo");
  136. $group->submit("")->value(t("Upload"));
  137. $form->add_rules_from(ORM::factory("item"));
  138. return $form;
  139. }
  140. static function get_edit_form($photo) {
  141. $form = new Forge("photos/$photo->id", "", "post", array("id" => "gEditPhotoForm"));
  142. $form->hidden("_method")->value("put");
  143. $group = $form->group("edit_item")->label(t("Edit Photo"));
  144. $group->input("title")->label(t("Title"))->value($photo->title);
  145. $group->textarea("description")->label(t("Description"))->value($photo->description);
  146. $group->input("filename")->label(t("Filename"))->value($photo->name)
  147. ->error_messages(
  148. "name_conflict", t("There is already a movie, photo or album with this name"))
  149. ->callback("item::validate_no_slashes")
  150. ->error_messages("no_slashes", t("The photo name can't contain a \"/\""))
  151. ->callback("item::validate_no_trailing_period")
  152. ->error_messages("no_trailing_period", t("The photo name can't end in \".\""));
  153. $group->input("slug")->label(t("Internet Address"))->value($photo->slug)
  154. ->callback("item::validate_url_safe")
  155. ->error_messages(
  156. "slug_conflict", t("There is already a movie, photo or album with this internet address"))
  157. ->error_messages(
  158. "not_url_safe",
  159. t("The internet address should contain only letters, numbers, hyphens and underscores"));
  160. module::event("item_edit_form", $photo, $form);
  161. $group = $form->group("buttons")->label("");
  162. $group->submit("")->value(t("Modify"));
  163. $form->add_rules_from(ORM::factory("item"));
  164. return $form;
  165. }
  166. /**
  167. * Return scaled width and height.
  168. *
  169. * @param integer $width
  170. * @param integer $height
  171. * @param integer $max the target size for the largest dimension
  172. * @param string $format the output format using %d placeholders for width and height
  173. */
  174. static function img_dimensions($width, $height, $max, $format="width=\"%d\" height=\"%d\"") {
  175. if (!$width || !$height) {
  176. return "";
  177. }
  178. if ($width > $height) {
  179. $new_width = $max;
  180. $new_height = (int)$max * ($height / $width);
  181. } else {
  182. $new_height = $max;
  183. $new_width = (int)$max * ($width / $height);
  184. }
  185. return sprintf($format, $new_width, $new_height);
  186. }
  187. }