Creating thumbnails from URLs using PHP

I want to create website thumbnails. I found several sites that process it using the API, for example http://www.websnapr.com/

How can this be done with PHP so that I can handle all requests on my server?

+3
source share
4 answers

PHP cannot do this on its own because it does not include an HTML rendering library.

You can find an external method for capturing screenshots and exchange data with this method using PHP.

First you need a system designed for screenshots. Look at IECapt (http://iecapt.sourceforge.net/), CutyCapt (http://cutycapt.sourceforge.net/) or khtml2png (http://khtml2png.sourceforge.net/) and configure one of them on the system .

PHP script, exec() , , .

:

<?php
$in_url = 'http://' . $_REQUEST['url']; // !!INSECURE!! In production, make sure to sanitize this input!
$filename = '/var/cutycapt/images/' . $_REQUEST['url'] . '.png'; // Will probably need to normalize filename too, this is just an illustration

// First check the file does not exist, if it does exist skip generation and reuse the file
// This is a super simple caching system that will help to reduce the resource requirements
if(!file_exists($filename)) {
  exec('/usr/local/bin/CutyCapt --url="' . $_REQUEST['url'] . '" --out="' . $filename . '"');
}

// Second check if the file exists, either from a previous run or from the above generation routine
if(file_exists($filename)) {
  header('Content-type: image/png');
  print file_get_contents($filename);
} else {
  header('Status: 500 Internal Server Error');
}
?>

script :

http://localhost/screenshot.php?url=www.google.com

, - (.. , - ), , .

+3

, . , , .

( ), Xvfb - / /.

Linux:

khtml2png.sourceforge.net

mysql-apache-php.com/website_screenshot.htm

cutycapt.sourceforge.net

www.unruhdesigns.com/news/2010/10/using-firefox-on-a-headless-server-to-make-screenshots-of-websites

:

iecapt.sourceforge.net

:

www.paulhammond.org/webkit2png/

EDIT: - rackspace, , , , webfaction.

+1

, php . .

0

, , , - .

, , Linux-, Xvfb, firefox import.

You can also check this question.

0
source

Source: https://habr.com/ru/post/1788850/


All Articles