0

So I have this local script generate.php that loops through some special folder structure of websites on my localhost and executes a phantomJS script using the PHP exec function to generate Thumbnails (1200x800) for these projects (These are just websites/php/html).

The problem is, that its really slow. I have about 18 different images to be generated and it takes from 30sec up to a minute for only those 18 which is not what I want. Is there an option to run those 18 execs concurrently with php or maybe there is a phantomJS script that can handle many images at once. Hope someone has an idea.

foreach(folder level 1)
   foreach(folder level 2)
      exec(phantomjs render image)
Laisender
  • 714
  • 1
  • 5
  • 10
  • Can you clarify, you want to create thumbnails of 1200x800 images, or create thumbnails sized 1200x800? If the former, why not just store a separate thumbnail for each image (create a missing thumbnail with PHP GD or ImageMagick), the latter doesn't make much sense ... – Teemu Apr 11 '22 at 09:44
  • @Teemu Just to clarify. These are 18 images (could get more) each of size 1200px/800px. There are no "Thumbnails". I just meant images/screenshots of the page in the end. – Laisender Apr 11 '22 at 09:54
  • Make a PHP tool which creates and saves the thumbnails (preferrably when an image is uploaded). Then you can use the thumbnails on the page with [srcset](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#using_the_srcset_attribute) attributes. To make the loading of the large images faster, see [Lazy loading](https://developer.mozilla.org/en-US/docs/Web/Performance/Lazy_loading). – Teemu Apr 11 '22 at 09:57
  • Hey thanks for your response but I think you dont understand my question. This is not about lazy loading or page speed problems. I am generating these screenshots with the headless browser PhantomJS. Phantom opens the webpage on the server, takes a screenshot of what he sees and then saves this screenshot to a local folder. The problem is that the generating takes really long and thats why I am searching for a solution to speed up phantomjs or make php run phantom in parallel processes instead of waiting for each and everyone to finish. – Laisender Apr 11 '22 at 10:08
  • I am not interested generating smaller thumbnails for speeding up the page. The images are always displayed at full resolution. – Laisender Apr 11 '22 at 10:11
  • You didn't say anything about a "screenshot" in your question. My comment still stands, though, you use the mentioned tricks when you load the page to Phantom.js (at least load the saved thumbnails instead of the full-size images). – Teemu Apr 11 '22 at 10:11
  • the nested for each loops could be replaced with a recursiveIterator which might be faster. Have you analysed the speed to generate a single screenshot for a statically declared folder/page? – Professor Abronsius Apr 11 '22 at 10:14
  • The websites that are loaded by PhantomJs don't contain any images. Rendering one Screenshot with Phantom is no problem, takes about 500ms to 1sec but the problem is that there are many of them and they all run in series, one after one. And thats why it takes so long. Each screenshot process contains a timeout because the pages need some time to fully render and execute Javascript. – Laisender Apr 11 '22 at 10:15
  • Perhaps [this thread](https://stackoverflow.com/questions/70855/how-can-one-use-multi-threading-in-php-applications) might be of interest with regards to `Is there an option to run those 18 execs concurrently with php` – Professor Abronsius Apr 11 '22 at 10:25

0 Answers0