Skip to content

Instantly share code, notes, and snippets.

@iiic
Created July 28, 2017 13:12
Show Gist options
  • Save iiic/214407d7e5c185b59b95d20483757e24 to your computer and use it in GitHub Desktop.
Save iiic/214407d7e5c185b59b95d20483757e24 to your computer and use it in GitHub Desktop.
Nastavení IPTC atributů pro obrázky JPEG s využitím setterů
<?php
class Iptc {
private $meta;
const OBJECT_NAME = '2#005';
const EDIT_STATUS = '2#007';
const PRIORITY = '2#010';
const CATEGORY = '2#015';
const SUPPLEMENTAL_CATEGORY = '2#020';
const FIXTURE_IDENTIFIER = '2#022';
const KEYWORDS = '2#025';
const RELEASE_DATE = '2#030';
const RELEASE_TIME = '2#035';
const SPECIAL_INSTRUCTIONS = '2#040';
const REFERENCE_SERVICE = '2#045';
const REFERENCE_DATE = '2#047';
const REFERENCE_NUMBER = '2#050';
const CREATED_DATE = '2#055';
const CREATED_TIME = '2#060';
const ORIGINATING_PROGRAM = '2#065';
const PROGRAM_VERSION = '2#070';
const OBJECT_CYCLE = '2#075';
const BYLINE = '2#080';
const BYLINE_TITLE = '2#085';
const CITY = '2#090';
const PROVINCE_STATE = '2#095';
const COUNTRY_CODE = '2#100';
const COUNTRY = '2#101';
const ORIGINAL_TRANSMISSION_REFERENCE = '2#103';
const HEADLINE = '2#105';
const CREDIT = '2#110';
const SOURCE = '2#115';
const COPYRIGHT_STRING = '2#116';
const CAPTION = '2#120';
const LOCAL_CAPTION = '2#121';
function __construct(array $info = array()) {
$this->meta = isset($info["APP13"]) ? iptcparse($info["APP13"]) : array();
}
function __set($name, $data) {
switch ($name) {
case 'copyright' :
$this->meta[self::COPYRIGHT_STRING] = array($data);
return;
case 'created' :
$this->meta[self::CREATED_DATE] = array($data->format('Y-m-d'));
$this->meta[self::CREATED_TIME] = array($data->format('H:i:s'));
return;
case 'title' :
$this->meta[self::HEADLINE] = array($data);
$this->meta[self::CAPTION] = array($data);
$this->meta[self::BYLINE_TITLE] = array($data);
return;
case 'originating' :
$this->meta[self::ORIGINATING_PROGRAM] = array($data);
return;
case 'keywords' :
$this->meta[self::KEYWORDS] = array(implode(',', $data));
return;
}
throw new \Exception(__CLASS__ . ": unknown meta '$name'");
}
function __get($name) {
switch ($name) {
case 'copyright' :
return $this->meta[self::COPYRIGHT_STRING][0];
case 'created' :
return $this->meta[self::CREATED_DATE][0] . ' ' . $this->meta[self::CREATED_TIME][0];
case 'title' :
return $this->meta[self::HEADLINE][0];
case 'originating' :
return $this->meta[self::ORIGINATING_PROGRAM][0];
case 'keywords' :
return $this->meta[self::KEYWORDS][0];
}
return '';
}
function dump() {
foreach($this->meta as $key => $value) {
echo "$key: $value[0]" . PHP_EOL;
}
}
function __toString() {
$utf8seq = chr(0x1b) . chr(0x25) . chr(0x47); //zapnuti podpory UTF8 v IPCT
$iptc_new = $this->maketag(1, '090', $utf8seq);
foreach ($this->meta as $s => $value) {
$tag = str_replace("2#", "", $s);
$iptc_new .= $this->maketag(2, $tag, substr($value[0], 0, 128));
}
return $iptc_new;
}
function maketag($type, $tag, $content) {
$len = strlen($content);
$length = (($len < 0x8000) ? "" :
chr(0x80).chr(0x04) . chr(($len >> 24) & 0xff). chr(($len >> 16) & 0xff)) .
chr(($len >> 8) & 0xff) . chr(($len) & 0xff);
return chr(0x1c) . chr($type) . chr((int) $tag) . $length . $content;
}
function clean() {
$this->meta = array();
}
}
<?php
include "Iptc.php";
$filename = 'mesic.jpg';
$image = imagecreatefromjpeg($oldfilename);
imageinterlace($image, true);
imagejpeg($image, $filename, 80);
getimagesize($filename, $info);
$iptc = new Iptc($info);
$iptc->originating = "PHP";
$iptc->copyright = "MyGoodShop s.r.o., all rights reserved";
$iptc->created = new \DateTime();
$iptc->title = "název produktu na obrázku";
$iptc->keywords = array('key1', 'key2', 'key3');
$mode = 0;
$image = iptcembed((string) $iptc, $filename, $mode);
file_put_contents($filename, $image);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment