示例#1
0
文件: GH16Test.php 项目: lsolesen/pel
 function testThisDoesNotWorkAsExpected()
 {
     $subject = "Превед, медвед!";
     $data = new PelDataWindow(file_get_contents($this->file));
     if (PelJpeg::isValid($data)) {
         $jpeg = new PelJpeg();
         $jpeg->load($data);
         $exif = $jpeg->getExif();
         if (null === $exif) {
             $exif = new PelExif();
             $jpeg->setExif($exif);
             $tiff = new PelTiff();
             $exif->setTiff($tiff);
         }
         $tiff = $exif->getTiff();
         $ifd0 = $tiff->getIfd();
         if (null === $ifd0) {
             $ifd0 = new PelIfd(PelIfd::IFD0);
             $tiff->setIfd($ifd0);
         }
     }
     $ifd0->addEntry(new PelEntryWindowsString(PelTag::XP_SUBJECT, $subject));
     file_put_contents($this->file, $jpeg->getBytes());
     $jpeg = new PelJpeg($this->file);
     $exif = $jpeg->getExif();
     $tiff = $exif->getTiff();
     $ifd0 = $tiff->getIfd();
     $written_subject = $ifd0->getEntry(PelTag::XP_SUBJECT);
     $this->assertEquals($subject, $written_subject->getValue());
 }
示例#2
0
 public function testRead()
 {
     Pel::clearExceptions();
     Pel::setStrictParsing(false);
     $jpeg = new PelJpeg(dirname(__FILE__) . '/no-exif.jpg');
     $exif = $jpeg->getExif();
     $this->assertNull($exif);
     $this->assertTrue(count(Pel::getExceptions()) == 0);
 }
示例#3
0
 private function saveToJpeg()
 {
     $jpegFile = new pel\PelJpeg($this->filePath);
     $exif = $jpegFile->getExif();
     $tiff = $exif->getTiff();
     $ifd0 = $tiff->getIfd();
     switch ($this->tag) {
         case 'Description':
             $ifd0->addEntry(new pel\PelEntryAscii(pel\PelTag::IMAGE_DESCRIPTION, $this->value));
             break;
         default:
             break;
     }
     $jpegFile->saveFile($this->filePath);
     $this->refreshDatabase();
     print 'save se povedlo';
     header("Refresh:5; url=index.php", true, 303);
 }
示例#4
0
 function testThisDoesNotWorkAsExpected()
 {
     $filename = dirname(__FILE__) . '/images/bug3017880.jpg';
     try {
         $exif = null;
         $success = 1;
         // return true by default, as this function may not resave the file, but it's still success
         $resave_file = 0;
         $jpeg = new PelJpeg($filename);
         // should all exif data on photo be cleared (gd and iu will always strip it anyway, so only
         // force strip if you know the image you're branding is an original)
         // $jpeg->clearExif();
         if ($exif === null) {
             $exif = new PelExif();
             $jpeg->setExif($exif);
             $tiff = new PelTiff();
             $exif->setTiff($tiff);
         }
         $tiff = $exif->getTiff();
         $ifd0 = $tiff->getIfd();
         if ($ifd0 == null) {
             $ifd0 = new PelIfd(PelIfd::IFD0);
             $tiff->setIfd($ifd0);
         }
         $software_name = 'Example V2';
         $software = $ifd0->getEntry(PelTag::SOFTWARE);
         if ($software == null) {
             $software = new PelEntryAscii(PelTag::SOFTWARE, $software_name);
             $ifd0->addEntry($software);
             $resave_file = 1;
             echo 'null';
         } else {
             $software->setValue($software_name);
             $resave_file = 1;
             echo 'update';
         }
         if ($resave_file == 1 && !file_put_contents($filename, $jpeg->getBytes())) {
             // if it was okay to resave the file, but it did not save correctly
             $success = 0;
         }
     } catch (Exception $e) {
         $this->fail('Test should not throw an exception');
     }
 }
示例#5
0
 function testThisDoesNotWorkAsExpected()
 {
     $tmpfile = dirname(__FILE__) . '/images/bug1730993_tmp.jpg';
     $bigfile = dirname(__FILE__) . '/images/bug1730993_large.jpg';
     // TODO: Should not throw exception
     return;
     try {
         require_once 'PelJpeg.php';
         $jpeg = new PelJpeg($tmpfile);
         // the error occurs here
         $exif = $jpeg->getExif();
         if ($exif != null) {
             $jpeg1 = new PelJpeg($bigfile);
             $jpeg1->setExif($exif);
             file_put_contents($bigfile, $jpeg1->getBytes());
         }
     } catch (Exception $e) {
         $this->fail('Test should not throw exception: ' . $e->getMessage());
     }
 }
示例#6
0
文件: GH21Test.php 项目: lsolesen/pel
 function testThisDoesNotWorkAsExpected()
 {
     $scale = 0.75;
     $input_jpeg = new PelJpeg($this->file);
     $original = ImageCreateFromString($input_jpeg->getBytes());
     $original_w = ImagesX($original);
     $original_h = ImagesY($original);
     $scaled_w = $original_w * $scale;
     $scaled_h = $original_h * $scale;
     $scaled = ImageCreateTrueColor($scaled_w, $scaled_h);
     ImageCopyResampled($scaled, $original, 0, 0, 0, 0, $scaled_w, $scaled_h, $original_w, $original_h);
     $output_jpeg = new PelJpeg($scaled);
     $exif = $input_jpeg->getExif();
     if ($exif !== null) {
         $output_jpeg->setExif($exif);
     }
     file_put_contents($this->file, $output_jpeg->getBytes());
     $jpeg = new PelJpeg($this->file);
     $exifin = $jpeg->getExif();
     $this->assertEquals($exif, $exifin);
 }
示例#7
0
文件: resize.php 项目: lsolesen/pel
    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());
$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);
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);
示例#8
0
文件: rename.php 项目: alamehor/pel
    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;
    }
示例#9
0
 * 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);
    /*
示例#10
0
 public function testRead()
 {
     Pel::clearExceptions();
     Pel::setStrictParsing(false);
     $jpeg = new PelJpeg(dirname(__FILE__) . '/nikon-coolscan-iv.jpg');
     $exif = $jpeg->getExif();
     $this->assertInstanceOf('lsolesen\\pel\\PelExif', $exif);
     $tiff = $exif->getTiff();
     $this->assertInstanceOf('lsolesen\\pel\\PelTiff', $tiff);
     /* The first IFD. */
     $ifd0 = $tiff->getIfd();
     $this->assertInstanceOf('lsolesen\\pel\\PelIfd', $ifd0);
     /* Start of IDF $ifd0. */
     $this->assertEquals(count($ifd0->getEntries()), 6);
     $entry = $ifd0->getEntry(271);
     // Make
     $this->assertInstanceOf('lsolesen\\pel\\PelEntryAscii', $entry);
     $this->assertEquals($entry->getValue(), 'Nikon');
     $this->assertEquals($entry->getText(), 'Nikon');
     $entry = $ifd0->getEntry(282);
     // XResolution
     $this->assertInstanceOf('lsolesen\\pel\\PelEntryRational', $entry);
     $this->assertEquals($entry->getValue(), array(0 => 2000, 1 => 1));
     $this->assertEquals($entry->getText(), '2000/1');
     $entry = $ifd0->getEntry(283);
     // YResolution
     $this->assertInstanceOf('lsolesen\\pel\\PelEntryRational', $entry);
     $this->assertEquals($entry->getValue(), array(0 => 2000, 1 => 1));
     $this->assertEquals($entry->getText(), '2000/1');
     $entry = $ifd0->getEntry(296);
     // ResolutionUnit
     $this->assertInstanceOf('lsolesen\\pel\\PelEntryShort', $entry);
     $this->assertEquals($entry->getValue(), 2);
     $this->assertEquals($entry->getText(), 'Inch');
     $entry = $ifd0->getEntry(306);
     // DateTime
     $this->assertInstanceOf('lsolesen\\pel\\PelEntryTime', $entry);
     $this->assertEquals($entry->getValue(), 1090023875);
     $this->assertEquals($entry->getText(), '2004:07:17 00:24:35');
     $entry = $ifd0->getEntry(531);
     // YCbCrPositioning
     $this->assertInstanceOf('lsolesen\\pel\\PelEntryShort', $entry);
     $this->assertEquals($entry->getValue(), 1);
     $this->assertEquals($entry->getText(), 'centered');
     /* Sub IFDs of $ifd0. */
     $this->assertEquals(count($ifd0->getSubIfds()), 2);
     $ifd0_0 = $ifd0->getSubIfd(2);
     // IFD Exif
     $this->assertInstanceOf('lsolesen\\pel\\PelIfd', $ifd0_0);
     /* Start of IDF $ifd0_0. */
     $this->assertEquals(count($ifd0_0->getEntries()), 7);
     $entry = $ifd0_0->getEntry(36864);
     // ExifVersion
     $this->assertInstanceOf('lsolesen\\pel\\PelEntryVersion', $entry);
     $this->assertEquals($entry->getValue(), 2.1);
     $this->assertEquals($entry->getText(), 'Exif Version 2.1');
     $entry = $ifd0_0->getEntry(37121);
     // ComponentsConfiguration
     $this->assertInstanceOf('lsolesen\\pel\\PelEntryUndefined', $entry);
     $this->assertEquals($entry->getValue(), "");
     $this->assertEquals($entry->getText(), 'Y Cb Cr -');
     $entry = $ifd0_0->getEntry(37500);
     // MakerNote
     $this->assertInstanceOf('lsolesen\\pel\\PelEntryUndefined', $entry);
     $expected = "0100";
     $this->assertEquals($entry->getValue(), $expected);
     $this->assertEquals($entry->getText(), '32 bytes unknown MakerNote data');
     $entry = $ifd0_0->getEntry(40960);
     // FlashPixVersion
     $this->assertInstanceOf('lsolesen\\pel\\PelEntryVersion', $entry);
     $this->assertEquals($entry->getValue(), 1);
     $this->assertEquals($entry->getText(), 'FlashPix Version 1.0');
     $entry = $ifd0_0->getEntry(40961);
     // ColorSpace
     $this->assertInstanceOf('lsolesen\\pel\\PelEntryShort', $entry);
     $this->assertEquals($entry->getValue(), 1);
     $this->assertEquals($entry->getText(), 'sRGB');
     $entry = $ifd0_0->getEntry(40962);
     // PixelXDimension
     $this->assertInstanceOf('lsolesen\\pel\\PelEntryLong', $entry);
     $this->assertEquals($entry->getValue(), 960);
     $this->assertEquals($entry->getText(), '960');
     $entry = $ifd0_0->getEntry(40963);
     // PixelYDimension
     $this->assertInstanceOf('lsolesen\\pel\\PelEntryLong', $entry);
     $this->assertEquals($entry->getValue(), 755);
     $this->assertEquals($entry->getText(), '755');
     /* Sub IFDs of $ifd0_0. */
     $this->assertEquals(count($ifd0_0->getSubIfds()), 0);
     $this->assertEquals($ifd0_0->getThumbnailData(), '');
     /* Next IFD. */
     $ifd0_1 = $ifd0_0->getNextIfd();
     $this->assertNull($ifd0_1);
     /* End of IFD $ifd0_0. */
     $ifd0_1 = $ifd0->getSubIfd(3);
     // IFD GPS
     $this->assertInstanceOf('lsolesen\\pel\\PelIfd', $ifd0_1);
     /* Start of IDF $ifd0_1. */
     $this->assertEquals(count($ifd0_1->getEntries()), 0);
     /* Sub IFDs of $ifd0_1. */
     $this->assertEquals(count($ifd0_1->getSubIfds()), 0);
     $this->assertEquals($ifd0_1->getThumbnailData(), '');
     /* Next IFD. */
     $ifd0_2 = $ifd0_1->getNextIfd();
     $this->assertNull($ifd0_2);
     /* End of IFD $ifd0_1. */
     $this->assertEquals($ifd0->getThumbnailData(), '');
     /* Next IFD. */
     $ifd1 = $ifd0->getNextIfd();
     $this->assertNull($ifd1);
     /* End of IFD $ifd0. */
     $this->assertTrue(count(Pel::getExceptions()) == 0);
 }
 /**
  * Strip orientation from EXIF data for an image at a path.
  *
  * @param $filePath
  *
  * @return bool
  */
 public function stripOrientationFromExifData($filePath)
 {
     if (!ImageHelper::canHaveExifData($filePath)) {
         return null;
     }
     $data = new PelDataWindow(IOHelper::getFileContents($filePath));
     // Is this a valid JPEG?
     if (PelJpeg::isValid($data)) {
         $jpeg = $file = new PelJpeg();
         $jpeg->load($data);
         $exif = $jpeg->getExif();
         if ($exif) {
             $tiff = $exif->getTiff();
             $ifd0 = $tiff->getIfd();
             // Delete the Orientation entry and re-save the file
             $ifd0->offsetUnset(PelTag::ORIENTATION);
             $file->saveFile($filePath);
             return true;
         }
     }
     return false;
 }
示例#12
0
 /**
  * Strip orientation from EXIF data for an image at a path.
  *
  * @param $filePath
  *
  * @return bool
  */
 public function stripOrientationFromExifData($filePath)
 {
     if (!ImageHelper::canHaveExifData($filePath)) {
         return null;
     }
     // Quick and dirty, if possible
     if ($this->isImagick() && method_exists('Imagick', 'setImageProperty')) {
         $image = new \Imagick($filePath);
         $image->setImageOrientation(\Imagick::ORIENTATION_UNDEFINED);
         $image->writeImages($filePath, true);
         return true;
     }
     $data = new PelDataWindow(IOHelper::getFileContents($filePath));
     // Is this a valid JPEG?
     if (PelJpeg::isValid($data)) {
         $jpeg = $file = new PelJpeg();
         $jpeg->load($data);
         $exif = $jpeg->getExif();
         if ($exif) {
             $tiff = $exif->getTiff();
             $ifd0 = $tiff->getIfd();
             // Delete the Orientation entry and re-save the file
             $ifd0->offsetUnset(PelTag::ORIENTATION);
             $file->saveFile($filePath);
             return true;
         }
     }
     return false;
 }
示例#13
0
文件: gps.php 项目: lsolesen/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());
}