default services conflit ?

This commit is contained in:
armansansd
2022-04-27 11:30:43 +02:00
parent 28190a5749
commit 8bb1064a3b
8132 changed files with 900138 additions and 426 deletions

View File

@@ -0,0 +1,56 @@
# README file for PEL Examples
## PEL Examples
This directory contains various examples of how PEL can be used in
small scripts. The scripts are meant to be executed from the command
line and they assume that you have PHP installed as /usr/bin/php. If
that is not the case, then please adjust the first line before
executing the scripts, or simply execute them as
```
% php <script> <arguments>
```
and let your shell find the PHP executable.
The examples are:
* dirsort.php: an example of how to use PEL to automatically sort a
collection of photos into different folders.
The script accepts a number of filenames on the command line, and
will move them to folders based on the month the photo was taken.
* dump-image.php: a simple script that will load an image and show the
structure.
A JPEG or TIFF image should be specified on the command line. The
dump will give detailed information about the structure in the
image.
* edit-description.php: a big example showing how to update the
ImageDescription Exif tag using PEL.
The script is heavely commented, and should be used as a good
reference as to how PEL works and how it should be used.
* gps.php: shows how to add GPS information to an image using PEL.
* rename.php: a script similar to dirsort.php, only that this one will
rename photos to a filename containing the full date and time taken
from the DateTime Exif tag.
* resize.php: a script to resize images.
## Contributing New Examples
More examples are needed! If you have a neat example of how PEL can
be used, then please contribute it to PEL. Please submit it as a new
item in the Github issue tracker:
http://github.com/lsolesen/pel/issues
Or issue a pull request. It will then be included in the next release
of PEL, of course with full credit in the AUTHORS file.

View File

@@ -0,0 +1,106 @@
#!/usr/bin/php
<?php
/**
* PEL: PHP Exif Library.
* A library with support for reading and
* writing all Exif headers in JPEG and TIFF images using PHP.
*
* Copyright (C) 2004, 2006 Martin Geisler.
*
* 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 in the file COPYING; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/* a printf() variant that appends a newline to the output. */
use lsolesen\pel\Pel;
use lsolesen\pel\PelDataWindow;
use lsolesen\pel\PelJpeg;
use lsolesen\pel\PelTag;
use lsolesen\pel\PelTiff;
function println($args)
{
$args = func_get_args();
$fmt = array_shift($args);
vprintf($fmt . "\n", $args);
}
/* Make PEL speak the users language, if it is available. */
setlocale(LC_ALL, '');
$prog = array_shift($argv);
if (isset($argv[0]) && $argv[0] == '-d') {
Pel::setDebug(true);
array_shift($argv);
}
if (empty($argv)) {
println('Usage: %s [-d] <file> ...', $prog);
println('Optional arguments:');
println(' -d turn debug output on.');
println('Mandatory arguments:');
println(' file ... one or more file names.');
exit(1);
}
/*
* We typically need lots of RAM to parse TIFF images since they tend
* to be big and uncompressed.
*/
ini_set('memory_limit', '32M');
foreach ($argv as $file) {
println('Reading file "%s".', $file);
$data = new PelDataWindow(file_get_contents($file));
if (PelJpeg::isValid($data)) {
$jpeg = new PelJpeg();
$jpeg->load($data);
$app1 = $jpeg->getExif();
if ($app1 == null) {
println('Skipping %s because no APP1 section was found.', $file);
continue;
}
$tiff = $app1->getTiff();
} elseif (PelTiff::isValid($data)) {
$tiff = new PelTiff($data);
} else {
println('Unrecognized image format! Skipping.');
continue;
}
$ifd0 = $tiff->getIfd();
$entry = $ifd0->getEntry(PelTag::DATE_TIME);
if ($entry == null) {
println('Skipping %s because no DATE_TIME tag was found.', $file);
continue;
}
$time = $entry->getValue();
$new = gmdate('../Y-m/', $time) . $file;
if (file_exists($new)) {
die('Aborting, ' . $new . ' exists!');
}
println('mv %s %s', $file, $new);
rename($file, $new);
}

View File

@@ -0,0 +1,98 @@
#!/usr/bin/php
<?php
/**
* PEL: PHP Exif Library.
* A library with support for reading and
* writing all Exif headers in JPEG and TIFF images using PHP.
*
* Copyright (C) 2004, 2005, 2006 Martin Geisler.
*
* 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 in the file COPYING; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/* Make PEL speak the users language, if it is available. */
setlocale(LC_ALL, '');
use lsolesen\pel\Pel;
use lsolesen\pel\PelConvert;
use lsolesen\pel\PelDataWindow;
use lsolesen\pel\PelJpeg;
use lsolesen\pel\PelTiff;
$prog = array_shift($argv);
$file = '';
while (! empty($argv)) {
switch ($argv[0]) {
case '-d':
Pel::setDebug(true);
break;
case '-s':
Pel::setStrictParsing(true);
break;
default:
$file = $argv[0];
break;
}
array_shift($argv);
}
if (empty($file)) {
printf("Usage: %s [-d] [-s] <filename>\n", $prog);
print("Optional arguments:\n");
print(" -d turn debug output on.\n");
print(" -s turn strict parsing on (halt on errors).\n");
print("Mandatory arguments:\n");
print(" filename a JPEG or TIFF image.\n");
exit(1);
}
if (! is_readable($file)) {
printf("Unable to read %s!\n", $file);
exit(1);
}
/*
* We typically need lots of RAM to parse TIFF images since they tend
* to be big and uncompressed.
*/
ini_set('memory_limit', '32M');
$data = new PelDataWindow(file_get_contents($file));
if (PelJpeg::isValid($data)) {
$img = new PelJpeg();
} elseif (PelTiff::isValid($data)) {
$img = new PelTiff();
} else {
print("Unrecognized image format! The first 16 bytes follow:\n");
PelConvert::bytesToDump($data->getBytes(0, 16));
exit(1);
}
/* Try loading the data. */
$img->load($data);
print($img);
/* Deal with any exceptions: */
if (count(Pel::getExceptions()) > 0) {
print("\nThe following errors were encountered while loading the image:\n");
foreach (Pel::getExceptions() as $e) {
print("\n" . $e->__toString());
}
}

View File

@@ -0,0 +1,245 @@
#!/usr/bin/php
<?php
/**
* PEL: PHP Exif Library.
* A library with support for reading and
* writing all Exif headers in JPEG and TIFF images using PHP.
*
* Copyright (C) 2004, 2005, 2006, 2007 Martin Geisler.
*
* 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 in the file COPYING; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/* a printf() variant that appends a newline to the output. */
use lsolesen\pel\Pel;
use lsolesen\pel\PelConvert;
use lsolesen\pel\PelDataWindow;
use lsolesen\pel\PelEntryAscii;
use lsolesen\pel\PelExif;
use lsolesen\pel\PelIfd;
use lsolesen\pel\PelJpeg;
use lsolesen\pel\PelTag;
use lsolesen\pel\PelTiff;
function println($args)
{
$args = func_get_args();
$fmt = array_shift($args);
vprintf($fmt . "\n", $args);
}
/* Make PEL speak the users language, if it is available. */
setlocale(LC_ALL, '');
/*
* Store the name of the script in $prog and remove this first part of
* the command line.
*/
$prog = array_shift($argv);
$error = false;
/*
* The next argument could be -d to signal debug mode where lots of
* extra information is printed out when the image is parsed.
*/
if (isset($argv[0]) && $argv[0] == '-d') {
Pel::setDebug(true);
array_shift($argv);
}
/* The mandatory input filename. */
if (isset($argv[0])) {
$input = array_shift($argv);
} else {
$error = true;
}
/* The mandatory output filename. */
if (isset($argv[0])) {
$output = array_shift($argv);
} else {
$error = true;
}
/*
* Usage information is printed if an error was found in the command
* line arguments.
*/
if ($error) {
println('Usage: %s [-d] <input> <output> [desc]', $prog);
println('Optional arguments:');
println(' -d turn debug output on.');
println(' desc the new description.');
println('Mandatory arguments:');
println(' input the input file, a JPEG or TIFF image.');
println(' output the output file for the changed image.');
exit(1);
}
/* Any remaining arguments are considered the new description. */
$description = implode(' ', $argv);
/*
* We typically need lots of RAM to parse TIFF images since they tend
* to be big and uncompressed.
*/
ini_set('memory_limit', '32M');
/*
* The input file is now read into a PelDataWindow object. At this
* point we do not know if the file stores JPEG or TIFF data, so
* instead of using one of the loadFile methods on PelJpeg or PelTiff
* we store the data in a PelDataWindow.
*/
println('Reading file "%s".', $input);
$data = new PelDataWindow(file_get_contents($input));
/*
* The static isValid methods in PelJpeg and PelTiff will tell us in
* an efficient maner which kind of data we are dealing with.
*/
if (PelJpeg::isValid($data)) {
/*
* The data was recognized as JPEG data, so we create a new empty
* PelJpeg object which will hold it. When we want to save the
* image again, we need to know which object to same (using the
* getBytes method), so we store $jpeg as $file too.
*/
$jpeg = $file = new PelJpeg();
/*
* We then load the data from the PelDataWindow into our PelJpeg
* object. No copying of data will be done, the PelJpeg object will
* simply remember that it is to ask the PelDataWindow for data when
* required.
*/
$jpeg->load($data);
/*
* The PelJpeg object contains a number of sections, one of which
* might be our Exif data. The getExif() method is a convenient way
* of getting the right section with a minimum of fuzz.
*/
$exif = $jpeg->getExif();
if ($exif == null) {
/*
* Ups, there is no APP1 section in the JPEG file. This is where
* the Exif data should be.
*/
println('No APP1 section found, added new.');
/*
* In this case we simply create a new APP1 section (a PelExif
* object) and adds it to the PelJpeg object.
*/
$exif = new PelExif();
$jpeg->setExif($exif);
/* We then create an empty TIFF structure in the APP1 section. */
$tiff = new PelTiff();
$exif->setTiff($tiff);
} else {
/*
* Surprice, surprice: Exif data is really just TIFF data! So we
* extract the PelTiff object for later use.
*/
println('Found existing APP1 section.');
$tiff = $exif->getTiff();
}
} elseif (PelTiff::isValid($data)) {
/*
* The data was recognized as TIFF data. We prepare a PelTiff
* object to hold it, and record in $file that the PelTiff object is
* the top-most object (the one on which we will call getBytes).
*/
$tiff = $file = new PelTiff();
/* Now load the data. */
$tiff->load($data);
} else {
/*
* The data was not recognized as either JPEG or TIFF data.
* Complain loudly, dump the first 16 bytes, and exit.
*/
println('Unrecognized image format! The first 16 bytes follow:');
PelConvert::bytesToDump($data->getBytes(0, 16));
exit(1);
}
/*
* TIFF data has a tree structure much like a file system. There is a
* root IFD (Image File Directory) which contains a number of entries
* and maybe a link to the next IFD. The IFDs are chained together
* like this, but some of them can also contain what is known as
* sub-IFDs. For our purpose we only need the first IFD, for this is
* where the image description should be stored.
*/
$ifd0 = $tiff->getIfd();
if ($ifd0 == null) {
/*
* No IFD in the TIFF data? This probably means that the image
* didn't have any Exif information to start with, and so an empty
* PelTiff object was inserted by the code above. But this is no
* problem, we just create and inserts an empty PelIfd object.
*/
println('No IFD found, adding new.');
$ifd0 = new PelIfd(PelIfd::IFD0);
$tiff->setIfd($ifd0);
}
/*
* Each entry in an IFD is identified with a tag. This will load the
* ImageDescription entry if it is present. If the IFD does not
* contain such an entry, null will be returned.
*/
$desc = $ifd0->getEntry(PelTag::IMAGE_DESCRIPTION);
/* We need to check if the image already had a description stored. */
if ($desc == null) {
/* The was no description in the image. */
println('Added new IMAGE_DESCRIPTION entry with "%s".', $description);
/*
* In this case we simply create a new PelEntryAscii object to hold
* the description. The constructor for PelEntryAscii needs to know
* the tag and contents of the new entry.
*/
$desc = new PelEntryAscii(PelTag::IMAGE_DESCRIPTION, $description);
/*
* This will insert the newly created entry with the description
* into the IFD.
*/
$ifd0->addEntry($desc);
} else {
/* An old description was found in the image. */
println('Updating IMAGE_DESCRIPTION entry from "%s" to "%s".', $desc->getValue(), $description);
/* The description is simply updated with the new description. */
$desc->setValue($description);
}
/*
* At this point the image on disk has not been changed, it is only
* the object structure in memory which represent the image which has
* been altered. This structure can be converted into a string of
* bytes with the getBytes method, and saving this in the output file
* completes the script.
*/
println('Writing file "%s".', $output);
$file->saveFile($output);

View File

@@ -0,0 +1,210 @@
<?php
/**
* PEL: PHP Exif Library.
* A library with support for reading and
* writing all Exif headers in JPEG and TIFF images using PHP.
*
* Copyright (C) 2007 Martin Geisler.
*
* 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 in the file COPYING; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/*
* Contributed by Andac Aydin (aandac@gmx.de).
* This example shows how one can add GPS information to a JPEG image.
* Any Exif information in the image will be overwritten by the new
* information.
* This example includes two functions:
* - convertDecimalTpDMS() converts decimal GPS coordinates (how you use them
* in Google Maps for example) to the conventional coordinate-system
* used in Exif data.
* - addGpsInfo() adds several Exif tags to your JPEG file.
*/
use lsolesen\pel\PelEntryAscii;
use lsolesen\pel\PelEntryByte;
use lsolesen\pel\PelEntryRational;
use lsolesen\pel\PelEntryUserComment;
use lsolesen\pel\PelExif;
use lsolesen\pel\PelIfd;
use lsolesen\pel\PelJpeg;
use lsolesen\pel\PelTag;
use lsolesen\pel\PelTiff;
/**
* Convert a decimal degree into degrees, minutes, and seconds.
*
* @param
* int the degree in the form 123.456. Must be in the interval
* [-180, 180].
* @return array a triple with the degrees, minutes, and seconds. Each
* value is an array itself, suitable for passing to a
* PelEntryRational. If the degree is outside the allowed interval,
* null is returned instead.
*/
function convertDecimalToDMS($degree)
{
if ($degree > 180 || $degree < - 180) {
return null;
}
$degree = abs($degree); // make sure number is positive
// (no distinction here for N/S
// or W/E).
$seconds = $degree * 3600; // Total number of seconds.
$degrees = floor($degree); // Number of whole degrees.
$seconds -= $degrees * 3600; // Subtract the number of seconds
// taken by the degrees.
$minutes = floor($seconds / 60); // Number of whole minutes.
$seconds -= $minutes * 60; // Subtract the number of seconds
// taken by the minutes.
$seconds = round($seconds * 100, 0); // Round seconds with a 1/100th
// second precision.
return [
[
$degrees,
1
],
[
$minutes,
1
],
[
$seconds,
100
]
];
}
/**
* Add GPS information to an image basic metadata.
* Any old Exif data
* is discarded.
*
* @param string $input
* the input filename.
* @param string $output
* the output filename. An updated copy of the input
* image is saved here.
* @param string $description
* image description.
* @param string $comment
* user comment.
* @param string $model
* camera model.
* @param float $longitude
* expressed as a fractional number of degrees,
* e.g. 12.345<EFBFBD>. Negative values denotes degrees west of Greenwich.
* @param float $latitude
* expressed as for longitude. Negative values
* denote degrees south of equator.
* @param float $date_time
* the altitude, negative values express an altitude
* below sea level.
* @param
* string the date and time.
*/
function addGpsInfo($input, $output, $description, $comment, $model, $longitude, $latitude, $altitude, $date_time)
{
/* Load the given image into a PelJpeg object */
$jpeg = new PelJpeg($input);
/*
* Create and add empty Exif data to the image (this throws away any
* old Exif data in the image).
*/
$exif = new PelExif();
$jpeg->setExif($exif);
/*
* Create and add TIFF data to the Exif data (Exif data is actually
* stored in a TIFF format).
*/
$tiff = new PelTiff();
$exif->setTiff($tiff);
/*
* Create first Image File Directory and associate it with the TIFF
* data.
*/
$ifd0 = new PelIfd(PelIfd::IFD0);
$tiff->setIfd($ifd0);
/*
* Create a sub-IFD for holding GPS information. GPS data must be
* below the first IFD.
*/
$gps_ifd = new PelIfd(PelIfd::GPS);
$ifd0->addSubIfd($gps_ifd);
/*
* The USER_COMMENT tag must be put in a Exif sub-IFD under the
* first IFD.
*/
$exif_ifd = new PelIfd(PelIfd::EXIF);
$exif_ifd->addEntry(new PelEntryUserComment($comment));
$ifd0->addSubIfd($exif_ifd);
$inter_ifd = new PelIfd(PelIfd::INTEROPERABILITY);
$ifd0->addSubIfd($inter_ifd);
$ifd0->addEntry(new PelEntryAscii(PelTag::MODEL, $model));
$ifd0->addEntry(new PelEntryAscii(PelTag::DATE_TIME, $date_time));
$ifd0->addEntry(new PelEntryAscii(PelTag::IMAGE_DESCRIPTION, $description));
$gps_ifd->addEntry(new PelEntryByte(PelTag::GPS_VERSION_ID, 2, 2, 0, 0));
/*
* Use the convertDecimalToDMS function to convert the latitude from
* something like 12.34<EFBFBD> to 12<EFBFBD> 20' 42"
*/
list ($hours, $minutes, $seconds) = convertDecimalToDMS($latitude);
/* We interpret a negative latitude as being south. */
$latitude_ref = ($latitude < 0) ? 'S' : 'N';
$gps_ifd->addEntry(new PelEntryAscii(PelTag::GPS_LATITUDE_REF, $latitude_ref));
$gps_ifd->addEntry(new PelEntryRational(PelTag::GPS_LATITUDE, $hours, $minutes, $seconds));
/* The longitude works like the latitude. */
list ($hours, $minutes, $seconds) = convertDecimalToDMS($longitude);
$longitude_ref = ($longitude < 0) ? 'W' : 'E';
$gps_ifd->addEntry(new PelEntryAscii(PelTag::GPS_LONGITUDE_REF, $longitude_ref));
$gps_ifd->addEntry(new PelEntryRational(PelTag::GPS_LONGITUDE, $hours, $minutes, $seconds));
/*
* Add the altitude. The absolute value is stored here, the sign is
* stored in the GPS_ALTITUDE_REF tag below.
*/
$gps_ifd->addEntry(new PelEntryRational(PelTag::GPS_ALTITUDE, [
abs($altitude),
1
]));
/*
* The reference is set to 1 (true) if the altitude is below sea
* level, or 0 (false) otherwise.
*/
$gps_ifd->addEntry(new PelEntryByte(PelTag::GPS_ALTITUDE_REF, (int) ($altitude < 0)));
/* Finally we store the data in the output file. */
file_put_contents($output, $jpeg->getBytes());
}

View File

@@ -0,0 +1,117 @@
#!/usr/bin/php
<?php
/**
* PEL: PHP Exif Library.
* A library with support for reading and
* writing all Exif headers in JPEG and TIFF images using PHP.
*
* Copyright (C) 2004, 2005, 2006 Martin Geisler.
*
* 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 in the file COPYING; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/*
* An example of how PEL can be used. The script will read the Exif
* timestamps in the files given, and rename the files based on that.
* Should there be several files with the same timestamp --- the
* resolution of the Exif timestamp is one second, so a burst of
* images will typically have identical timestamps --- then the next
* available time will be used.
*/
/* a printf() variant that appends a newline to the output. */
use lsolesen\pel\Pel;
use lsolesen\pel\PelDataWindow;
use lsolesen\pel\PelJpeg;
use lsolesen\pel\PelTag;
use lsolesen\pel\PelTiff;
function println($args)
{
$args = func_get_args();
$fmt = array_shift($args);
vprintf($fmt . "\n", $args);
}
/* Make PEL speak the users language, if it is available. */
setlocale(LC_ALL, '');
$prog = array_shift($argv);
$error = false;
/* Accept the optional -d command line argument. */
if (isset($argv[0]) && $argv[0] == '-d') {
Pel::setDebug(true);
array_shift($argv);
}
/*
* Print usage information if there are no more command line
* arguments.
*/
if (empty($argv)) {
println('Usage: %s [-d] <file> ...', $prog);
println('Optional arguments:');
println(' -d turn debug output on.');
println('Mandatory arguments:');
println(' file ... one or more file names.');
println();
println('The files will be renamed based on their Exif timestamp.');
exit(1);
}
/*
* We typically need lots of RAM to parse TIFF images since they tend
* to be big and uncompressed.
*/
ini_set('memory_limit', '32M');
foreach ($argv as $file) {
println('Reading file "%s".', $file);
$data = new PelDataWindow(file_get_contents($file));
if (PelJpeg::isValid($data)) {
$jpeg = new PelJpeg();
$jpeg->load($data);
$app1 = $jpeg->getExif();
$tiff = $app1->getTiff();
} elseif (PelTiff::isValid($data)) {
$tiff = new PelTiff($data);
} else {
println('Unrecognized image format! Skipping.');
continue;
}
$ifd0 = $tiff->getIfd();
$entry = $ifd0->getEntry(PelTag::DATE_TIME);
if ($entry == null) {
println('Skipping %s because no DATE_TIME tag was found.', $file);
continue;
}
$time = $entry->getValue();
do {
$new = gmdate('Y:m:d-H:i:s', $time) . strchr($file, '.');
println('Trying file name %s', $new);
$time ++;
} while (file_exists($new));
rename($file, $new);
}

View File

@@ -0,0 +1,139 @@
#!/usr/bin/php
<?php
/**
* PEL: PHP Exif Library.
* A library with support for reading and
* writing all Exif headers in JPEG and TIFF images using PHP.
*
* Copyright (C) 2006, 2007 Martin Geisler.
*
* 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 in the file COPYING; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
namespace Pel\examples;
use lsolesen\pel\Pel;
use lsolesen\pel\PelJpeg;
/**
* a printf() variant that appends a newline to the output.
*/
function println($args)
{
$args = func_get_args();
$fmt = array_shift($args);
vprintf($fmt . "\n", $args);
}
/* Make PEL speak the users language, if it is available. */
setlocale(LC_ALL, '');
$argv = $_SERVER['argv'];
/*
* Store the name of the script in $prog and remove this first part of
* the command line.
*/
$prog = array_shift($argv);
$error = false;
/*
* The next argument could be -d to signal debug mode where lots of
* extra information is printed out when the image is parsed.
*/
if (isset($argv[0]) && $argv[0] == '-d') {
Pel::setDebug(true);
array_shift($argv);
}
/* The mandatory input filename. */
if (isset($argv[0])) {
$input = array_shift($argv);
} else {
$error = true;
}
/* The mandatory output filename. */
if (isset($argv[0])) {
$output = array_shift($argv);
} else {
$error = true;
}
/* The mandatory scale factor. */
if (isset($argv[0])) {
$scale = array_shift($argv);
} else {
$error = true;
}
/*
* Usage information is printed if an error was found in the command
* line arguments.
*/
if ($error) {
println('Usage: %s [-d] <input> <output> <scale>', $prog);
println('Optional arguments:');
println(' -d turn debug output on.');
println('Mandatory arguments:');
println(' input the input filename, a JPEG image.');
println(' output filename for saving the changed image.');
println(' scale scale factor, say 0.5 to resize to half the ' . 'original size.');
exit(1);
}
/* The input file is now loaded into a PelJpeg object. */
println('Reading file "%s".', $input);
$input_jpeg = new PelJpeg($input);
/*
* The input image is already loaded, so we can reuse the bytes stored
* in $input_jpeg when creating the Image resource.
*/
$original = ImageCreateFromString($input_jpeg->getBytes());
if ($original === false) {
throw new \Exception('Can\'t create image from string');
}
$original_w = ImagesX($original);
$original_h = ImagesY($original);
$scaled_w = $original_w * $scale;
$scaled_h = $original_h * $scale;
/* Now create the scaled image. */
$scaled = ImageCreateTrueColor($scaled_w, $scaled_h);
if ($original === false) {
throw new \Exception('Can\'t create true color image from scaled resources');
}
ImageCopyResampled($scaled, $original, 0, 0, 0, 0, $scaled_w, $scaled_h, $original_w, $original_h);
/*
* We want the raw JPEG data from $scaled. Luckily, one can create a
* PelJpeg object from an image resource directly:
*/
$output_jpeg = new PelJpeg($scaled);
/* Retrieve the original Exif data in $jpeg (if any). */
$exif = $input_jpeg->getExif();
/* If no Exif data was present, then $exif is null. */
if ($exif != null) {
$output_jpeg->setExif($exif);
}
/* We can now save the scaled image. */
println('Writing file "%s".', $output);
$output_jpeg->saveFile($output);