Below is a file iterarot unction I have working on that iterates over folders and gathers all images, it saves each image’s file path to a $files array and then later on the code has to iterate over that $files array
Right now the code I am working on actually sens a file through imagemagick 3 times ( I didn’t write the original code) so I am trying to improve the whole code, So far I have cut that down to 2 times and when this is finished it will be only 1 time for each image.
Basically the current code runs an image through imagemagick to see it it is an image or not, later on it does it again but this time to get the image extension, and then the 3rd time was to make sure it was an image again (I know pretty crazy that people write code that bad)
So to reduce it to 1 time, when the $files array runs an image file through imagemagick the first time to make sure it is an image, I now have that function returning the file extension instead of a simple true/false. So when the filepath is saved to the array I would like to save the returned file extension to the array with that particular file path so that I can access it later without running imagemagick again.
So here is the function that cycle thorough folders grabbing all image files and then sends them to imagemagick. It then saves the filepath to the array, can someone show me how to save the file extension to this array and then how to access it in the code below the function
function find_recursive_images($path)
{
$directory = new RecursiveDirectoryIterator($path,RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new RecursiveIteratorIterator($directory,RecursiveIteratorIterator::LEAVES_ONLY);
$exntensions = array("png", "jpg", "jpeg", "gif", "bmp");
foreach ($iterator as $fileinfo) {
if (in_array($fileinfo->getExtension(), $exntensions)) {
//This gets the filetype of the image from ImageMagick
$img = self::get_type($fileinfo->getPathname());
if ($img) {
//This sets the full file path to the $files array
$files[] = $fileinfo->getPathname();
// I tried something like this but not correct
$files[][] = $img;
}
}
}
return $files;
}
I then access the array like this right now…
foreach ($files as $file)
{
// ie E:\Server\_ImageOptimize\img\testfiles\css3-please.png
$source_file = $file;
// $file also needs to return a filetype that is got when it ran thorugh imagemagick
}
How about you use an associative array for each image? try this:
To build your array:
$files[] = array(location => $fileinfo->getPathname(), type => $img);To loop through your aray: