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 (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 7.3

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Configuring and Sending out Emails

Image and Picture Processing

 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

 Managing PHP Engine and Modules on macOS

 Managing PHP Engine and Modules on CentOS

 Outdated Tutorials

 References

 Full Version in PDF/EPUB