simonmlewis wrote:So I can add something in getSrcSet to see if it is doing the whole process to create the new images.
If it is, then what? make it run the deletion from in there? Considering it has all the filename and directory there.
But if it is NOT using that, because the newly _475... images have been made, I could put it where..?
I recommended getSrcSet because you know that's going to be called frequently, perhaps removing the need for a one-off script or a cron job. Where in that function you place it is up to you. Could the thumbnail exist even if the full sized image doesn't? Then you can call it as soon as you have the thumbnail path.
Code: Select all
function getSrcSet($filename, $resize_type = 'wide')
{
$root_directory = dirname(__DIR__);
$paths = getPaths($resize_type);
$fullsize_directory = $root_directory . $paths['fullsize'];
$thumbnail_directory = $root_directory . $paths['thumbnail'];
// Cleanup any old thumbnails that may be lying around
if ($resize_type === 'thumbnails') {
$old_thumbnail = $thumbnail_directory . DIRECTORY_SEPARATOR . $filename;
if (file_exists($old_thumbnail)) {
unlink($old_thumbnail);
}
}
$images_path = str_replace(DIRECTORY_SEPARATOR, '/', $paths['thumbnail']);
$srcset = [];
$widths = getWidths($resize_type);
if (file_exists($fullsize_directory . DIRECTORY_SEPARATOR . $filename)) {
$basename = pathinfo($filename, PATHINFO_FILENAME);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
foreach ($widths as $size => $width) {
$fullname = $basename . '_' . $size . '.' . $extension;
if (file_exists($thumbnail_directory . DIRECTORY_SEPARATOR . $fullname)) {
$srcset[] = "{$images_path}/{$fullname} {$size}w";
} else {
resizeSingleImage($filename, $size, $width, $resize_type);
$srcset[] = "{$images_path}/{$fullname} {$size}w";
}
}
}
return implode(', ', $srcset);
}
If it can't, then call it once you have ascertained that the full sized image exists.
Code: Select all
function getSrcSet($filename, $resize_type = 'wide')
{
$root_directory = dirname(__DIR__);
$paths = getPaths($resize_type);
$fullsize_directory = $root_directory . $paths['fullsize'];
$thumbnail_directory = $root_directory . $paths['thumbnail'];
$images_path = str_replace(DIRECTORY_SEPARATOR, '/', $paths['thumbnail']);
$srcset = [];
$widths = getWidths($resize_type);
if (file_exists($fullsize_directory . DIRECTORY_SEPARATOR . $filename)) {
if ($resize_type === 'thumbnails') {
$old_thumbnail = $thumbnail_directory . DIRECTORY_SEPARATOR . $filename;
if (file_exists($old_thumbnail)) {
unlink($old_thumbnail);
}
}
$basename = pathinfo($filename, PATHINFO_FILENAME);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
foreach ($widths as $size => $width) {
$fullname = $basename . '_' . $size . '.' . $extension;
if (file_exists($thumbnail_directory . DIRECTORY_SEPARATOR . $fullname)) {
$srcset[] = "{$images_path}/{$fullname} {$size}w";
} else {
resizeSingleImage($filename, $size, $width, $resize_type);
$srcset[] = "{$images_path}/{$fullname} {$size}w";
}
}
}
return implode(', ', $srcset);
}
Finally, if you want to extract a separate function, you could do something like this
Code: Select all
function deleteFile($path)
{
if (is_file($path)) {
unlink($path);
}
}
function getSrcSet($filename, $resize_type = 'wide')
{
$root_directory = dirname(__DIR__);
$paths = getPaths($resize_type);
$fullsize_directory = $root_directory . $paths['fullsize'];
$thumbnail_directory = $root_directory . $paths['thumbnail'];
$images_path = str_replace(DIRECTORY_SEPARATOR, '/', $paths['thumbnail']);
$srcset = [];
$widths = getWidths($resize_type);
if (file_exists($fullsize_directory . DIRECTORY_SEPARATOR . $filename)) {
if ($resize_type === 'thumbnails') {
deleteFile($thumbnail_directory . DIRECTORY_SEPARATOR . $filename);
}
$basename = pathinfo($filename, PATHINFO_FILENAME);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
foreach ($widths as $size => $width) {
$fullname = $basename . '_' . $size . '.' . $extension;
if (file_exists($thumbnail_directory . DIRECTORY_SEPARATOR . $fullname)) {
$srcset[] = "{$images_path}/{$fullname} {$size}w";
} else {
resizeSingleImage($filename, $size, $width, $resize_type);
$srcset[] = "{$images_path}/{$fullname} {$size}w";
}
}
}
return implode(', ', $srcset);
}
Truth is it's all basically the same.