What's the best way to format this for readability?
if (strpos($file, '.jpg',1) && file_exists("$thumbsdir/$file") == false || strpos($file, '.gif',1) && file_exists("$thumbsdir/$file") == false || strpos($file, '.png',1) && file_exists("$thumbsdir/$file") == false) {
createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsdir/$file\n");
}
I'd extract the "is an image" logic into its own function, which makes the if
more readable and also allows you to centralize the logic.
function is_image($filename) {
$image_extensions = array('png', 'gif', 'jpg');
foreach ($image_extensions as $extension)
if (strrpos($filename, ".$extension") !== FALSE)
return true;
return false;
}
if (is_image($file) && !file_exists("$thumbsdir/$file")) {
createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsdir/$file\n");
}
if ((strpos($file, '.jpg',1) ||
strpos($file, '.gif',1) ||
strpos($file, '.png',1))
&& file_exists("$thumbsdir/$file") == false)
{
createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsdir/$file\n");
}
function check_thumbnail($file)
{
return (strpos($file, '.jpg',1) && file_exists("$thumbsdir/$file") == false ||
strpos($file, '.gif',1) && file_exists("$thumbsdir/$file") == false ||
strpos($file, '.png',1) && file_exists("$thumbsdir/$file") == false);
}
if (check_thumbnail ($file)) {
createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsdir/$file\n");
}
After extracting the logic to a separate function, you can reduce the duplication:
function check_thumbnail($file)
{
return (strpos($file, '.jpg',1) ||
strpos($file, '.gif',1) ||
strpos($file, '.png',1)) &&
(file_exists("$thumbsdir/$file") == false);
}
I would seperate the ifs as there is some repeating code in there. Also I try to exit a routine as early as possible:
if (!strpos($file, '.jpg',1) && !strpos($file, '.gif',1) && !strpos($file, '.png',1))
{
return;
}
if(file_exists("$thumbsdir/$file"))
{
return;
}
createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsdir/$file\n");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With