PHP снимка на сайт

kikity_94

Registered
Заглавието е малко странно, но сега ще обесня какво искам да попитам.
Знаете ли как може да се направи снимка на сайт чрез php т.е. примерно добавям линк на някаф сайт в дб и кодът някак си да влезе в сайта да го снима и да ми покаце изображението му.
Както е тук в презевнтацийте
 
Настрайваш си пътищата в този файл ии размерите лесно е :
start_screenshot.php
PHP:
<?php

//set these variables
$url = 'http://www.google.bg'; //the URL you want to take a screen shot of
$image_filename = 'google.jpg'; //where you want to save the image (needs to be a jpg in this example)
$width = 1000; //in pixels
$height = 800; //in pixels
$screenshot_url = 'http://localhost/3/screenshot.php'; //the full address of the file we just created
$resized_width = 500; //if you want to shrink the image, set this to a value in pixels.  otherwise, set it to false

//now we connect to the screenshot script
$full_url = $screenshot_url.'?url='.urlencode($url).'&width='.$width.'&height='.$height.'&resized_width='.$resized_width;
$handle = fopen($full_url, "rb") or die("Error! Couldn't open URL."); //open in binary mode
$output = stream_get_contents($handle);
fclose($handle);
if (!$output) { die('Error! Output was not generated!'); }

//save the returned image
$fp = fopen($image_filename, 'w') or die('Error! Could not open image file!');
fwrite($fp, $output) or die('Error! Could not write image file!');
fclose($fp);

//finally, display your new image
echo '<p><img src="'.$image_filename.'" /><br />'.$image_filename.'</p>';

?>

screenshot.php
PHP:
<?php

//update these variables based on the dimensions of your local IE window:
$crop = true; //set this to false if you don't want image cropped
$crop_top = 97; //in pixels
$crop_right = 26; //pay attention to scrollbars
$crop_bottom = 49; //pay attention to scrollbars
$crop_left = 9;

//grab variables from the query string
$url = trim(strip_tags(urldecode($_GET['url'])));
if (!$url) { die('Error! No URL.'); }
$height = intval(@$_GET['height']);
if (!$height) { $height = 800; }
$width = intval(@$_GET['width']);
if (!$width) { $width = 1000; }
$resized_width = intval($_GET['resized_width']);

//open up a new Internet Explorer window and go to the specified URL
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->Width = $width;
$browser->Height = $height;
$browser->Navigate($url);

//wait for it to finish loading
while ($browser->Busy) {
    com_message_pump(4000);
}
sleep(5); //give it an extra 5 seconds to load images, videos, etc.

//take snapshot
$im = imagegrabwindow($handle);

//close down Internet Explorer
$browser->Quit();

//check to be sure the image exists
if (!$im) { die('Error! Image could not be created!'); }

//crop the image to get rid of menu, scrollbars, etc.
if ($crop) {
    $new_width = $width - $crop_left - $crop_right;
    if ($new_width < 1) { die('Error! Crop width cannot be less than 1 pixel!'); }
    $new_height = $height - $crop_top - $crop_bottom;
    if ($new_height < 1) { die('Error! Crop height cannot be less than 1 pixel!'); }
    $im2 = imagecreatetruecolor($new_width, $new_height);
    imagecopy($im2, $im, 0, 0, $crop_left, $crop_top, $new_width, $new_height) or die('Error! Could not crop image.');
    imagedestroy($im); //destroy the original
    $image = 'im2';
} else { $image = 'im'; }

//resize the image
if ($resized_width) {
    $new_width = $resized_width;
    $new_height = round(( $new_width / imagesx($$image) ) * imagesy($$image));
    $im3 = imagecreatetruecolor($new_width,$new_height);
    imagecopyresized ($im3, $$image, 0, 0, 0, 0, $new_width, $new_height, imagesx($$image), imagesy($$image)) or die('Error! Could not resize image.');
    imagedestroy($$image); //destroy the original
    $image = 'im3';
}

//display the image, then destroy it
header('Content-Type: image/jpeg');
imagejpeg($$image);
imagedestroy($$image);

?>
Файловете ти трябва да са в една директория :)
 
Това нещо май не работи :D има ли възможност да е с процедърен код а не ООП

Warning: fopen(http://localhost/3/screenshot.php?url=http%3A%2F%2Fwww.web-tourist.net&width=100&height=100&resized_width=100) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in E:\Programs\xampp\htdocs\thumbweb\start_sceenchot.php on line 11
Error! Couldn't open URL

Некфи такис грешки ми излизат :D

$image_filename тук дори не знам ко трябва са попълни

EDIT: Подкарах скрипта обаче има друг проблем, когато създаде снимката то е просто файл, но изображението липсва т.е не мога да прегледам снимката защото е един вид повредена
 
http://code.google.com/p/wkhtmltopdf/
В Download секцията има и пакет wkhtmltoimage. Хостинга ти трябва да е на Линукс и да можеш да изпълняваш външни програми с exec
 
еми хостинга ми не е на линукс в момента си работя с xampp и windows xp
Няма ли как да стане ?
 
Инструмента работи под Линукс и OSX.
Докато си на Win ще трябва да измислиш нещо друго.
Можеш да използваш онлайн инструменти като http://www.thumbalizr.com/ и с php да изпращаш заявката и да взимаш скрийншота в последствие
 
Еми щом е така имам попринцип и друга идея така, че явно нея ще трябва да използвам колкото ида не ми са иска :D
 
kikity_94 каза:
Еми щом е така имам попринцип и друга идея така, че явно нея ще трябва да използвам колкото ида не ми са иска :D
При мен кода си бачка перфектно ! Настройваш си пътищата в първия файл и си ток и жици :)

$url = 'http://www.google.bg'; //the URL you want to take a screen shot of$image_filename = 'google.jpg'; //where you want to save the image (needs to be a jpg in this example)
$width = 1000; //in pixels
$height = 800; //in pixels
$screenshot_url = 'http://localhost/3/screenshot.php'; //the full address of the file we just created
 
nbacool2 каза:
Това е решението - http://phantomjs.org/

С този код си нямам никаква идея как се работи има някакъв exe файл и някфи други, а в другата папка само .js и .coffe (кви са тез? :D) файлове :D

dakata__92 каза:
kikity_94 каза:
Еми щом е така имам попринцип и друга идея така, че явно нея ще трябва да използвам колкото ида не ми са иска :D
При мен кода си бачка перфектно ! Настройваш си пътищата в първия файл и си ток и жици :)

$url = 'http://www.google.bg'; //the URL you want to take a screen shot of$image_filename = 'google.jpg'; //where you want to save the image (needs to be a jpg in this example)
$width = 1000; //in pixels
$height = 800; //in pixels
$screenshot_url = 'http://localhost/3/screenshot.php'; //the full address of the file we just created

Дака и при мен работи кода въпроса е че не прави изображението нещо както трябва. Ето виж как ми излиза кода като го тествам
opt1345658522k.PNG


Самото изображение идва повредено.
Ето и как го тествам

PHP:
	$url = 'http://www.google.bg'; //the URL you want to take a screen shot of
	$image_filename = 'google.jpg'; //where you want to save the image (needs to be a jpg in this example)
	$width = 100; //in pixels
	$height = 100; //in pixels
	$screenshot_url = 'http://тук слагам ип-то ми/thumbweb/screenshot.php'; //the full address of the file we just created
	$resized_width = 500; //if you want to shrink the image, set this to a value in pixels.  otherwise, set it to false

И 2та файла са ми в една директория
 
kikity_94 каза:
естествено :D все пак се занимавам с уеб ;d
Питам те защото в screenshot.php се ползва като браузер Еxplorer-а :] При мен кода си бачка перфектно, явно или нещо не си настроил или, някоя настройка на браузела ти ебава нещата :)
 
kikity_94 каза:
Еми аз го тествам през мозила, но ей сега тествах и през IE, но пак същата работа ...
Нее си ме разбрал! През който и браузер да го ползваш си бачка :) За ИЕ говорех за тези редове :


screenshot.php
PHP:
//open up a new Internet Explorer window and go to the specified URL
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->Width = $width;
$browser->Height = $height;
$browser->Navigate($url);


....
 
Вместо
$screenshot_url = 'http://тук слагам ип-то ми/thumbweb/screenshot.php'; //the full address of the file we just created

да слагаш ИП адреса, ползвай само localhost :)

$screenshot_url = 'http://localhost/thumbweb/screenshot.php'; //the full address of the file we just created
 

Back
Горе