I have this custom plugin for extracting iptc metadata from images but since PHP 8.0 and kirby 3.9, I can’t get it to work. It was working under Kirby 3.7 and PHP 7.4 though.
I’ve updated to the latest kirby 3.9.6.1 and PHP 8.1 and I still can’t manage to make it worked.
<?php
function iptc($image) {
$size = getimagesize($image->url(), $info);
if(isset($info['APP13'])){
$iptc = iptcparse($info['APP13']);
// Get keywords
$keywords = '';
if (is_array($iptc["2#025"])) {
$keywordcount = count($iptc["2#025"]);
for ($i=0; $i < $keywordcount; $i++) {
$keywords .= $iptc['2#025'][$i].', ';
}
$keywords = rtrim($keywords,', ');
}
// Get caption
$caption = $iptc["2#120"][0];
// Get copyright
$copyright = $iptc["2#116"][0];
// Return $iptc array
$iptc = [
'keywords' => $keywords,
'caption' => $caption,
'copyright' => $copyright
];
return $iptc;
}
};
Kirby::plugin('nicolasrobel/iptc', [
'hooks' => [
'file.create:after' => function (Kirby\Cms\File $file) {
if ($file->type() == 'image') {
// Get date created from exif data
$datecreated = $file->exif()->timestamp();
// Get IPTC array via function
$iptc = iptc($file);
foreach (kirby()->languages() as $lang) {
// 'key' = file’s blueprint field to update
$file->update([
// 'date' => $datecreated,
'file_tags' => $iptc['keywords'],
'caption' => $iptc['caption'],
'credit' => $iptc['copyright']
], $lang->code());
}
}
}
]
]);
When I upload an image, I have this error:
Trying to access array offset on value of type null
Any clue?
Many thanks,
Nicolas
That’s very little info to go from - what line does the error even point to?
Mmmh… that’s weird but maybe it’s a clue… when I have an image with all the metadatas filled “description” + “copyrights” + “keywords”. It works and the metadata are extracted as expected. But if I drop an image without any metadatas, I have this error now?!:
www.buddycarrskateboards-6.jpeg
Undefined array key "2#025"
Does it help to understand where the error is coming from?
I don’t have any errors from the debug in Kirby, it’s a dialog box below:
In the console:
XHRPOST
https://efplauevq.preview.infomaniak.website/api/pages/news+books-msff/files
[HTTP/2 400 Bad Request 272ms]
POST
https://efplauevq.preview.infomaniak.website/api/pages/news+books-msff/files
Status
400
Bad Request
VersionHTTP/2
Transferred351 B (60 B size)
Referrer Policysame-origin
DNS ResolutionSystem
cache-control
no-store, private
content-encoding
gzip
content-type
application/json; charset=UTF-8
date
Wed, 16 Aug 2023 11:07:49 GMT
server
Apache
strict-transport-security
max-age=16000000
vary
Accept-Encoding
X-Firefox-Spdy
h2
Accept
*/*
Accept-Encoding
gzip, deflate, br
Accept-Language
en-US,en;q=0.5
Cache-Control
no-cache
Connection
keep-alive
Content-Length
57991
Content-Type
multipart/form-data; boundary=---------------------------14844929692940598925726614190
Cookie
kirby_session=0f0ab160f2c2206f7f9226a460e087bb8e0aebbc%2B1692187871.dfc60ce5ef27c0f14432.c5fdeaa72996337c5d90de34620d7f3901907fe8589cacead2ea842ef0b3effc
Host
efplauevq.preview.infomaniak.website
Origin
https://efplauevq.preview.infomaniak.website
Pragma
no-cache
Referer
https://efplauevq.preview.infomaniak.website/panel/pages/news+books-msff?language=fr
Sec-Fetch-Dest
empty
Sec-Fetch-Mode
cors
Sec-Fetch-Site
same-origin
TE
trailers
User-Agent
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/116.0
X-CSRF
b80570ce9fa408bcc74db239fb1273dd735d97dbb1af3296fc5a8840bc28fd75
Many thanks.
It looks like iptcparse($info['APP13'])
might return something different in PHP 8 than PHP 7.
The problem starts at is_array($iptc["2#025"])
where a file without exif data doesn’t have an entry with key 2#025
. So Php can’t even check if it’s an array cause it already fails trying to access this non-existing entry. You can avoid this by using ??
as fallback operator is_array($iptc["2#025"] ?? null)
- in that case if the entry doesn’t exist, it will check is_array(null)
which will be false.
You have a few more places there you try to access very specific keys. I would imagine each of these fails when adding a file without that metadata.
1 Like