Provide state-of-the-art image capture, screen capture, video capture and audio capture software
Home     Download     Purchase     Support     Contact     View my shopping cart
You are here: ACASystems » ACA WebThumb ActiveX Control » FAQ » Convert HTML to image using PHP

How to convert HTML to image with PHP? - PHP Sample Code(1)

Download ACA WebThumb ActiveX

For Windows 7/Vista/2003/XP/2000

To convert HTML to image with PHP, the easiest way is to use our HTML converter SDK - ACA WebThumb ActiveX.

With a few function calls in PHP, it helps you convert html page from any URL to image or thumbnails easily and quickly, takes the snapshots of given URLs as JPG, GIF, PNG, BMP, TIFF, WMF and EMF image.

Guidelines: Convert HTML to image with PHP

  1. Free download the HTML Converter SDK - ACA WebThumb ActiveX, and then install it.
  2. You will find rich php sample scripts in the folder {install-folder}\sample\php.
  3. If you want launch these samples, you must copy/upload these PHP script files to your WWW directory, and enter the script URL in your browser, for example: http://mydomain/php/index.html.

PHP Code: Convert HTML to image with PHP

A simple PHP Script for converting HTML to image:

<?
  // PHP html to image.
  // This script shows how to convert the google.com homepage to a PNG image file.
  $WebThumb_Maker = new COM('ACAWebThumb.ThumbMaker')
    or die("Start ACAWebThumb.ThumbMakerfailed");

  $WebThumb_Maker->SetURL("http://www.google.com"); 
  if ( 0 == $WebThumb_Maker->StartSnap() )
  {
    // Tanke snapshot successful, call SetImageFile() to save the image as a PNG file.
    echo "Take snapshot successful." ;
    $WebThumb_Maker->SaveImage("google.png");
  }
?>

Reference

  • IThumbMaker Interface Reference: StartSnap() - Start the snap job.
  • IThumbMaker Interface Reference: SaveImage() - Save the image to the local file.

PHP Code: Convert HTML to image in memory with PHP, and then output to client browser

This PHP sample script shows how to make a web thumbnail image from HTML page in the memory and output to client browser.

PHP script file: online-thumb.php
<?
  //----------------------------------------------------------------------------------------
  // Script:
  //   online-thumb.php
  //
  // Remark:
  //   This sample script shows how to make a web thumbnail in the memory and 
  //   output to client browser.
  // 
  // Parameters:
  //   url: The URL of HTML page that you want to convert. the default URL is 
  //        http://www.google.com
  //   width: The thumbnail image width. The default value is 320.
  //   height: The thumbnail image height. The defaul value is 240.
  //   ratiotype: The width/height ratio type. 0: keep ratio by width; 1: keep ratio by height.
  //              The default is 0.
  //
  // Example:
  //   <img src="online-thumb.php"> or
  //   <img src="online-thumb.php?url=http://www.google.com"> or
  //   <img src="online-thumb.php?url=http://www.google.com&width=320&height=240&ratiotype=0">
  //----------------------------------------------------------------------------------------

  // Create instance ACAWebThumb.ThumbMaker
  $HTML_Converter = new COM("ACAWebThumb.ThumbMaker") or die
    ("Create ACAWebThumb.ThumbMaker failed. Please make sure the component has been installed.");

  // Get the parameters
  $t_strURL = isset($_GET["url"]) ? $_GET["url"] : "http://www.google.com";
  $t_iWidth = isset($_GET["width"]) ? $_GET["width"] : 320;
  $t_iHeight = isset($_GET["height"]) ? $_GET["height"] : 240;
  $t_iRatioType = isset($_GET["ratiotype"]) ? $_GET["ratiotype"] : 0;

  // Set the URL and start the snap job.
  $HTML_Converter->SetURL($t_strURL); 
  if ( 0 == $HTML_Converter->StartSnap() )
  {
    // snap successful, set the thumbnail size and get image bytes
    $HTML_Converter->SetThumbSize ($t_iWidth, $t_iHeight, $t_iRatioType);
    //get image bytes by PNG format
    $t_arrThumbImageBytes = $HTML_Converter->GetImageBytes ("png"); 
    // output the image bytes to client browser
    if ( count($t_arrThumbImageBytes) > 0 )
    {
      // set the output header as PNG image, then output the thumbnail image bytes.
      header("Content-type: image/png");  
      foreach($t_arrThumbImageBytes as $byte) 
        echo chr($byte);
    }
  }  
?>

* If you have installed ACA WebThumb ActiveX on your computer, you can find this php script in the folder {install-folder}\sample\php\online-thumb.php.

Download ACA WebThumb ActiveX

For Windows 7/Vista/2003/XP/2000

Reference

  • IThumbMaker Interface Reference: GetImageBytes() - Get the image memory-bytes of the thumbnail image.
  • PHP Manual: header() - Send a raw HTTP header

Other resource about Converting HTML to image with PHP:

  • PHP Sample Code(2): How to convert webpage to image with PHP?
  • C# Sample Code: How to take screenshot of web page in C#(C-sharp)?
  • FAQ: Which image format is best?
  • FAQ: What programming languages does ACA WebThumb ActiveX Control support?
  • ACA WebThumb ActiveX Interface Reference
  • Free Download ACA WebThumb ActiveX Control
  • W3Schools: HTML Images

  • Get discount alerts, new product information and technical articles:
    Name:
    Email:
    * Get 10% off your first order by joining our mailing list!
    * You may safely unsubscribe at any time!