I don't think your problem is easely solvable in php,
(with servlets it might be posible, if you would you a kind of thread manager , still this is not easy)
But I think you should rethink your concept.
Using GREP in a loop with Database queries, cannot be correct.
You should use your Database select statements to do string comparisons, this is an order of a magnitude faster.
If you don't want to "render all graphics after a big query",
maybe you should display a list of matched image names.
Then let the user select which images to render, and render these after a form submit.
As I said, I think you need to rethink your implementation and maybe part of your design.
Here in Germany we would say
(you are on the wrong track)