Exemple #1
1
Fichier : gps.php Projet : ni-c/pel
/**
 * Add GPS information to an image basic metadata. Any old Exif data
 * is discarded.
 *
 * @param string the input filename.
 *
 * @param string the output filename. An updated copy of the input
 * image is saved here.
 *
 * @param string image description.
 *
 * @param string user comment.
 *
 * @param string camera model.
 *
 * @param float longitude expressed as a fractional number of degrees,
 * e.g. 12.345°. Negative values denotes degrees west of Greenwich.
 *
 * @param float latitude expressed as for longitude. Negative values
 * denote degrees south of equator.
 *
 * @param float 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° to 12° 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, array(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());
}
 function testArrayAccess()
 {
     $ifd = new PelIfd(PelIfd::IFD0);
     $this->assertEqual(sizeof($ifd->getIterator()), 0);
     $desc = new PelEntryAscii(PelTag::IMAGE_DESCRIPTION, 'Hello?');
     $date = new PelEntryTime(PelTag::DATE_TIME, 12345678);
     $ifd[] = $desc;
     $ifd[] = $date;
     $this->assertIdentical($ifd[PelTag::IMAGE_DESCRIPTION], $desc);
     $this->assertIdentical($ifd[PelTag::DATE_TIME], $date);
     unset($ifd[PelTag::DATE_TIME]);
     $this->assertFalse(isset($ifd[PelTag::DATE_TIME]));
 }
Exemple #3
0
 /**
  * Return a string representation of this object.
  *
  * @return string a string describing this object. This is mostly useful
  *         for debugging.
  */
 public function __toString()
 {
     $str = Pel::fmt("Dumping TIFF data...\n");
     if ($this->ifd != null) {
         $str .= $this->ifd->__toString();
     }
     return $str;
 }
function addGPSdata($infile, $outfile, $GPS_lat, $GPS_lon, $GPS_alt)
{
    try {
        $image = new PelJpeg($infile);
    } catch (Exception $exc) {
        return -1;
    }
    if ($image instanceof PelJpeg) {
        if ($image->isValid(new PelDataWindow($image->getBytes()))) {
            $exif = $image->getExif();
            if ($exif == null) {
                $exif = new PelExif();
                $image->setExif($exif);
                $exif->setTiff(new PelTiff());
            }
            $tiff = $exif->getTiff();
            $ifd0 = $tiff->getIfd();
            if ($ifd0 == null) {
                $ifd0 = new PelIFD(PelIfd::IFD0);
                $tiff->setIfd($ifd0);
            }
            /* Tags erzeugen */
            $subifd = new PelIfd(PelIfd::GPS);
            $GPS_latref = $GPS_lat < 0 ? "S" : "N";
            $GPS_lonref = $GPS_lon < 0 ? "W" : "E";
            $GPS_altref = $GPS_alt < 0 ? 1 : 0;
            list($degrees, $minutes, $milliseconds) = dec2dms(abs($GPS_lat));
            $gpslat = new PelEntryRational(PelTag::GPS_LATITUDE, array($degrees, 1), array($minutes, 1), array($milliseconds, 1000));
            list($degrees, $minutes, $milliseconds) = dec2dms(abs($GPS_lon));
            $gpslon = new PelEntryRational(PelTag::GPS_LONGITUDE, array($degrees, 1), array($minutes, 1), array($milliseconds, 1000));
            echo $GPS_alt * 1000;
            $gpsalt = new PelEntryRational(PelTag::GPS_ALTITUDE, array(abs($GPS_alt * 1000), 1000));
            $gpslatref = new PelEntryAscii(PelTag::GPS_LATITUDE_REF, $GPS_latref);
            $gpslonref = new PelEntryAscii(PelTag::GPS_LONGITUDE_REF, $GPS_lonref);
            $gpsaltref = new PelEntryByte(PelTag::GPS_ALTITUDE_REF, $GPS_altref);
            $gpsversion = new PelEntryByte(PelTag::GPS_VERSION_ID, 2, 2, 0, 0);
            /* Daten eintragen.*/
            $subifd->addEntry($gpsversion);
            $subifd->addEntry($gpslat);
            $subifd->addEntry($gpslon);
            $subifd->addEntry($gpsalt);
            $subifd->addEntry($gpslatref);
            $subifd->addEntry($gpslonref);
            $subifd->addEntry($gpsaltref);
            $ifd0->addSubIfd($subifd);
            file_put_contents($outfile, $image->getBytes());
            return 0;
        }
    }
    return -1;
}
Exemple #5
0
function ifdToTest($name, $number, PelIfd $ifd)
{
    println();
    println('/* Start of IDF %s%d. */', $name, $number);
    $entries = $ifd->getEntries();
    println('$this->assertEqual(count(%s%d->getEntries()), %d);', $name, $number, count($entries));
    foreach ($entries as $tag => $entry) {
        println();
        println('$entry = %s%d->getEntry(%d); // %s', $name, $number, $tag, PelTag::getName($ifd->getType(), $tag));
        entryToTest('$entry', $entry);
    }
    println();
    println('/* Sub IFDs of %s%d. */', $name, $number);
    $sub_ifds = $ifd->getSubIfds();
    println('$this->assertEqual(count(%s%d->getSubIfds()), %d);', $name, $number, count($sub_ifds));
    $n = 0;
    $sub_name = $name . $number . '_';
    foreach ($sub_ifds as $type => $sub_ifd) {
        println('%s%d = %s%d->getSubIfd(%d); // IFD %s', $sub_name, $n, $name, $number, $type, $sub_ifd->getName());
        println('$this->assertIsA(%s%d, \'PelIfd\');', $sub_name, $n);
        ifdToTest($sub_name, $n, $sub_ifd);
        $n++;
    }
    println();
    if (strlen($ifd->getThumbnailData()) > 0) {
        println('$thumb_data = file_get_contents(dirname(__FILE__) .');
        println('                                \'/%s\');', $GLOBALS['thumb_filename']);
        println('$this->assertEqual(%s%d->getThumbnailData(), $thumb_data);', $name, $number);
    } else {
        println('$this->assertEqual(%s%d->getThumbnailData(), \'\');', $name, $number);
    }
    println();
    println('/* Next IFD. */');
    $next = $ifd->getNextIfd();
    println('%s%d = %s%d->getNextIfd();', $name, $number + 1, $name, $number);
    if ($next instanceof PelIfd) {
        println('$this->assertIsA(%s%d, \'PelIfd\');', $name, $number + 1);
        println('/* End of IFD %s%d. */', $name, $number);
        ifdToTest($name, $number + 1, $next);
    } else {
        println('$this->assertNull(%s%d);', $name, $number + 1);
        println('/* End of IFD %s%d. */', $name, $number);
    }
}
Exemple #6
0
 function testWriteRead()
 {
     Pel::setStrictParsing(true);
     $ifd = new PelIfd(PelIfd::IFD0);
     $this->assertTrue($ifd->isLastIfd());
     foreach ($this->entries as $entry) {
         $ifd->addEntry($entry);
     }
     $tiff = new PelTiff();
     $this->assertNull($tiff->getIfd());
     $tiff->setIfd($ifd);
     $this->assertNotNull($tiff->getIfd());
     $exif = new PelExif();
     $this->assertNull($exif->getTiff());
     $exif->setTiff($tiff);
     $this->assertNotNull($exif->getTiff());
     $jpeg = new PelJpeg(dirname(__FILE__) . '/no-exif.jpg');
     $this->assertNull($jpeg->getExif());
     $jpeg->setExif($exif);
     $this->assertNotNull($jpeg->getExif());
     $jpeg->saveFile('test-output.jpg');
     $this->assertTrue(file_exists('test-output.jpg'));
     $this->assertTrue(filesize('test-output.jpg') > 0);
     /* Now read the file and see if the entries are still there. */
     $jpeg = new PelJpeg('test-output.jpg');
     $exif = $jpeg->getExif();
     $this->assertIsA($exif, 'PelExif');
     $tiff = $exif->getTiff();
     $this->assertIsA($tiff, 'PelTiff');
     $ifd = $tiff->getIfd();
     $this->assertIsA($ifd, 'PelIfd');
     $this->assertEqual($ifd->getType(), PelIfd::IFD0);
     $this->assertTrue($ifd->isLastIfd());
     foreach ($this->entries as $entry) {
         $this->assertEqual($ifd->getEntry($entry->getTag())->getValue(), $entry->getValue());
     }
     unlink('test-output.jpg');
 }
Exemple #7
0
 /**
  * Turn this directory into text.
  *
  * @return string information about the directory, mainly for
  * debugging.
  */
 function __toString()
 {
     $str = Pel::fmt("Dumping IFD %s with %d entries...\n", $this->getName(), count($this->entries));
     foreach ($this->entries as $entry) {
         $str .= $entry->__toString();
     }
     $str .= Pel::fmt("Dumping %d sub IFDs...\n", count($this->sub));
     foreach ($this->sub as $type => $ifd) {
         $str .= $ifd->__toString();
     }
     if ($this->next != null) {
         $str .= $this->next->__toString();
     }
     return $str;
 }
Exemple #8
0
/**
 * Add GPS information to an image basic metadata. Any old Exif data
 * is discarded.
 *
 * @param string the input filename.
 *
 * @param string the output filename. An updated copy of the input
 * image is saved here.
 *
 * @param string image description.
 *
 * @param string user comment.
 *
 * @param string camera model.
 *
 * @param float longitude expressed as a fractional number of degrees,
 * e.g. 12.345°. Negative values denotes degrees west of Greenwich.
 *
 * @param float latitude expressed as for longitude. Negative values
 * denote degrees south of equator.
 *
 * @param float the altitude, negative values express an altitude
 * below sea level.
 *
 * @param string the date and time.
 */
function addGpsInfo($input, $description, $comment, $artist, $make, $model, $longitude, $latitude)
{
    /* 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::ARTIST, $artist));
    $ifd0->addEntry(new PelEntryAscii(PelTag::MAKE, $make));
    $ifd0->addEntry(new PelEntryAscii(PelTag::MODEL, $model));
    $ifd0->addEntry(new PelEntryAscii(PelTag::IMAGE_DESCRIPTION, $description));
    $gps_ifd->addEntry(new PelEntryByte(PelTag::GPS_VERSION_ID, 2, 2, 0, 0));
    // Negative numbers indicate different reference then the usual N/E
    if ($longitude < 0) {
        $longitude_ref = 'W';
    } else {
        $longitude_ref = 'E';
    }
    if ($latitude < 0) {
        $latitude_ref = 'S';
    } else {
        $latitude_ref = 'N';
    }
    list($h, $m, $s) = convertDecimalToDMS($latitude);
    $gps_ifd->addEntry(new PelEntryAscii(PelTag::GPS_LATITUDE_REF, $latitude_ref));
    $gps_ifd->addEntry(new PelEntryRational(PelTag::GPS_LATITUDE, $h, $m, $s));
    list($h, $m, $s) = convertDecimalToDMS($longitude);
    $gps_ifd->addEntry(new PelEntryAscii(PelTag::GPS_LONGITUDE_REF, $longitude_ref));
    $gps_ifd->addEntry(new PelEntryRational(PelTag::GPS_LONGITUDE, $h, $m, $s));
    $gps_ifd->addEntry(new PelEntryRational(PelTag::GPS_ALTITUDE, array(0, 0)));
    $gps_ifd->addEntry(new PelEntryByte(PelTag::GPS_ALTITUDE_REF, 0));
    //print($gps_ifd);
    file_put_contents($input, $jpeg->getBytes());
}