1<?php
2/*
3 * Gallery - a web based photo album viewer and editor
4 * Copyright (C) 2000-2008 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
21GalleryCoreApi::requireOnce('modules/sizelimit/classes/SizeLimitHelper.class');
22
23/**
24 * ItemAdd option using the size limit values to resize gallery images when uploaded.
25 * @package SizeLimit
26 * @subpackage UserInterface
27 * @author Felix Rabinovich <felix@rabinovich.org>
28 * @version $Revision: 17580 $
29 */
30class SetSizeOption extends ItemAddOption {
31
32    /**
33     * @see ItemAddOption::isAppropriate
34     */
35    function isAppropriate() {
36	return array(null, true);
37    }
38
39    /**
40     * @see ItemAddOption::handleRequestAfterAdd
41     */
42    function handleRequestAfterAdd($form, $items) {
43	$errors = array();
44	$warnings = array();
45	list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'sizelimit');
46	if ($ret) {
47	    return array($ret, null, null);
48	}
49	foreach ($items as $i => $item) {
50	    $warnings[$i] = array();
51	    if (!GalleryUtilities::isA($item, 'GalleryDataItem')) {
52		continue;
53	    }
54
55	    list ($ret, $param) = GalleryCoreApi::fetchAllPluginParameters(
56		'module', 'sizelimit', $item->getParentId());
57	    if ($ret) {
58		return array($ret, null, null);
59	    }
60	    $param = array_merge(
61		array('keepOriginal' => 0, 'width' => 0, 'height' => 0, 'size' => 0), $param);
62
63	    if ($param['width'] && $param['height'] && method_exists($item, 'getwidth') &&
64		    method_exists($item, 'getheight') &&
65		    ($item->getWidth() > $param['width'] ||
66		     $item->getHeight() > $param['height'])) {
67		$args = array($param['width'], $param['height']);
68		if ($param['keepOriginal']) {
69		    $ret = SizeLimitHelper::buildDerivativeWithLimits($item, 'scale', $args);
70		} else {
71		    $ret = SizeLimitHelper::applyLimits($item, 'scale', $args);
72		}
73		if ($ret) {
74		    if ($ret->getErrorCode() & ERROR_UNSUPPORTED_FILE_TYPE) {
75			$warnings[$i][] = $module->translate(
76			    array('text' => 'WARNING: Cannot resize mime type %s',
77				  'arg1' => $item->getMimeType()));
78		    } else {
79			return array($ret, null, null);
80		    }
81		}
82	    }
83
84	    if (isset($param['size']) && $param['size'] > 0 &&
85		    ($item->getSize() >> 10) > $param['size']) {
86		$args = array($param['size']);
87		if ($param['keepOriginal']) {
88		    $ret = SizeLimitHelper::buildDerivativeWithLimits($item, 'compress',
89								      $args);
90		} else {
91		    $ret = SizeLimitHelper::applyLimits($item, 'compress', $args);
92		}
93		if ($ret) {
94		    if ($ret->getErrorCode() & ERROR_UNSUPPORTED_FILE_TYPE) {
95			$warnings[$i][] = $module->translate(
96			    array('text' => 'WARNING: Cannot compress mime type %s',
97				  'arg1' => $item->getMimeType()));
98		    } else {
99			return array($ret, null, null);
100		    }
101		}
102	    }
103	}
104	return array(null, $errors, $warnings);
105    }
106}
107?>
108