1
0

Version 2 of the jpegtran plugin. Initial commit

This commit is contained in:
Anthony Callegaro 2012-08-03 23:02:23 +02:00
commit 03e5381a5e
8 changed files with 375 additions and 0 deletions

View File

@ -0,0 +1,116 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Jpegtran gallery3 module admin page
*
* Copyright (C) Anthony Callegaro
* 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 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.
*
* A copy of the GPL2 License is available here :
* http://www.gnu.org/licenses/gpl-2.0.html
*/
class Admin_Jpegtran_Controller extends Admin_Controller {
/**
* Admin page index function
*/
public function index() {
// Set page properties
$view = new Admin_View("admin.html");
$view->page_title = t("Jpegtran");
$view->content = new View("admin_jpegtran.html");
// Attempt to locate jpegtran
if (is_file($path = exec('which jpegtran'))) {
$view->content->system_path = $path;
}
// Get module parameters from the DB
$view->content->path = module::get_var("jpegtran", "path");
// First time run (no param in the DB)
if (empty($view->content->path) && ! empty($path)) {
// Add the located path in the DB
module::set_var("jpegtran", "path", $path);
$view->content->path = $path;
}
// Add the form to the page
$view->content->form = $this->_get_admin_form($view->content->path);
// Display the page
print $view;
}
/**
* Server path autocomplete function
*/
public function autocomplete() {
$directories = array();
$path_prefix = Input::instance()->get("q");
foreach (glob("{$path_prefix}*") as $file) {
// Filter only links. Display both directories and files
if (!is_link($file)) {
$directories[] = html::clean($file);
}
}
ajax::response(implode("\n", $directories));
}
/**
* Function called by the input form on button press
*/
public function save() {
access::verify_csrf();
$form = $this->_get_admin_form();
if ($form->validate()) {
$path = html_entity_decode($form->add_path->path->value);
// Check the entered path
if (is_link($path)) {
$form->add_path->path->add_error("is_symlink", 1);
} else if (!is_readable($path)) {
$form->add_path->path->add_error("not_readable", 1);
} else {
// If the path is correct, save it to the DB
module::set_var("jpegtran", "path", $path);
message::success(t("Successfully saved %path", array("path" => $path)));
jpegtran::check_config($path);
url::redirect("admin/jpegtran");
}
}
// In case of failure re-display the admin page
$view = new Admin_View("admin.html");
$view->page_title = t("Jpegtran");
$view->content = new View("admin_jpegtran.html");
$view->content->form = $form;
print $view;
}
/**
* Create the input form with the input box and save button
*/
private function _get_admin_form($path) {
$form = new Forge("admin/jpegtran/save", "", "post",
array("id" => "g-jpegtran-admin-form", "class" => "g-short-form"));
$add_path = $form->group("add_path");
$add_path->input("path")
->label(t("Path"))
->rules("required")
->id("g-path")
->value($path)
->error_messages("not_readable", t("This file is not readable by the webserver"))
->error_messages("is_symlink", t("Symbolic links are not allowed"));
$add_path->submit("save")->value(t("Save path"));
return $form;
}
}
?>

View File

@ -0,0 +1,51 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Jpegtran gallery3 module gallery_graphics_core overloading
*
* Copyright (C) Carl Streeter, Anthony Callegaro
* 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 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.
*
* A copy of the GPL2 License is available here :
* http://www.gnu.org/licenses/gpl-2.0.html
*/
class gallery_graphics extends gallery_graphics_Core {
/**
* Rotate an image. Valid options are degrees
*
* @param string $input_file
* @param string $output_file
* @param array $options
*/
static function rotate($input_file, $output_file, $options) {
graphics::init_toolkit();
module::event("graphics_rotate", $input_file, $output_file, $options);
// Convert degrees for jpegtran specific format
$jt_degrees = $options["degrees"];
if ($jt_degrees < 0) {
$jt_degrees += 360;
}
// Get path from the DB
$path = module::get_var("jpegtran", "path");
// Try to run jpegtran and falls back to the default if it fails
if($error = exec($path.' -rot '.escapeshellarg($jt_degrees).' -outfile '.escapeshellarg($output_file).' -copy all '.escapeshellarg($input_file)))
{
Image::factory($input_file)
->quality(module::get_var("gallery", "image_quality"))
->rotate($options["degrees"])
->save($output_file);
}
module::event("graphics_rotate_completed", $input_file, $output_file, $options);
}
}

39
helpers/jpegtran.php Normal file
View File

@ -0,0 +1,39 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Jpegtran gallery3 module helper functions
*
* Copyright (C) Anthony Callegaro
* 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 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.
*
* A copy of the GPL2 License is available here :
* http://www.gnu.org/licenses/gpl-2.0.html
*/
class jpegtran_Core {
/**
* Check if the module is configured and display a warning screen
* if it isn't.
*/
static function check_config($path=null) {
if ($path === null) {
$path = module::get_var("jpegtran", "path");
}
if (empty($path)) {
// Display a permanent warning to configure the plugin
site_status::warning(
t("Jpegtran needs configuration. <a href=\"%url\">Configure it now!</a>",
array("url" => html::mark_clean(url::site("admin/jpegtran")))),
"jpegtran_configuration");
} else {
// Clear the warning
site_status::clear("jpegtran_configuration");
}
}
}

View File

@ -0,0 +1,32 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Jpegtran gallery3 module admin menu
*
* Copyright (C) Anthony Callegaro
* 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 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.
*
* A copy of the GPL2 License is available here :
* http://www.gnu.org/licenses/gpl-2.0.html
*/
class jpegtran_event_Core {
/**
* Overload the admin_menu event to add jpegtran admin page in the
* settings submenu
*/
static function admin_menu($menu, $theme) {
$menu->get("settings_menu")
->append(
Menu::factory("link")
->id("jpegtran")
->label(t("Jpegtran"))
->url(url::site("admin/jpegtran")));
}
}

View File

@ -0,0 +1,44 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Jpegtran gallery3 module installer
*
* Copyright (C) Anthony Callegaro
* 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 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.
*
* A copy of the GPL2 License is available here :
* http://www.gnu.org/licenses/gpl-2.0.html
*/
class jpegtran_installer {
/**
* This will run on install (only on the first time !)
* If you want to re-run this function you need to delete the module
* entry from the DB.
*/
static function install() {
module::set_version("jpegtran", 2);
// Attempt to locate jpegtran
if (is_file($path = exec('which jpegtran'))) {
module::set_var("jpegtran", "path", $path);
}
// Run check_config to display an error if jpegtran needs to be configured
jpegtran::check_config();
}
/**
* Disable the configuration warning on uninstall
*/
static function deactivate() {
site_status::clear("jpegtran_configuration");
// module::delete doesn't seems to do anything
module::delete("jpegtran");
}
}

35
jpegtran Normal file
View File

@ -0,0 +1,35 @@
[file_prefs]
final_new_line=true
ensure_convert_new_lines=false
strip_trailing_spaces=false
replace_tabs=true
[indentation]
indent_width=2
indent_type=0
indent_hard_tab_width=8
detect_indent=false
detect_indent_width=false
indent_mode=2
[project]
name=jpegtran
base_path=/home/letic/work/wps/jpegtran
description=
file_patterns=
[long line marker]
long_line_behaviour=1
long_line_column=72
[files]
current_page=4
FILE_NAME_0=292;None;0;16;1;1;0;%2Fhome%2Fletic%2Fwork%2Fwps%2Fjpegtran%2Fmodule.info;0;4
FILE_NAME_1=757;PHP;0;16;0;1;0;%2Fhome%2Fletic%2Fwork%2Fwps%2Fjpegtran%2Fviews%2Fadmin_jpegtran.html.php;0;2
FILE_NAME_2=989;PHP;0;16;0;1;0;%2Fhome%2Fletic%2Fwork%2Fwps%2Fjpegtran%2Fhelpers%2Fjpegtran_event.php;0;2
FILE_NAME_3=2407;PHP;0;16;0;1;0;%2Fhome%2Fletic%2Fwork%2Fwps%2Fjpegtran%2Fcontrollers%2Fadmin_jpegtran.php;0;2
FILE_NAME_4=1502;PHP;0;16;0;1;0;%2Fhome%2Fletic%2Fwork%2Fwps%2Fjpegtran%2Fhelpers%2FMY_gallery_graphics.php;0;2
[VTE]
last_dir=/home/letic/work/wps

7
module.info Executable file
View File

@ -0,0 +1,7 @@
name = Jpegtran
description = "Rotate images using jpegtran"
version = 2
author_name = "Carl Streeter and LeTic"
author_url = "http://codex.gallery2.org/Gallery:Team"
info_url = "http://codex.gallery2.org/Gallery3:Modules:jpegtran"
discuss_url = "http://gallery.menalto.com/node/98524#comment-356535"

View File

@ -0,0 +1,51 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<?php
/**
* Jpegtran gallery3 module admin page template
*
* Copyright (C) Anthony Callegaro
* 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 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.
*
* A copy of the GPL2 License is available here :
* http://www.gnu.org/licenses/gpl-2.0.html
*/
?>
<?= $theme->css("jquery.autocomplete.css") ?>
<?= $theme->script("jquery.autocomplete.js") ?>
<script type="text/javascript">
$("document").ready(function() {
$("#g-path").gallery_autocomplete(
"<?= url::site("__ARGS__") ?>".replace("__ARGS__", "admin/jpegtran/autocomplete"),
{
max: 256,
loadingClass: "g-loading-small",
});
});
</script>
<div class="g-block">
<h1> <?= t("Jpegtran") ?> </h1>
<div class="g-block-content">
<p>
<?= t("Jpegtran is a lossless tool provided by libjpeg that is able to rotate/optimise/resize images without any re-encoding.") ?>
</p>
<? if (! empty($system_path)): ?>
<p>
<?= t("Jpegtran has been found in $system_path. You can put a custom value below.") ?>
</p>
<? else: ?>
<p>
<?= t("Jpegtran has not been detected on your system. Please enter its full path below :") ?>
</p>
<? endif ?>
<?= $form ?>
</div>
</div>