GD Library - Print 2 Pictures on 1 Page

This section provides a tutorial example on how to compose a print page with 2 pictures placed vertically using GD Library functions.

You can follow this tutorial to compose a print page with 2 pictures placed vertically using GD Library functions.

1. Here is the PHP script, GD-2-To-1-Page.php:

<?php
#  GD-2-To-1-Page.php
#- Copyright 2009 (c) HerongYang.com. All Rights Reserved.

$resolution = 300;
$margin = intval(0.5*$resolution);
$page_width = intval(8.5*$resolution);
$page_height = intval(11.0*$resolution);

if (count($argv)<3) {
  print("Usage: php GD-2-To-1-Page input1 input2.\n");
  exit;
}
$pic1 = imagecreatefrompng($argv[1]);
$pic2 = imagecreatefrompng($argv[2]);

$page = imagecreatetruecolor($page_width,$page_height);
$white = imagecolorallocate($page, 255,255,255);
imagefill($page, 0,0, $white);

$width = $page_width - 2*$margin;
$height = intval($page_height/2 - 1.5*$margin);
$center_x = intval($width/2) + $margin;
$center_y = intval($height/2) + $margin;
post_on_page($page, $pic1, $center_x, $center_y, $width, $height);

$width = $page_width - 2*$margin;
$height = intval($page_height/2 - 1.5*$margin);
$center_x = intval($width/2) + $margin;
$center_y = intval($height/2) + $margin + $height + $margin;
post_on_page($page, $pic2, $center_x, $center_y, $width, $height);

$file = "GD-Page.png";
imagepng($page, $file);
print("Page saved:\n");
print("   File: $file\n");

function post_on_page($page, $pic, $center_x, $center_y, $width, $height) {
  $w = imagesx($pic);
  $h = imagesy($pic);
  $ratio = min($width/$w,$height/$h);
  if ($ratio<1.0) {
    $w = intval($ratio*$w);
    $pic = imagescale($pic,$w);
    $h = imagesy($pic);
  }
  $off_x = $center_x - intval($w/2);
  $off_y = $center_y - intval($h/2);
  imagecopy($page,$pic, $off_x,$off_y, 0,0, $w,$h);

  $black = imagecolorallocate($page, 0,0,0);
  imagerectangle($page,$off_x-1,$off_y-1, $off_x+$w+1,$off_y+$h+1, $black);
}
?>

Run it with 2 sample pictures:

php GD-2-To-1-Page.php sample1.png sample2.png
Page saved:
   File: GD-Page.png

Note that the script is using a resolution of 300 DPI. Your picture should have more than 300*8=2400 pixel in one direction to fill the page. In other words, the camera should take about 2400*2400=5,760,000 or 6M pixels.

Table of Contents

 About This Book

 Introduction and Installation of PHP

 Managing PHP Engine and Modules on macOS

 Managing PHP Engine and Modules on CentOS

 cURL Module - Client for URL

 DOM Module - Parsing HTML Documents

GD Module - Manipulating Images and Pictures

 GD Library of Image Functions

 GD Library - Draw Graphical Elements

GD Library - Print 2 Pictures on 1 Page

 GD Library - Create Transparent Image

 Transparent Image Over Text Web Page

 GD Library - Pad Transparent Image

 MySQLi Module - Accessing MySQL Server

 OpenSSL Module - Cryptography and SSL/TLS Toolkit

 PCRE Module - Perl Compatible Regular Expressions

 SOAP Module - Creating and Calling Web Services

 SOAP Module - Server Functions and Examples

 Zip Module - Managing ZIP Archive Files

 References

 Full Version in PDF/EPUB