Skip to content
Browse files

Add new PDF module. Requires Gallery v3.0.5 or greater (v56+).

@todo - add discuss url
  • Loading branch information...
1 parent 0a06e60 commit edf7b8fcd5a8eee31e4534f89cb31b2e65558e52 @shadlaws shadlaws committed
View
74 3.0/modules/pdf/controllers/admin_pdf.php
@@ -0,0 +1,74 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2013 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Admin_Pdf_Controller extends Admin_Controller {
+ public function index() {
+ // print screen from new form
+ $form = $this->_get_admin_form();
+ $this->_print_screen($form);
+ }
+
+ public function save() {
+ access::verify_csrf();
+ $form = $this->_get_admin_form();
+ if ($form->validate()) {
+ module::set_var("pdf", "make_thumb", ($form->settings->make_thumb->value == 1));
+ module::set_var("pdf", "movie_overlay_hide", ($form->settings->movie_overlay_hide->value == 1));
+ if ($form->settings->rebuild_thumbs->value == 1) {
+ pdf::mark_dirty();
+ }
+ // All done - redirect with message
+ message::success(t("PDF settings updated successfully"));
+ url::redirect("admin/pdf");
+ }
+ // Not valid - print screen from existing form
+ $this->_print_screen($form);
+ }
+
+ private function _print_screen($form) {
+ // get module parameters
+ $gs_path = pdf::find_gs();
+ $gs_dir = substr($gs_path, 0, strrpos($gs_path, "/"));
+ $gs_version = pdf::get_gs_version();
+ // make and print view
+ $view = new Admin_View("admin.html");
+ $view->page_title = t("PDF settings");
+ $view->content = new View("admin_pdf.html");
+ $view->content->form = $form;
+ $view->content->gs_dir = $gs_dir;
+ $view->content->gs_version = $gs_version;
+ print $view;
+ }
+
+ private function _get_admin_form() {
+ $form = new Forge("admin/pdf/save", "", "post", array("id" => "g-pdf-admin-form"));
+ $group = $form->group("settings")->label(t("PDF settings"));
+ $group->checkbox("make_thumb")
+ ->label(t("Generate thumbnails (only if Ghostscript is found)"))
+ ->checked(module::get_var("pdf", "make_thumb", null));
+ $group->checkbox("movie_overlay_hide")
+ ->label(t("Hide overlay from \"movie_overlay\" module (tested with Movie Overlay v3)"))
+ ->checked(module::get_var("pdf", "movie_overlay_hide", null));
+ $group->checkbox("rebuild_thumbs")
+ ->label(t("Mark all existing PDFs for rebuild - afterward, go to Maintenace | Rebuild Images"))
+ ->checked(false); // always set as false
+ $form->submit("")->value(t("Save"));
+ return $form;
+ }
+}
View
3 3.0/modules/pdf/css/pdf_resize.css
@@ -0,0 +1,3 @@
+.g-pdf-resize {
+ width: 100%; height: 70%; background: transparent;
+}
View
3 3.0/modules/pdf/css/pdf_thumb.css
@@ -0,0 +1,3 @@
+.g-pdf-thumb .g-movie-thumb span {
+ display: none;
+}
View
67 3.0/modules/pdf/helpers/pdf.php
@@ -0,0 +1,67 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2013 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class pdf_Core {
+ /**
+ * Return the path to the gs binary if one exists and is executable, or null.
+ * (ref: movie::find_ffmpeg())
+ */
+ static function find_gs() {
+ if (!($gs_path = module::get_var("pdf", "gs_path")) || !@is_executable($gs_path)) {
+ $gs_path = system::find_binary("gs", module::get_var("gallery", "graphics_toolkit_path"));
+ module::set_var("pdf", "gs_path", $gs_path);
+ }
+ return $gs_path;
+ }
+
+ /**
+ * Return version number of gs if found, an empty string if not.
+ * (ref: movie::get_ffmpeg_version())
+ */
+ static function get_gs_version() {
+ if (pdf::find_gs()) {
+ $path = module::get_var("pdf", "gs_path");
+ exec(escapeshellcmd($path)." -version", $output);
+ if (stristr($output[0], "ghostscript")) {
+ // Found "ghostscript" in the response - it's valid.
+ return $output[0];
+ }
+ }
+ return "";
+ }
+
+ /**
+ * Mark all PDF thumbs as dirty
+ * (ref: graphics::mark_dirty())
+ */
+ static function mark_dirty() {
+ graphics::mark_dirty(true, false, "movie", "application/pdf");
+ }
+
+ /**
+ * Fix mime types of all existing PDFs (could be set incorrectly before this module was installed)
+ */
+ static function fix_mime_types() {
+ $db = db::build()
+ ->update("items")
+ ->set("mime_type", "application/pdf")
+ ->where("name", "REGEXP", "\.pdf$") // ends in pdf
+ ->execute();
+ }
+}
View
98 3.0/modules/pdf/helpers/pdf_event.php
@@ -0,0 +1,98 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2013 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class pdf_event_Core {
+
+ /**
+ * Add PDFs as a type of "movie"
+ * (ref: legal_file::get_movie_types_by_extension())
+ */
+ static function movie_types_by_extension($types_by_extension_wrapper) {
+ $types_by_extension_wrapper->types_by_extension["pdf"] = "application/pdf";
+ }
+
+ /**
+ * Generate the thumbnails for PDFs
+ * (ref: graphics::generate())
+ */
+ static function movie_extract_frame($input_file, $output_file, $movie_options_wrapper=null, $item=null) {
+ $RESOLUTION = 300; // (DPI) - we extract high-res frames so resize has plenty to work with.
+ if (strtolower(pathinfo($input_file, PATHINFO_EXTENSION)) == "pdf") {
+ if (module::get_var("pdf", "make_thumb") && ($path = pdf::find_gs())) {
+ // Enabled and gs found - make the thumb.
+ $path = module::get_var("pdf", "gs_path");
+ $exec_args = " -q -dBATCH -dMaxBitmap=500000000 -dNOPAUSE -dSAFER -sDEVICE=jpeg" .
+ " -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r$RESOLUTION -dFirstPage=1" .
+ " -dLastPage=1 -sOutputFile=";
+ $exec_end = " -c quit";
+ exec(escapeshellcmd($path) . $exec_args . escapeshellarg($output_file) . " " .
+ escapeshellarg($input_file) . $exec_end, $exec_output, $exec_status);
+ if (!$exec_status && filesize($output_file)) {
+ // Thumb generated without reported errors - we're done.
+ return;
+ }
+ }
+ // It's a PDF, but the thumb was blank/nonexistant or had errors, or gs wasn't
+ // configured/enabled - copy PDF icon from images instead
+ copy(MODPATH . "pdf/images/ico-pdf.jpg", $output_file);
+ }
+ }
+
+ /**
+ * Make resize image to show the PDF object
+ * (ref: Item_Model::movie_img())
+ */
+ static function movie_img($movie_img, $item) {
+ if (strtolower(pathinfo($item->name, PATHINFO_EXTENSION)) == "pdf") {
+ $view = new View("pdf_movie_img.html");
+ $view->url = $item->file_url(true);
+ $view->attrs = $movie_img->attrs;
+ $view->object_attrs = array("data" => $view->url, "class" => "g-pdf-resize",
+ "type" => "application/pdf");
+ $movie_img->view[] = $view;
+ }
+ }
+
+ /**
+ * Add admin menu
+ * (ref: Admin_View::admin_menu())
+ */
+ static function admin_menu($menu, $theme) {
+ $menu->get("settings_menu")
+ ->append(
+ Menu::factory("link")
+ ->id("pdf")
+ ->label(t("PDF"))
+ ->url(url::site("admin/pdf")));
+ }
+
+ /**
+ * Get PDF file metadata (height and width)
+ * (ref: movie::get_file_metadata())
+ */
+ static function movie_get_file_metadata($file_path, $metadata) {
+ if ((strtolower(pathinfo($file_path, PATHINFO_EXTENSION)) == "pdf") && ($path = pdf::find_gs())) {
+ // Parsing gs output properly can be a pain. So, let's go for a reliable albeit inefficient
+ // approach: re-extract the frame (into tmp) and get its image size.
+ $temp_file = system::temp_filename("pdf_", "jpg");
+ pdf_event::movie_extract_frame($file_path, $temp_file, null, null);
+ list($metadata->height, $metadata->width) = photo::get_file_metadata($temp_file);
+ }
+ }
+}
View
50 3.0/modules/pdf/helpers/pdf_installer.php
@@ -0,0 +1,50 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2013 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class pdf_installer {
+ static function install() {
+ // Set gs_path variable
+ if ($gs_path = exec("which gs")) {
+ module::set_var("pdf", "gs_path", $gs_path);
+ }
+ pdf::find_gs();
+ // Set other variables
+ module::set_var("pdf", "make_thumb", true);
+ module::set_var("pdf", "movie_overlay_hide", false);
+ module::set_version("pdf", 1);
+ }
+
+ static function can_activate() {
+ $messages = array();
+ if (module::get_version("gallery") < 56) {
+ $messages["warn"][] = t("PDF requires Gallery v3.0.5 or newer.");
+ }
+ return $messages;
+ }
+
+ static function activate() {
+ // Fix mime types of all existing PDFs
+ pdf::fix_mime_types();
+ }
+
+ static function uninstall() {
+ // Delete vars from database
+ module::clear_all_vars("pdf");
+ }
+}
View
38 3.0/modules/pdf/helpers/pdf_theme.php
@@ -0,0 +1,38 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2013 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class pdf_theme_Core {
+ // Add CSS for PDF resize view
+ static function head($theme) {
+ $buf = $theme->css("pdf_resize.css");
+ if (module::get_var("pdf", "movie_overlay_hide", false)) {
+ $buf .= $theme->css("pdf_thumb.css");
+ }
+ return $buf;
+ }
+
+ // Add class for thumb view (useful for CSS)
+ static function thumb_bottom($theme, $child) {
+ if (strtolower(pathinfo($child->name, PATHINFO_EXTENSION)) == "pdf") {
+ $id = "g-item-id-" . $child->id;
+ $class = "g-pdf-thumb";
+ return "<script type=\"text/javascript\">$(\"#$id\").addClass(\"$class\");</script>";
+ }
+ }
+}
View
BIN 3.0/modules/pdf/images/ghostscript.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN 3.0/modules/pdf/images/ico-pdf.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
7 3.0/modules/pdf/module.info
@@ -0,0 +1,7 @@
+name = PDF
+description = "Add PDF support for uploading, viewing, and generating thumbnails."
+version = 1
+author_name = "Shad Laws"
+author_url = ""
+info_url = "http://codex.galleryproject.org/Gallery3:Modules:pdf"
+discuss_url = ""
View
42 3.0/modules/pdf/views/admin_pdf.html.php
@@ -0,0 +1,42 @@
+<?php defined("SYSPATH") or die("No direct script access.") ?>
+<div id="g-pdf-admin" class="g-block ui-helper-clearfix">
+ <h1> <?= t("PDF settings") ?> </h1>
+ <p>
+ <?= t("This module adds PDF support to Gallery, namely by:") ?>
+ </p>
+ <p>
+ <b><?= t("Enabling PDF as a valid file type") ?></b>: <?= t("PDF is included as a \"movie\" type, allowing uploads as normal with the uploader, server_add, etc. This requires that movie uploads be allowed (see <a href=\"%admin_movies_url\">here</a>).",
+ array("admin_movies_url" => "movies")) ?><br/>
+ <b><?= t("Showing PDF in resize view") ?></b>: <?= t("Instead of showing the resize image (or movie), an embedded view of the PDF is shown.") ?><br/>
+ <b><?= t("Generating thumbnails") ?></b>: <?= t("JPG thumbnails are created from the first page of the PDF. This requires Ghostscript to be installed. If Ghostscript is unavailable or disabled below, a PDF icon is used instead.") ?><br/>
+ </p>
+ <p>
+ <?= t("Although popular, Ghostscript is not installed on all Linux systems.") ?>
+ <?= t("To use Ghostscript without fully installing it, download a pre-compiled version from one of the links <a href=\"%url\">here</a>.", array("url" => "http://www.ghostscript.com/download/gsdnld.html")) ?>
+ <?= t("Then, rename the binary file to \"gs\" and put it in Gallery's \"bin\" directory (e.g. \"/gallery3/bin\"), where Gallery will auto-detect it.") ?>
+ </p>
+
+ <div class="g-available">
+ <h2> <?= t("Current Ghostscript configuration") ?> </h2>
+ <div id="g-gs" class="g-block">
+ <img class="logo" width="100" height="100" src="<?= url::file("modules/pdf/images/ghostscript.png") ?>" alt="<? t("Visit the Ghostscript project site") ?>" />
+ <p>
+ <?= t("Ghostscript is an interpreter for the PostScript language and for PDF.") ?><br/>
+ <?= t("Please refer to the <a href=\"%url\">Ghostscript website</a> for more information.", array("url" => "http://www.ghostscript.com/")) ?>
+ </p>
+ <div class="g-module-status g-info">
+ <? if ($gs_dir): ?>
+ <? if ($gs_version): ?>
+ <p><?= t("%version was found in %dir", array("version" => $gs_version, "dir" => $gs_dir)) ?></p>
+ <? else: ?>
+ <p><?= t("Ghostscript (of unknown version) was found in %dir", array("dir" => $gs_dir)) ?></p>
+ <? endif ?>
+ <? else: ?>
+ <p><?= t("We could not locate Ghostscript on your system.") ?></p>
+ <? endif ?>
+ </div>
+ </div>
+ </div>
+
+ <?= $form ?>
+</div>
View
6 3.0/modules/pdf/views/pdf_movie_img.html.php
@@ -0,0 +1,6 @@
+<?php defined("SYSPATH") or die("No direct script access.") ?>
+<a href="<?= $url ?>" <?= html::attributes($attrs) ?>>
+ <object <?= html::attributes($object_attrs) ?>>
+ <p><?= t("Your web browser doesn't have a PDF plugin. Click here to download the PDF file instead.") ?></p>
+ </object>
+</a>

0 comments on commit edf7b8f

Please sign in to comment.
Something went wrong with that request. Please try again.