94152a085a78e9429ced07686935c6e5c2a4f326
[yaffs-website] / vendor / lsolesen / pel / examples / resize.php
1 #!/usr/bin/php
2 <?php
3
4 /**
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.
8  *
9  * Copyright (C) 2006, 2007 Martin Geisler.
10  *
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.
15  *
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.
20  *
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
25  */
26
27 /* a printf() variant that appends a newline to the output. */
28 function println($args)
29 {
30     $args = func_get_args();
31     $fmt = array_shift($args);
32     vprintf($fmt . "\n", $args);
33 }
34
35 /* Make PEL speak the users language, if it is available. */
36 setlocale(LC_ALL, '');
37
38 /* Load the required PEL files for handling JPEG images. */
39 require_once '../autoload.php';
40
41 use lsolesen\pel\PelJpeg;
42
43 /*
44  * Store the name of the script in $prog and remove this first part of
45  * the command line.
46  */
47 $prog = array_shift($argv);
48 $error = false;
49
50 /*
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.
53  */
54 if (isset($argv[0]) && $argv[0] == '-d') {
55     Pel::setDebug(true);
56     array_shift($argv);
57 }
58
59 /* The mandatory input filename. */
60 if (isset($argv[0])) {
61     $input = array_shift($argv);
62 } else {
63     $error = true;
64 }
65
66 /* The mandatory output filename. */
67 if (isset($argv[0])) {
68     $output = array_shift($argv);
69 } else {
70     $error = true;
71 }
72
73 /* The mandatory scale factor. */
74 if (isset($argv[0])) {
75     $scale = array_shift($argv);
76 } else {
77     $error = true;
78 }
79
80 /*
81  * Usage information is printed if an error was found in the command
82  * line arguments.
83  */
84 if ($error) {
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.');
92     exit(1);
93 }
94
95 /* The input file is now loaded into a PelJpeg object. */
96 println('Reading file "%s".', $input);
97 $input_jpeg = new PelJpeg($input);
98
99 /*
100  * The input image is already loaded, so we can reuse the bytes stored
101  * in $input_jpeg when creating the Image resource.
102  */
103 $original = ImageCreateFromString($input_jpeg->getBytes());
104 $original_w = ImagesX($original);
105 $original_h = ImagesY($original);
106
107 $scaled_w = $original_w * $scale;
108 $scaled_h = $original_h * $scale;
109
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);
113
114 /*
115  * We want the raw JPEG data from $scaled. Luckily, one can create a
116  * PelJpeg object from an image resource directly:
117  */
118 $output_jpeg = new PelJpeg($scaled);
119
120 /* Retrieve the original Exif data in $jpeg (if any). */
121 $exif = $input_jpeg->getExif();
122
123 /* If no Exif data was present, then $exif is null. */
124 if ($exif != null) {
125     $output_jpeg->setExif($exif);
126 }
127
128 /* We can now save the scaled image. */
129 println('Writing file "%s".', $output);
130 $output_jpeg->saveFile($output);