snapshot.class.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Insipid
  4. * Personal web-bookmark-system
  5. *
  6. * Copyright 2016-2020 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. private $_googlePageSpeed = 'https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=';
  35. private $_wkhtmltoimageOptions = '--load-error-handling ignore --quality 80 --quiet --width 1900';
  36. public function __constructor() {}
  37. /**
  38. * call given url with google PageSpeed API
  39. * to recieve image data
  40. *
  41. * @param String $url URL to take a thumbnail from
  42. * @return
  43. */
  44. public function doSnapshot($url,$filename) {
  45. $ret = false;
  46. if(!empty($url) && is_writable(dirname($filename))) {
  47. $theCall = Summoner::curlCall($this->_googlePageSpeed.urlencode($url).'&screenshot=true');
  48. if(!empty($theCall)) {
  49. $jsonData = json_decode($theCall,true);
  50. if(!empty($jsonData) && isset($jsonData['screenshot']['data'])) {
  51. $imageData = $jsonData['screenshot']['data'];
  52. $imageData = str_replace(['_', '-'], ['/', '+'], $imageData);
  53. $imageData = base64_decode($imageData);
  54. $ret = file_put_contents($filename, $imageData);
  55. }
  56. }
  57. }
  58. return $ret;
  59. }
  60. /**
  61. * use configired WKHTMLTOPDF_COMMAND to create a whole page screenshot
  62. * of the given link and store it locally
  63. *
  64. * @param String $url URL to take the screenshot from
  65. * @return
  66. */
  67. public function wholePageSnpashot($url,$filename) {
  68. $ret = false;
  69. require_once 'lib/shellcommand.class.php';
  70. if(!empty($url) && is_writable(dirname($filename))) {
  71. $cmd = WKHTMLTOPDF_COMMAND;
  72. $params = $this->_wkhtmltoimageOptions." ".$url." ".$filename;
  73. $command = new ShellCommand($cmd." ".$params);
  74. if ($command->execute()) {
  75. $ret = $command->getOutput();
  76. } else {
  77. error_log($command->getError());
  78. $ret = $command->getExitCode();
  79. }
  80. }
  81. return $ret;
  82. }
  83. }