5 * PEL: PHP Exif Library.
6 * A library with support for reading and
7 * writing all Exif headers in JPEG and TIFF images using PHP.
9 * Copyright (C) 2006, 2007 Martin Geisler.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program in the file COPYING; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
24 * Boston, MA 02110-1301 USA
27 /* a printf() variant that appends a newline to the output. */
28 function println($args)
30 $args = func_get_args();
31 $fmt = array_shift($args);
32 vprintf($fmt . "\n", $args);
35 /* Make PEL speak the users language, if it is available. */
36 setlocale(LC_ALL, '');
38 /* Load the required PEL files for handling JPEG images. */
39 require_once '../autoload.php';
41 use lsolesen\pel\PelJpeg;
44 * Store the name of the script in $prog and remove this first part of
47 $prog = array_shift($argv);
51 * The next argument could be -d to signal debug mode where lots of
52 * extra information is printed out when the image is parsed.
54 if (isset($argv[0]) && $argv[0] == '-d') {
59 /* The mandatory input filename. */
60 if (isset($argv[0])) {
61 $input = array_shift($argv);
66 /* The mandatory output filename. */
67 if (isset($argv[0])) {
68 $output = array_shift($argv);
73 /* The mandatory scale factor. */
74 if (isset($argv[0])) {
75 $scale = array_shift($argv);
81 * Usage information is printed if an error was found in the command
85 println('Usage: %s [-d] <input> <output> <scale>', $prog);
86 println('Optional arguments:');
87 println(' -d turn debug output on.');
88 println('Mandatory arguments:');
89 println(' input the input filename, a JPEG image.');
90 println(' output filename for saving the changed image.');
91 println(' scale scale factor, say 0.5 to resize to half the ' . 'original size.');
95 /* The input file is now loaded into a PelJpeg object. */
96 println('Reading file "%s".', $input);
97 $input_jpeg = new PelJpeg($input);
100 * The input image is already loaded, so we can reuse the bytes stored
101 * in $input_jpeg when creating the Image resource.
103 $original = ImageCreateFromString($input_jpeg->getBytes());
104 $original_w = ImagesX($original);
105 $original_h = ImagesY($original);
107 $scaled_w = $original_w * $scale;
108 $scaled_h = $original_h * $scale;
110 /* Now create the scaled image. */
111 $scaled = ImageCreateTrueColor($scaled_w, $scaled_h);
112 ImageCopyResampled($scaled, $original, 0, 0, 0, 0, $scaled_w, $scaled_h, $original_w, $original_h);
115 * We want the raw JPEG data from $scaled. Luckily, one can create a
116 * PelJpeg object from an image resource directly:
118 $output_jpeg = new PelJpeg($scaled);
120 /* Retrieve the original Exif data in $jpeg (if any). */
121 $exif = $input_jpeg->getExif();
123 /* If no Exif data was present, then $exif is null. */
125 $output_jpeg->setExif($exif);
128 /* We can now save the scaled image. */
129 println('Writing file "%s".', $output);
130 $output_jpeg->saveFile($output);