jcodyb
April 14, 2024, 8:10pm
1
I’m trying to use the kirby 4 resize on upload with this code in my image blueprint:
create:
width: 2000
height: 2000
But that is interfering with my config which was set to pull exif data after file creation:
'hooks' => [
'file.create:after' => function ($file) {
if ($file->type() == 'image') {
// Get date created from exif data
$camera = $file->exif()->camera()->model();
$iso = $file->exif()->iso();
$aperture = $file->exif()->aperture();
$user = kirby()->user()->username();
$userId = kirby()->user();
$date = date("Y-m-d");
function parseFocalLength($file)
{
$rawfocalLength = $file->exif()->focalLength();
if (is_null($rawfocalLength)) {
return '';
}
[$a, $b] = explode('/', $rawfocalLength);
if ($a && $b && $b !== 0) {
return $a/$b . 'mm';
}
return '';
};
$focalLength = parseFocalLength($file);
$file->update(array(
'camera' => $camera,
'iso' => $iso,
'aperture' => $aperture,
'focalLength' => $focalLength,
'Photographer' => $user,
'User-ID' => $userId,
'Date-uploaded' => $date,
// 'tags' => $keywords
));
}
}
],
Without the blueprint “create” the exif is pulled correctly. Is there a workaround for this or something I’m doing wrong?
jcodyb
June 4, 2024, 11:56pm
2
I did try
'file.create:before'
as well and that didn’t work-
jcodyb
June 4, 2024, 11:57pm
3
I have another image related question that maybe isn’t worth opening a new topic thread for, but is the exif data for “35mm equivalent focal length” supported? Can I make that the default when pulling $exif->focalLength()
?
Trying to replace the created file with a resized one doesn’t work either?
return [
'debug' => true,
'hooks' => [
'file.create:after' => function ($file) {
// Check if the uploaded file is an image
if (in_array($file->extension(), ['jpg', 'jpeg', 'png'])) {
// Extract EXIF data
$exifData = @exif_read_data($file->root());
// Initialize focal length variable
$focalLength = null;
// If EXIF data is available, extract focal length
if ($exifData) {
// Check for 35mm equivalent focal length
if (isset($exifData['FocalLengthIn35mmFilm'])) {
$focalLength = $exifData['FocalLengthIn35mmFilm'] . ' mm (35mm equivalent)';
} elseif (isset($exifData['FocalLength'])) {
// Fallback to regular focal length
$focalLength = $exifData['FocalLength'];
// Convert rational number to a human-readable format
if (strpos($focalLength, '/') !== false) {
list($numerator, $denominator) = explode('/', $focalLength);
if ($denominator != 0) {
$focalLength = ($numerator / $denominator) . ' mm';
}
}
}
}
// Get the current user and date
$user = kirby()->user()->username();
$userId = kirby()->user();
$date = date("Y-m-d");
// Update the file metadata with EXIF data, user, and date
$file->update([
'Photographer' => $user,
'User-ID' => $userId,
'Date-uploaded' => $date,
'focalLength' => $focalLength
]);
// Resize the image to 2000 pixels width
$resizedImage = $file->resize(2000);
// Replace the original file with the resized image
$file->replace($resizedImage->root());
}
}
]
];
You need to create a thumb like this instead:
kirby()->thumb($file->root(), $file->root(), [
'width' => 2000,
]);
Don’t use replace()
.
1 Like