Skip to content

Commit 745dc9d

Browse files
committed
update
1 parent 7cf876c commit 745dc9d

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
"name": "rafiki23/metadata-extractor",
33
"description": "A Laravel package for extracting IPTC and EXIF data from images",
44
"type": "library",
5+
"version": "1.0.5",
56
"authors": [
67
{
78
"name": "Rafiki23"
89
}
910
],
11+
"license": "MIT",
1012
"autoload": {
1113
"psr-4": {
1214
"Rafiki23\\MetadataExtractor\\": "src/"

src/IPTCExtractor.php

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class IPTCExtractor {
1616
'2#090' => 'city',
1717
'2#092' => 'sublocation',
1818
'2#095' => 'provinceState',
19+
'2#100' => 'countryISOcode',
1920
'2#101' => 'countryName',
2021
'2#103' => 'originalTransmissionReference',
2122
'2#105' => 'headline',
@@ -40,14 +41,90 @@ public static function extract($path) {
4041
$keywords = '';
4142
$keywordcount = count($iptc["2#025"]);
4243

43-
for ($i=0; $i < $keywordcount; $i++) {
44+
for ($i=0; $i < $keywordcount; $i++) {
4445
$keywords .= $iptc['2#025'][$i].', ';
4546
}
4647
$iptcData[$name] = rtrim($keywords,', ');
4748
}
49+
if (isset($iptc[$code]) && $code == '2#020') {
50+
$supplementalCategories = '';
51+
$catcount = count($iptc["2#020"]);
52+
53+
for ($i=0; $i < $catcount; $i++) {
54+
$supplementalCategories .= $iptc['2#020'][$i].', ';
55+
}
56+
$iptcData[$name] = rtrim($supplementalCategories,', ');
57+
}
4858
}
4959
}
5060
}
5161
return $iptcData;
5262
}
63+
64+
65+
/**
66+
* Zapisuje dane IPTC do obrazu.
67+
*
68+
* @param string $path Ścieżka do pliku obrazu.
69+
* @param array $data Dane IPTC do zapisania.
70+
* @return bool Wynik operacji zapisu.
71+
*/
72+
public static function saveIptcData($path, $data) {
73+
// Odczytanie istniejących danych IPTC
74+
$existingIptcData = Self::extract($path); // Pobranie istniejących tagów IPTC
75+
76+
// dd($existingIptcData);
77+
// Przygotowanie nowych danych IPTC do zapisu
78+
$iptcData = '';
79+
foreach (self::$iptcTags as $code => $name) {
80+
if (isset($data[$name])) {
81+
// Użyj nowej wartości
82+
$value = $data[$name];
83+
} elseif (isset($existingIptcData[$name])) {
84+
// Użyj istniejącej wartości
85+
$value = $existingIptcData[$name];
86+
} else {
87+
continue; // Jeśli brak zarówno nowej, jak i istniejącej wartości, kontynuuj
88+
}
89+
90+
$iptcData .= self::iptcMakeTag(substr($code, 0, 1), substr($code, 2), $value);
91+
}
92+
93+
//dd($iptcData);
94+
//dd($path);
95+
// Pobranie zawartości obrazu
96+
//$content = file_get_contents($path);
97+
//dd($content);
98+
// Zapisanie danych IPTC
99+
$content = iptcembed($iptcData, $path);
100+
101+
if ($content === false) {
102+
return false;
103+
}
104+
105+
// Zapisanie zmodyfikowanego obrazu
106+
return file_put_contents($path, $content) !== false;
107+
}
108+
109+
/**
110+
* Tworzy tag IPTC.
111+
*/
112+
private static function iptcMakeTag($rec, $data, $value) {
113+
$length = strlen($value);
114+
$retval = chr(0x1C) . chr($rec) . chr($data);
115+
116+
if ($length < 0x8000) {
117+
$retval .= chr($length >> 8) . chr($length & 0xFF);
118+
} else {
119+
$retval .= chr(0x80) .
120+
chr(0x04) .
121+
chr(($length >> 24) & 0xFF) .
122+
chr(($length >> 16) & 0xFF) .
123+
chr(($length >> 8) & 0xFF) .
124+
chr($length & 0xFF);
125+
}
126+
127+
return $retval . $value;
128+
}
129+
53130
}

src/MetadataExtractor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ public static function extractIPTC($path) {
77
return IPTCExtractor::extract($path);
88
}
99

10+
public static function saveIptcData($path, $data) {
11+
return IPTCExtractor::saveIptcData($path, $data);
12+
}
13+
1014
public static function extractEXIF($path) {
1115
return EXIFExtractor::extract($path);
1216
}

0 commit comments

Comments
 (0)