Skip to content

Commit

Permalink
Merge pull request #1 from ycs77/analysis-QMmAkp
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
ycs77 committed Nov 29, 2020
2 parents e8141e6 + dc4095e commit ee2eb15
Show file tree
Hide file tree
Showing 27 changed files with 793 additions and 789 deletions.
25 changes: 14 additions & 11 deletions src/Format/JPEG.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

namespace Ycs77\ImageMetadata\Format;

use Ycs77\ImageMetadata\Image;
use Ycs77\ImageMetadata\Metadata\Exif;
use Ycs77\ImageMetadata\Metadata\Iptc;
use Ycs77\ImageMetadata\Metadata\Xmp;
use Ycs77\ImageMetadata\Image;

/**
* @author Daniel Chesterton <daniel@chestertondevelopment.com>
Expand Down Expand Up @@ -66,6 +67,7 @@ private function getSegmentsByName($name)
public function setXmp(Xmp $xmp)
{
$this->xmp = $xmp;

return $this;
}

Expand Down Expand Up @@ -101,7 +103,7 @@ public function save($filename = null)
$handle = @fopen($filename, 'wb');

// Check if the file opened successfully
if (!$handle) {
if (! $handle) {
throw new \Exception(sprintf('Could not open file %s', $filename));
}

Expand All @@ -124,8 +126,8 @@ private function write($handle)

// write each segment
foreach ($this->segments as $segment) {
$segmentContent = sprintf("\xFF%c", $segment->getType()); // marker
$segmentContent .= pack("n", strlen($segment->getData()) + 2); // size
$segmentContent = sprintf("\xFF%c", $segment->getType()); // marker
$segmentContent .= pack('n', strlen($segment->getData()) + 2); // size
$segmentContent .= $segment->getData();

fwrite($handle, $segmentContent);
Expand Down Expand Up @@ -180,6 +182,7 @@ public static function fromString($string)
public static function fromImagick(\Imagick $imagick)
{
$imagick->setImageFormat('jpg');

return self::fromString($imagick->getImageBlob());
}

Expand Down Expand Up @@ -215,7 +218,7 @@ public static function fromStream($fileHandle, $filename = null)
$imageData = null;

// Cycle through the file until, either an EOI (End of image) marker is hit or end of file is hit
while (($data[1] != "\xD9") && (!feof($fileHandle))) {
while (($data[1] != "\xD9") && (! feof($fileHandle))) {
// Found a segment to look at.
// Check that the segment marker is not a restart marker, restart markers don't have size or data
if ((ord($data[1]) < 0xD0) || (ord($data[1]) > 0xD7)) {
Expand All @@ -234,7 +237,7 @@ public static function fromStream($fileHandle, $filename = null)
$compressedData = '';
do {
$compressedData .= fread($fileHandle, 1048576);
} while (!feof($fileHandle));
} while (! feof($fileHandle));

// Strip off EOI and anything after
$eoiPos = strpos($compressedData, "\xFF\xD9");
Expand All @@ -253,7 +256,6 @@ public static function fromStream($fileHandle, $filename = null)
}

return new self($imageData, $segments, $filename);

} finally {
fclose($fileHandle);
}
Expand All @@ -273,7 +275,7 @@ public static function fromFile($filename)
{
$fileHandle = @fopen($filename, 'rb');

if (!$fileHandle) {
if (! $fileHandle) {
throw new \Exception(sprintf('Could not open file %s', $filename));
}

Expand All @@ -287,18 +289,19 @@ private function insertXmpSegment()
{
$xmp = $this->getXmp();

if (!$xmp) {
if (! $xmp) {
return;
}

$renderSegment = function (Xmp $xmp) {
return "http://ns.adobe.com/xap/1.0/\x00" . $xmp->getString();
return "http://ns.adobe.com/xap/1.0/\x00".$xmp->getString();
};

foreach ($this->getSegmentsByName('APP1') as $segment) {
// And if it has the Adobe XMP/RDF label (http://ns.adobe.com/xap/1.0/\x00) ,
if (strncmp($segment->getData(), "http://ns.adobe.com/xap/1.0/\x00", 29) == 0) {
$segment->setData($renderSegment($xmp));

return;
}
}
Expand All @@ -322,7 +325,7 @@ private function insertXmpSegment()
*/
public function getXmp()
{
if (!$this->xmp) {
if (! $this->xmp) {
$possible = $this->getSegmentsByName('APP1');
$xmpData = null;

Expand Down
186 changes: 95 additions & 91 deletions src/Format/JPEG/Segment.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Ycs77\ImageMetadata\Format\JPEG;

/**
Expand All @@ -7,110 +8,109 @@
class Segment
{
private static $segmentNames = [
0xC0 => "SOF0", 0xC1 => "SOF1", 0xC2 => "SOF2", 0xC3 => "SOF4",
0xC5 => "SOF5", 0xC6 => "SOF6", 0xC7 => "SOF7", 0xC8 => "JPG",
0xC9 => "SOF9", 0xCA => "SOF10", 0xCB => "SOF11", 0xCD => "SOF13",
0xCE => "SOF14", 0xCF => "SOF15",
0xC4 => "DHT", 0xCC => "DAC",
0xC0 => 'SOF0', 0xC1 => 'SOF1', 0xC2 => 'SOF2', 0xC3 => 'SOF4',
0xC5 => 'SOF5', 0xC6 => 'SOF6', 0xC7 => 'SOF7', 0xC8 => 'JPG',
0xC9 => 'SOF9', 0xCA => 'SOF10', 0xCB => 'SOF11', 0xCD => 'SOF13',
0xCE => 'SOF14', 0xCF => 'SOF15',
0xC4 => 'DHT', 0xCC => 'DAC',

0xD0 => "RST0", 0xD1 => "RST1", 0xD2 => "RST2", 0xD3 => "RST3",
0xD4 => "RST4", 0xD5 => "RST5", 0xD6 => "RST6", 0xD7 => "RST7",
0xD0 => 'RST0', 0xD1 => 'RST1', 0xD2 => 'RST2', 0xD3 => 'RST3',
0xD4 => 'RST4', 0xD5 => 'RST5', 0xD6 => 'RST6', 0xD7 => 'RST7',

0xD8 => "SOI", 0xD9 => "EOI", 0xDA => "SOS", 0xDB => "DQT",
0xDC => "DNL", 0xDD => "DRI", 0xDE => "DHP", 0xDF => "EXP",
0xD8 => 'SOI', 0xD9 => 'EOI', 0xDA => 'SOS', 0xDB => 'DQT',
0xDC => 'DNL', 0xDD => 'DRI', 0xDE => 'DHP', 0xDF => 'EXP',

0xE0 => "APP0", 0xE1 => "APP1", 0xE2 => "APP2", 0xE3 => "APP3",
0xE4 => "APP4", 0xE5 => "APP5", 0xE6 => "APP6", 0xE7 => "APP7",
0xE8 => "APP8", 0xE9 => "APP9", 0xEA => "APP10", 0xEB => "APP11",
0xEC => "APP12", 0xED => "APP13", 0xEE => "APP14", 0xEF => "APP15",
0xE0 => 'APP0', 0xE1 => 'APP1', 0xE2 => 'APP2', 0xE3 => 'APP3',
0xE4 => 'APP4', 0xE5 => 'APP5', 0xE6 => 'APP6', 0xE7 => 'APP7',
0xE8 => 'APP8', 0xE9 => 'APP9', 0xEA => 'APP10', 0xEB => 'APP11',
0xEC => 'APP12', 0xED => 'APP13', 0xEE => 'APP14', 0xEF => 'APP15',

0xF0 => "JPG0", 0xF1 => "JPG1", 0xF2 => "JPG2", 0xF3 => "JPG3",
0xF4 => "JPG4", 0xF5 => "JPG5", 0xF6 => "JPG6", 0xF7 => "JPG7",
0xF8 => "JPG8", 0xF9 => "JPG9", 0xFA => "JPG10", 0xFB => "JPG11",
0xFC => "JPG12", 0xFD => "JPG13",
0xF0 => 'JPG0', 0xF1 => 'JPG1', 0xF2 => 'JPG2', 0xF3 => 'JPG3',
0xF4 => 'JPG4', 0xF5 => 'JPG5', 0xF6 => 'JPG6', 0xF7 => 'JPG7',
0xF8 => 'JPG8', 0xF9 => 'JPG9', 0xFA => 'JPG10', 0xFB => 'JPG11',
0xFC => 'JPG12', 0xFD => 'JPG13',

0xFE => "COM", 0x01 => "TEM", 0x02 => "RES"
0xFE => 'COM', 0x01 => 'TEM', 0x02 => 'RES',
];

private static $segmentDescriptions = [
0xC0 => "Start Of Frame (SOF) Huffman - Baseline DCT",
0xC1 => "Start Of Frame (SOF) Huffman - Extended sequential DCT",
0xC2 => "Start Of Frame Huffman - Progressive DCT (SOF2)",
0xC3 => "Start Of Frame Huffman - Spatial (sequential) lossless (SOF3)",
0xC5 => "Start Of Frame Huffman - Differential sequential DCT (SOF5)",
0xC6 => "Start Of Frame Huffman - Differential progressive DCT (SOF6)",
0xC7 => "Start Of Frame Huffman - Differential spatial (SOF7)",
0xC8 => "Start Of Frame Arithmetic - Reserved for JPEG extensions (JPG)",
0xC9 => "Start Of Frame Arithmetic - Extended sequential DCT (SOF9)",
0xCA => "Start Of Frame Arithmetic - Progressive DCT (SOF10)",
0xCB => "Start Of Frame Arithmetic - Spatial (sequential) lossless (SOF11)",
0xCD => "Start Of Frame Arithmetic - Differential sequential DCT (SOF13)",
0xCE => "Start Of Frame Arithmetic - Differential progressive DCT (SOF14)",
0xCF => "Start Of Frame Arithmetic - Differential spatial (SOF15)",
0xC4 => "Define Huffman Table(s) (DHT)",
0xCC => "Define Arithmetic coding conditioning(s) (DAC)",

0xD0 => "Restart with modulo 8 count 0 (RST0)",
0xD1 => "Restart with modulo 8 count 1 (RST1)",
0xD2 => "Restart with modulo 8 count 2 (RST2)",
0xD3 => "Restart with modulo 8 count 3 (RST3)",
0xD4 => "Restart with modulo 8 count 4 (RST4)",
0xD5 => "Restart with modulo 8 count 5 (RST5)",
0xD6 => "Restart with modulo 8 count 6 (RST6)",
0xD7 => "Restart with modulo 8 count 7 (RST7)",

0xD8 => "Start of Image (SOI)",
0xD9 => "End of Image (EOI)",
0xDA => "Start of Scan (SOS)",
0xDB => "Define quantization Table(s) (DQT)",
0xDC => "Define Number of Lines (DNL)",
0xDD => "Define Restart Interval (DRI)",
0xDE => "Define Hierarchical progression (DHP)",
0xDF => "Expand Reference Component(s) (EXP)",

0xE0 => "Application Field 0 (APP0) - usually JFIF or JFXX",
0xE1 => "Application Field 1 (APP1) - usually EXIF or XMP/RDF",
0xE2 => "Application Field 2 (APP2) - usually Flashpix",
0xE3 => "Application Field 3 (APP3)",
0xE4 => "Application Field 4 (APP4)",
0xE5 => "Application Field 5 (APP5)",
0xE6 => "Application Field 6 (APP6)",
0xE7 => "Application Field 7 (APP7)",

0xE8 => "Application Field 8 (APP8)",
0xE9 => "Application Field 9 (APP9)",
0xEA => "Application Field 10 (APP10)",
0xEB => "Application Field 11 (APP11)",
0xEC => "Application Field 12 (APP12) - usually [picture info]",
0xED => "Application Field 13 (APP13) - usually photoshop IRB / IPTC",
0xEE => "Application Field 14 (APP14)",
0xEF => "Application Field 15 (APP15)",

0xF0 => "Reserved for JPEG extensions (JPG0)",
0xF1 => "Reserved for JPEG extensions (JPG1)",
0xF2 => "Reserved for JPEG extensions (JPG2)",
0xF3 => "Reserved for JPEG extensions (JPG3)",
0xF4 => "Reserved for JPEG extensions (JPG4)",
0xF5 => "Reserved for JPEG extensions (JPG5)",
0xF6 => "Reserved for JPEG extensions (JPG6)",
0xF7 => "Reserved for JPEG extensions (JPG7)",
0xF8 => "Reserved for JPEG extensions (JPG8)",
0xF9 => "Reserved for JPEG extensions (JPG9)",
0xFA => "Reserved for JPEG extensions (JPG10)",
0xFB => "Reserved for JPEG extensions (JPG11)",
0xFC => "Reserved for JPEG extensions (JPG12)",
0xFD => "Reserved for JPEG extensions (JPG13)",

0xFE => "Comment (COM)",
0x01 => "For temp private use arith code (TEM)",
0x02 => "Reserved (RES)"
0xC0 => 'Start Of Frame (SOF) Huffman - Baseline DCT',
0xC1 => 'Start Of Frame (SOF) Huffman - Extended sequential DCT',
0xC2 => 'Start Of Frame Huffman - Progressive DCT (SOF2)',
0xC3 => 'Start Of Frame Huffman - Spatial (sequential) lossless (SOF3)',
0xC5 => 'Start Of Frame Huffman - Differential sequential DCT (SOF5)',
0xC6 => 'Start Of Frame Huffman - Differential progressive DCT (SOF6)',
0xC7 => 'Start Of Frame Huffman - Differential spatial (SOF7)',
0xC8 => 'Start Of Frame Arithmetic - Reserved for JPEG extensions (JPG)',
0xC9 => 'Start Of Frame Arithmetic - Extended sequential DCT (SOF9)',
0xCA => 'Start Of Frame Arithmetic - Progressive DCT (SOF10)',
0xCB => 'Start Of Frame Arithmetic - Spatial (sequential) lossless (SOF11)',
0xCD => 'Start Of Frame Arithmetic - Differential sequential DCT (SOF13)',
0xCE => 'Start Of Frame Arithmetic - Differential progressive DCT (SOF14)',
0xCF => 'Start Of Frame Arithmetic - Differential spatial (SOF15)',
0xC4 => 'Define Huffman Table(s) (DHT)',
0xCC => 'Define Arithmetic coding conditioning(s) (DAC)',

0xD0 => 'Restart with modulo 8 count 0 (RST0)',
0xD1 => 'Restart with modulo 8 count 1 (RST1)',
0xD2 => 'Restart with modulo 8 count 2 (RST2)',
0xD3 => 'Restart with modulo 8 count 3 (RST3)',
0xD4 => 'Restart with modulo 8 count 4 (RST4)',
0xD5 => 'Restart with modulo 8 count 5 (RST5)',
0xD6 => 'Restart with modulo 8 count 6 (RST6)',
0xD7 => 'Restart with modulo 8 count 7 (RST7)',

0xD8 => 'Start of Image (SOI)',
0xD9 => 'End of Image (EOI)',
0xDA => 'Start of Scan (SOS)',
0xDB => 'Define quantization Table(s) (DQT)',
0xDC => 'Define Number of Lines (DNL)',
0xDD => 'Define Restart Interval (DRI)',
0xDE => 'Define Hierarchical progression (DHP)',
0xDF => 'Expand Reference Component(s) (EXP)',

0xE0 => 'Application Field 0 (APP0) - usually JFIF or JFXX',
0xE1 => 'Application Field 1 (APP1) - usually EXIF or XMP/RDF',
0xE2 => 'Application Field 2 (APP2) - usually Flashpix',
0xE3 => 'Application Field 3 (APP3)',
0xE4 => 'Application Field 4 (APP4)',
0xE5 => 'Application Field 5 (APP5)',
0xE6 => 'Application Field 6 (APP6)',
0xE7 => 'Application Field 7 (APP7)',

0xE8 => 'Application Field 8 (APP8)',
0xE9 => 'Application Field 9 (APP9)',
0xEA => 'Application Field 10 (APP10)',
0xEB => 'Application Field 11 (APP11)',
0xEC => 'Application Field 12 (APP12) - usually [picture info]',
0xED => 'Application Field 13 (APP13) - usually photoshop IRB / IPTC',
0xEE => 'Application Field 14 (APP14)',
0xEF => 'Application Field 15 (APP15)',

0xF0 => 'Reserved for JPEG extensions (JPG0)',
0xF1 => 'Reserved for JPEG extensions (JPG1)',
0xF2 => 'Reserved for JPEG extensions (JPG2)',
0xF3 => 'Reserved for JPEG extensions (JPG3)',
0xF4 => 'Reserved for JPEG extensions (JPG4)',
0xF5 => 'Reserved for JPEG extensions (JPG5)',
0xF6 => 'Reserved for JPEG extensions (JPG6)',
0xF7 => 'Reserved for JPEG extensions (JPG7)',
0xF8 => 'Reserved for JPEG extensions (JPG8)',
0xF9 => 'Reserved for JPEG extensions (JPG9)',
0xFA => 'Reserved for JPEG extensions (JPG10)',
0xFB => 'Reserved for JPEG extensions (JPG11)',
0xFC => 'Reserved for JPEG extensions (JPG12)',
0xFD => 'Reserved for JPEG extensions (JPG13)',

0xFE => 'Comment (COM)',
0x01 => 'For temp private use arith code (TEM)',
0x02 => 'Reserved (RES)',
];

private $data;
private $start;
private $type;


public function __construct($type, $start, $data)
{
$this->type = $type;
Expand Down Expand Up @@ -138,6 +138,7 @@ public function getData()
public function setData($data)
{
$this->data = $data;

return $this;
}

Expand All @@ -161,6 +162,7 @@ public function getStart()
public function setStart($start)
{
$this->start = $start;

return $this;
}

Expand All @@ -184,6 +186,7 @@ public function getType()
public function setType($type)
{
$this->type = $type;

return $this;
}

Expand All @@ -197,6 +200,7 @@ public function getName()
if (isset(self::$segmentNames[$this->type])) {
return self::$segmentNames[$this->type];
}

return '';
}

Expand All @@ -210,11 +214,11 @@ public function getDescription()
if (isset(self::$segmentDescriptions[$this->type])) {
return self::$segmentDescriptions[$this->type];
}

return '';
}

public function isXmpSegment()
{

}
}
7 changes: 4 additions & 3 deletions src/Format/PNG.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace Ycs77\ImageMetadata\Format;

use Ycs77\ImageMetadata\Image;
use Ycs77\ImageMetadata\Metadata\UnsupportedException;
use Ycs77\ImageMetadata\Metadata\Xmp;
use Ycs77\ImageMetadata\Image;

/**
* @author Daniel Chesterton <daniel@chestertondevelopment.com>
Expand Down Expand Up @@ -55,7 +56,7 @@ public function __construct($contents, $filename = null)
public function getBytes()
{
if ($this->xmp && ($this->xmp->hasChanges() || $this->hasNewXmp)) {
$data = "XML:com.adobe.xmp\x00\x00\x00\x00\x00" . $this->xmp->getString();
$data = "XML:com.adobe.xmp\x00\x00\x00\x00\x00".$this->xmp->getString();

$xmpChunk = $this->getXmpChunk();

Expand Down Expand Up @@ -85,7 +86,7 @@ public function getBytes()
*/
public function getXmp()
{
if (!$this->xmp) {
if (! $this->xmp) {
$xmpChunk = $this->getXmpChunk();

if ($xmpChunk) {
Expand Down

0 comments on commit ee2eb15

Please sign in to comment.