Webpages to PNg/Jpg/Gif/BMP

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
ahsanra
Forum Newbie
Posts: 9
Joined: Fri Jan 20, 2006 12:57 am

Webpages to PNg/Jpg/Gif/BMP

Post by ahsanra »

Hi,

Is there a way to convert a piece of HTML/webpage to a PNg/JPG/GIF/BMP format.
How can we do that in PHP?


waiting for your response.

Thanks
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Its been discussed before. Search this forum for "screenshot"
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

Grab yourself a copy of IECapt...
Then use this little script. :D

http://iecapt.sourceforge.net/

Code: Select all

<?php
/**
 * IECapt - PHP Frontend.
 * -
 * IECapt is a small application designed for screengrabbing websites
 * Using the Internet Explorer rendering engine.
 * -
 * This frontend is designed to allow people to enter URLs online
 * and be thrown a screenshot (resized to required dimensions) back
 * -
 * This code is COPYRIGHT (c) 2006, Peter Corcoran. ALL RIGHTS RESERVED.
 * ANY UNLICENSED USAGE WILL BE PUNISHED TO THE FULL EXTENT OF THE LAW.
 **/
 ?>
 <html>
  <head>
    <title>IECapt - Frontend</title>
    <style>
     * {
         font-family: verdana;
         font-size: 10px;
     }
    </style>
  </head>
  <body>
 <?php
/**
 * Okay, we will certainly go over the 30 second time limit.
 * So we need to get rid of it. UNLIMITED TIME!!! :P
 **/ 
set_time_limit(0);
/**
 * If we had a URL posted...
 **/
if($_POST['url']) {
    /**
     * This URL is going to end up in the shell.
     * So we need to make it safe!
     **/
    $f = escapeshellcmd($_POST['url']);
    /**
     * Notify the user that we are working on creating the thumbnail
     **/
    echo "    Attempting to create thumb... Please wait<br />\n";
    /**
     * Flush the output buffer.
     * -> This is for stupid browsers that won't display text till 100% loaded
     **/
    flush();
    /**
     * Lets start creating that thumbnail
     * -> This step takes a LONG time.
     **/
    shell_exec("IECapt.exe \"" . $f . "\" " . md5($f) . ".png");
    /**
     * Okay, the max width was sent to us via _POST
     * and the max height doesn't exist, so make it big.
     **/
    $maxw = (int)$_POST['maxw'];
    $maxh = 10000;
    /**
     * We need to resize what the IECapt generated now...
     * -> So lets load it!
     **/
	$im = imagecreatefrompng(md5($f) . ".png");
	/**
	 * Lets find out how big the image, that IECapt got, is...
	 **/
	$w = ImageSX($im);
	$h = ImageSY($im);
	/**
	 * Now resize that baby!
	 * -> Thank you Alex Poole :)
	 **/
	list($image) = resizeImage($im, $w, $h, $maxw, $maxh);
	/**
	 * Resized... lets throw it back where it came from.
	 **/
	imagepng($image, md5($f) . ".png");
	/**
	 * And let the user know we have finished.
	 **/
    echo "    Created!<br /><img src=\"" . md5($f) . ".png\" />\n";
/**
 * End If
 **/
}

/**
 * Output HTML
 **/
?>
    <form actiom="thumb.php" method="POST">
      <table>
        <tr><td>URL to Thumb: </td><td><input type="text" name="url" />        </td></tr>
        <tr><td>Maximum Width:</td><td><input name="maxw" value="300" />       </td></tr>
        <tr><td>&nbsp;        </td><td><input type="submit" value="ThumbIT!" /></td></tr>
      </table>
    </form>
  </body>
</html>
<?php
/**
 * resizeImage
 * -
 * A small function by Alex Poole for resizing images
 **/
function resizeImage($image, $iw, $ih, $maxw, $maxh) {
    if ($iw > $maxw || $ih > $maxh){
        if ($iw>$maxw && $ih<=$maxh){
            $proportion=($maxw*100)/$iw;
            $neww=$maxw;
            $newh=ceil(($ih*$proportion)/100);
        } else if ($iw<=$maxw && $ih>$maxh){
            $proportion=($maxh*100)/$ih;
            $newh=$maxh;
            $neww=ceil(($iw*$proportion)/100);
        } else {
            if ($iw/$maxw > $ih/$maxh){
                $proportion=($maxw*100)/$iw;
                $neww=$maxw;
                $newh=ceil(($ih*$proportion)/100);
            } else {
                $proportion=($maxh*100)/$ih;
                $newh=$maxh;
                $neww=ceil(($iw*$proportion)/100);
            }
        }
    } else {
        $neww=$iw;
        $newh=$ih;
    }
    if (function_exists("imagecreatetruecolor")){
        $newImage=imagecreatetruecolor($neww, $newh);
        imagecopyresampled($newImage, $image, 0,0,0,0, $neww, $newh, $iw, $ih);
    } else {
        $newImage=imagecreate($neww, $newh);
        imagecopyresized($newImage, $image, 0,0,0,0, $neww, $newh, $iw, $ih);
    }
    return Array($newImage, $neww, $newh);
}
?>
ahsanra
Forum Newbie
Posts: 9
Joined: Fri Jan 20, 2006 12:57 am

Post by ahsanra »

well this utility is for windows server and i am using LINUX server.

i need a solution that works with php and linux.


Thanks
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

I'd be interested in this as well. I'm currently using a firefox extension to acomplish this task manually, it's a real pita when we have hundreds of client sites we'd like a full screenshot for..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Port Gecko to PHP. :)
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

:lol: I'll get right on that ;)

Though, on that note, the extension uses java to get the screenshots (i havn't had a chance to look thru the extension), so I'm thinking that there must be some way to either modify the extension, or use php to access the java part of the code /somehow/ and get the image data I'm looking at...

Using: http://andy.5263.org/screengrab/
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

I 'pose it wouldn't be too hard to make:
http://khtml2png.sourceforge.net/
work on a server, then use something like the code I posted about to create the screens.
Post Reply