'Php picture name pattern [closed]

Just wondering if one could help

I am using a script which requires image filenames to be in this format :

$config_pictureNamePattern = '%d%m%Y%H%i

and files it currently is looking for is in this format, day,month,year, hour :

image-0110-2017-0014.jpg

However, my camera is only able to send in this format :

20220218150001.jpg

How can i fix this ?

Matt

php


Solution 1:[1]

Details are needed to really answer. Are you asking how to change the expected format? Seems a good approach

Or, do you want to rename your file? If the later, then it sounds like you are asking about basic string manipulation. Here is an approach using two string functions.

<?php

$raw = '20220218150001.jpg';

$year = substr($raw, 0, 4);
$month = substr($raw, 4, 2);
$day = substr($raw, 6, 2);
$pictureNumber = substr($raw, 8, 10-strpos($raw, '.'));

$converted = "image-{$day}{$month}-{$year}-{$pictureNumber}.jpg";

echo $converted;

If you need time parts and they are not present in the filenmame you could leverage filesystem, pull from EXIF, or make it up.

Seeing your comment. You can see how that date format is used here: https://github.com/NikoleiTesla/tilacam/blob/master/php/picture.php in the parseName() function. Not super complicated. I'd play around with it and learn if you're interested. Best way.

Really about just these lines...

<?php
$config_pictureNamePattern = '%d%m%Y%H%i';
$filename = 'image-0110-2017-1629.jpg';

//Above is 'passed in'

//fix formatting by removing %'s
$config_pictureNamePattern = str_replace('%','',$config_pictureNamePattern);

//get just the number part of the file name.
$filename = basename($filename);
$res = preg_replace("/[^0-9]/", "", $filename);

//parse that string for a DateTime.
$dateTime = date_create_from_format($config_pictureNamePattern, $res);

$formatedDateTime = date_format($dateTime, 'Y-m-d H:i:s');

var_dump($formatedDateTime);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1