From 4ed64983dcdae0b79be61edfc276521106b4d229 Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Sat, 30 Apr 2022 15:51:57 +0200 Subject: [PATCH] 0.0.1 --- .editorconfig | 24 +++++++++ .gitattributes | 51 ++++++++++++++++++ .gitignore | 46 ++++++++++++++++ LICENSE | 21 ++++++++ README.md | 25 +++++++++ composer.json | 22 ++++++++ src/SimpleIPTC.php | 105 ++++++++++++++++++++++++++++++++++++ src/SimpleIPTCException.php | 86 +++++++++++++++++++++++++++++ 8 files changed, 380 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/SimpleIPTC.php create mode 100644 src/SimpleIPTCException.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..f9d7962 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,24 @@ +; This file is for unifying the coding style for different editors and IDEs. +; More information at http://editorconfig.org + +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.bat] +end_of_line = crlf + +[*.yml] +indent_style = space +indent_size = 2 + +[*.twig] +insert_final_newline = false + +[Makefile] +indent_style = tab diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..cc578ba --- /dev/null +++ b/.gitattributes @@ -0,0 +1,51 @@ +# Define the line ending behavior of the different file extensions +# Set default behavior, in case users don't have core.autocrlf set. +* text=auto +* text eol=lf + +# Explicitly declare text files we want to always be normalized and converted +# to native line endings on checkout. +*.php text +*.default text +*.ctp text +*.sql text +*.md text +*.po text +*.js text +*.css text +*.ini text +*.properties text +*.txt text +*.xml text +*.svg text +*.yml text +.htaccess text + +# Declare files that will always have CRLF line endings on checkout. +*.bat eol=crlf + +# Declare files that will always have LF line endings on checkout. +*.pem eol=lf + +# Denote all files that are truly binary and should not be modified. +*.dbf binary +*.prj binary +*.shp binary +*.shx binary +*.sld binary +*.png binary +*.jpg binary +*.gif binary +*.ico binary +*.mo binary +*.pdf binary +*.phar binary +*.woff binary +*.woff2 binary +*.ttf binary +*.otf binary +*.eot binary +*.gz binary +*.bz2 binary +*.7z binary +*.zip binary diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..733e365 --- /dev/null +++ b/.gitignore @@ -0,0 +1,46 @@ +# CakePHP specific files # +########################## +/config/app.php +/config/.env +/logs/* +/tmp/* +/vendor/* +/webroot/files/* + +# OS generated files # +###################### +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +Icon? +ehthumbs.db +Thumbs.db +*.zip + +# Tool specific files # +####################### +/.vscode +/composer.lock +/.phpdoc +# vim +*~ +*.swp +*.swo +# sublime text & textmate +*.sublime-* +*.stTheme.cache +*.tmlanguage.cache +*.tmPreferences.cache +# Eclipse +.settings/* +# JetBrains, aka PHPStorm, IntelliJ IDEA +.idea/* +# NetBeans +nbproject/* +# Visual Studio Code +.vscode +# Sass preprocessor +.sass-cache/ +data diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..861453e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Fawno + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e6a1ca6 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +[![GitHub license](https://img.shields.io/github/license/fawno/SimpleIPTC)](https://github.com/fawno/SimpleIPTC/blob/master/LICENSE) +[![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/fawno/SimpleIPTC)](https://github.com/fawno/SimpleIPTC/tags) +[![GitHub release](https://img.shields.io/github/release/fawno/SimpleIPTC)](https://github.com/fawno/SimpleIPTC/releases) +[![Packagist](https://img.shields.io/packagist/v/fawno/simpleiptc)](https://packagist.org/packages/fawno/simpleiptc) +[![Packagist Downloads](https://img.shields.io/packagist/dt/fawno/simpleiptc)](https://packagist.org/packages/fawno/simpleiptc/stats) +[![GitHub issues](https://img.shields.io/github/issues/fawno/SimpleIPTC)](https://github.com/fawno/SimpleIPTC/issues) +[![GitHub forks](https://img.shields.io/github/forks/fawno/SimpleIPTC)](https://github.com/fawno/SimpleIPTC/network) +[![GitHub stars](https://img.shields.io/github/stars/fawno/SimpleIPTC)](https://github.com/fawno/SimpleIPTC/stargazers) + +# SimpleSVG + +Simple PHP class for IPTC documents + +## Instalation + +```sh +php composer.phar require "fawno/simpleiptc" +``` + +```php +=7.1.0" + }, + "autoload": { + "psr-4": { + "SimpleIPTC\\": "src/" + } + } +} diff --git a/src/SimpleIPTC.php b/src/SimpleIPTC.php new file mode 100644 index 0000000..62b50e6 --- /dev/null +++ b/src/SimpleIPTC.php @@ -0,0 +1,105 @@ + [ + 'name' => 'EnvelopeRecordVersion', + 'format' => 'n', // int16u big endian + ], + '1#020' => [ + 'name' => 'FileFormat', + 'format' => 'n', // int16u big endian + ], + '1#022' => [ + 'name' => 'FileVersion', + 'format' => 'n', // int16u big endian + ], + '2#000' => [ + 'name' => 'ApplicationRecordVersion', + 'format' => 'n', // int16u big endian + ], + '2#055' => [ + 'name' => 'DateCreated', + 'format' => 'a*', + ], + '2#243' => [ + 'name' => 'CustomData', + 'format' => 'a*', + ], + ]; + + /** + * @param string $bin + * @return array + */ + public static function bin2iptc (string $bin) : array { + $iptc = []; + + $fp = fopen('php://memory','rb+'); + fwrite ($fp, $bin); + rewind($fp); + + while ("\x1C" == fread($fp, 1)) { + $parts = unpack('Crecord/Cid/nlength', fread($fp, 4)); + + $tag = sprintf('%u#%03u', $parts['record'], $parts['id']); + $format = self::IPTC_TAGS[$tag]['format'] ?? 'a*'; + + if ($parts['length'] == 0x8004) { + $parts = unpack('Nlength', fread($fp, 4)); + } + + $iptc[] = [ + 'tag' => $tag, + 'name' => self::IPTC_TAGS[$tag]['name'] ?? null, + 'value' => current(unpack($format, fread($fp, $parts['length']))), + ]; + } + + fclose($fp); + + return $iptc; + } + + /** + * @param array $iptc + * @return string + */ + public static function iptc2bin (array $iptc) : string { + $data = ''; + + foreach ($iptc as $record) { + if (!empty($record['value'])) { + $tag = $record['tag'] ?? null; + $format = self::IPTC_TAGS[$tag]['format'] ?? 'a*'; + $value = pack($format, $record['value']); + $length = strlen($value); + + $data .= pack('C*', 0x1C, ...explode('#', $tag)); + $data .= ($length < 0x8000) ? pack('n', $length) : pack('nN', 0x8004, $length); + $data .= $value; + } else { + print_r($record); + } + } + + return $data; + } + } diff --git a/src/SimpleIPTCException.php b/src/SimpleIPTCException.php new file mode 100644 index 0000000..1c8170f --- /dev/null +++ b/src/SimpleIPTCException.php @@ -0,0 +1,86 @@ +severity = $severity; + + if (!is_null($filename)) { + $this->filename = $filename; + } + + if (!is_null($line)) { + $this->line = $line; + } + + parent::__construct($message, $code, $previous); + } + + /** + * Gets the exception severity + * + * @return int + * Returns the severity of the exception. + */ + public function getSeverity () : int { + return $this->severity; + } + }