Converting an HTML web page to a JPG image with PHP?

thisismyurl's picture

They have: 5 posts

Joined: Jan 2009

Hi all, I'm working on a web site that I need to show users a preview of their design choices before checkout but of course, using HTML they've be able to simply download the solution before paying. To which I though it would be better to use an HTML2JPG converter but I've been struggling to find a good one.

So my question is ... does anybody have a good HTML2JPG converter that will work in PHP? I know there's a couple for Windows ISS but in this particular case, I need to stay on Linux.

thanks, Chris

Christopher Ross

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

Hi Chris,

Welcome to TWF! Laughing out loud

The projects/portfolio section on this site uses http://www.websnapr.com/

might work for you... Smiling

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

I think the OP needs something more in house than websnapr. There is a script here that can make a PDF for you. If you need it in an image format, there should be a script that could take it from PDF to the image format you're looking for.

thisismyurl's picture

They have: 5 posts

Joined: Jan 2009

Thanks guys, both suggestions are great. I'll take a look at WebSnapr but based on the requirement to do multiple views of variable data, I might need to go with the PDF version. I'll let you both know how it turns out.

Christopher Ross

He has: 698 posts

Joined: Jul 2005

I've never tried it, but I think you could utilize the imagegrabscreen() or imagegrabwindow() functions from the GD library to output an image of the current screen. They should both work something like this:

<?php
//All of your normal code would go here
//But before exiting, grab a snapshot
$image = imagegrabscreen();
imagejpeg($image,"my_screenshot.jpeg");
imagedestroy($image);
?>

The imagegrabwindow() function requires more parameters that I haven't investigated, but the imagegrabscreen() function I believe would capture the entire screen, not just the window of focus.

Kurtis

JeevesBond's picture

He has: 3,955 posts

Joined: Jun 2002

I think you're right to go with a third-party solution. Generating a screen shot from the server can be a real hassle: Web servers don't generally come with a window manager installed, as they take up valuable RAM. Xorg is taking up 200MB on my desktop machine, for example (although you'll need much less for just taking a screen shot of a Web page).

I investigated doing screen shots for our project pages, but found that installing Xorg, worrying about the amount of RAM it would take, and setting it all up was far more work than just using a third-party solution. Smiling

Good luck with your search though, and welcome to the forums!

a Padded Cell our articles site!

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Those third party services are designed for public pages though, not HTML code that the OP is trying to keep private.

JeevesBond's picture

He has: 3,955 posts

Joined: Jun 2002

Those third party services are designed for public pages though, not HTML code that the OP is trying to keep private.

I meant to include your html2fpdf in the phrase third-party solution. Just haven't had any caffeine yet today.

I really should remedy that problem immediately! Smiling

a Padded Cell our articles site!

thisismyurl's picture

They have: 5 posts

Joined: Jan 2009

Hey all, the html2fpdf did most of what I needed so thanks for the great help.

AndrewCF's picture

They have: 3 posts

Joined: Jan 2009

Hi Chris,

If you website based Windows server, you can use this HTML Convert SDK - ACA WebThumb ActiveX.

A simple PHP Script for converting HTML to image:

<?php
  $t_xMaker
= new COM('ACAWebThumb.ThumbMaker')
    or die(
"Start ACAWebThumb.ThumbMakerfailed");

 
$t_xMaker->SetURL("http://www.webmaster-forums.net");
  if (
0 == $t_xMaker->StartSnap() )
  {
   
// Snap successful, call SetImageFile() to save the image.
   
echo "Take screenshot successful." ;
   
$t_xMaker->SaveImage("c:/webmaster-forums.png");
  }
?>

If you PHP version >= 5.2.2, you can using following code:

<?php
$browser
= new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->Navigate("http://www.webmaster-forums.net");

/* Still working? */
while ($browser->Busy) {
   
com_message_pump(4000);
}
$im = imagegrabwindow($handle, 0);
$browser->Quit();
imagepng($im, "webmaster-forums.png");
imagedestroy($im);
?>

They have: 58 posts

Joined: Dec 2010

Well that code snippet didn't work for me - @AndrewCF - can you possibly suggest what may be wrong?

By the way, I'm running latest version of PHP, but since I've never tried something similar in past, I'm not able to guess where I'm going wrong!

Consult the best indian seo company to boost your SERP rankings.

Grab the latest tech news and SEO updates on SeoEncyclopedia.com

They have: 1 posts

Joined: Nov 2011

i want to convert html page into image with php, but i can;t found any where proper response, i also use gd but not anything there, also i use imageegrabscreen(). please nay one hav any idea correct idea how to do this foor linux os. thnks plz reply soon as project deadline aare near...

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.