snapshot.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Insipid
  4. * Personal web-bookmark-system
  5. *
  6. * Copyright 2016-2023 Johannes Keßler
  7. *
  8. * Development starting from 2011: Johannes Keßler
  9. * https://www.bananas-playground.net/projekt/insipid/
  10. *
  11. * creator:
  12. * Luke Reeves <luke@neuro-tech.net>
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.
  26. *
  27. */
  28. /**
  29. * class Snapshot
  30. * create from given ULR a Screenshot for storage
  31. * right now it uses google pagespeedonline.
  32. */
  33. class Snapshot {
  34. /**
  35. * @var string
  36. */
  37. private string $_googlePageSpeed = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=';
  38. /**
  39. * @var string
  40. */
  41. private string $_wkhtmltoimageOptions = '--load-error-handling ignore --quality 80 --quiet --width 1900';
  42. /**
  43. * Snapshot constructor
  44. */
  45. public function __constructor(): void {}
  46. /**
  47. * call given url with google PageSpeed API
  48. * to receive image data
  49. *
  50. * @param String $url URL to take a thumbnail from
  51. * @param string $filename
  52. * @return boolean
  53. */
  54. public function doSnapshot(string $url, string $filename): bool {
  55. $ret = false;
  56. if(!empty($url) && is_writable(dirname($filename))) {
  57. if(DEBUG) {
  58. Summoner::sysLog("[DEBUG] try to save to $filename with $this->_googlePageSpeed for $url");
  59. }
  60. $theCall = Summoner::curlCall($this->_googlePageSpeed.urlencode($url).'&screenshot=true');
  61. if(!empty($theCall)) {
  62. $jsonData = json_decode($theCall,true);
  63. if(DEBUG) {
  64. Summoner::sysLog("[DEBUG] Call result data: ".var_export($jsonData, true));
  65. }
  66. if(!empty($jsonData) && isset($jsonData['lighthouseResult']['audits']['full-page-screenshot']['details']['screenshot']['data'])) {
  67. $imageData = $jsonData['lighthouseResult']['audits']['full-page-screenshot']['details']['screenshot']['data'];
  68. $source = fopen($imageData, 'r');
  69. $destination = fopen($filename, 'w');
  70. if(stream_copy_to_stream($source, $destination)) {
  71. $ret = $filename;
  72. }
  73. fclose($source);
  74. fclose($destination);
  75. } elseif(DEBUG) {
  76. Summoner::sysLog("[DEBUG] invalid json data. Path ['lighthouseResult']['audits']['full-page-screenshot']['details']['screenshot']['data'] not found in : ".var_export($jsonData, true));
  77. }
  78. } elseif(DEBUG) {
  79. Summoner::sysLog("[DEBUG] curl call failed");
  80. }
  81. }
  82. return $ret;
  83. }
  84. /**
  85. * use configured COMPLETE_PAGE_SCREENSHOT_COMMAND to create a whole page screenshot
  86. * of the given link and store it locally
  87. *
  88. * @TODO: TBD
  89. *
  90. * @param String $url URL to take the screenshot from
  91. * @param string $filename
  92. * @return boolean
  93. */
  94. public function wholePageSnapshot(string $url, string $filename): bool {
  95. $ret = false;
  96. require_once 'lib/shellcommand.class.php';
  97. if(!empty($url) && is_writable(dirname($filename))) {
  98. $cmd = COMPLETE_PAGE_SCREENSHOT_COMMAND;
  99. $params = $this->_wkhtmltoimageOptions." ".$url." ".$filename;
  100. $command = new ShellCommand($cmd." ".$params);
  101. if ($command->execute()) {
  102. $ret = $command->getOutput();
  103. } else {
  104. Summoner::sysLog($command->getError());
  105. $ret = $command->getExitCode();
  106. }
  107. }
  108. return $ret;
  109. }
  110. }