0% found this document useful (0 votes)
66 views

Web Based Application Development With PHP: WBP (22619) Lect 11

This document discusses creating and manipulating images using PHP's GD graphics library. It covers basic graphics concepts like pixels, colors, and file formats. It then describes functions for creating, drawing on, and modifying images through operations like scaling, rotation, and adding text. The document also introduces the FPDF library for generating PDF documents from PHP scripts. Key functions and methods discussed include imagecreate(), imagecolorallocate(), imagefilledrectangle(), imagerotate(), imagestring(), imagecopyresampled(), and the FPDF class.

Uploaded by

Govind Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Web Based Application Development With PHP: WBP (22619) Lect 11

This document discusses creating and manipulating images using PHP's GD graphics library. It covers basic graphics concepts like pixels, colors, and file formats. It then describes functions for creating, drawing on, and modifying images through operations like scaling, rotation, and adding text. The document also introduces the FPDF library for generating PDF documents from PHP scripts. Key functions and methods discussed include imagecreate(), imagecolorallocate(), imagefilledrectangle(), imagerotate(), imagestring(), imagecopyresampled(), and the FPDF class.

Uploaded by

Govind Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Web Based Application

development with PHP


WBP (22619)
Lect 11

G. K. YADAV
Lect, Computer Engineering
Government Polytechnic Yavatmal
COMPETENCY
Develop simple web-based application
using PHP language
Unit-II
Arrays, Functions and Graphics
CO
b) Perform operations based on arrays and
graphics.
UO
2 d) Scale the given image using graphics
concept/functions.
Basic Graphics Concept:
• An image is a rectangle of pixels of various colors
• Colors are identified by their position in the palette, an array of colors. Each
entry in the palette has three separate color values—one for red, one for
green, and one for blue. Each value ranges from 0 (this color not present)
to 255 (this color at full intensity).
• Image files are rarely a straightforward dump of the pixels and the palette.
Instead, various file formats (GIF, JPEG, PNG, etc.) have been created that
attempt to compress the data somewhat to make smaller files.
• Different file formats handle image transparency, which controls whether
and how the background shows through the image, in different ways.
Some, such as PNG, support an alpha channel, an extra value for every pixel
reflecting the transparency at that point. Others, such as GIF, simply
designate one entry in the palette as indicating transparency. Still others,
like JPEG, don’t support transparency at all.
Basic Graphics Concept:
• Antialiasing is where pixels at the edge of a shape are moved or
recolored to make a gradual transition between the shape and
its background. This prevents the rough and jagged edges that
can make for unappealing images. Some functions that draw on
an image implement antialiasing.
• With 256 possible values for each of red, green, and blue, there
are 16,777,216 possible colors for each pixel. Some file formats
limit the number of colors you can have in a palette (e.g., GIF
supports no more than 256 colors); others let you have as
many colorsas you need.
Creating images:
<?php
$image = imagecreate(200, 200);
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
imagefilledrectangle($image, 50, 50, 150, 150, $black);
header("Content-Type: image/png");
imagepng($image);
?>
The Structure of a Graphics Program:
• You can create a 256-color image with the imagecreate() function,
which returns an image handle:
$image = imagecreate(width, height);
• All colors used in an image must be allocated with the
imagecolorallocate() function. The first color allocated becomes the
background color for the image1:
$color = imagecolorallocate(image, red, green, blue);
we wrote the color values in hexadecimal to bring the function call
closer to the HTML color representation #FFFFFF and #000000.
• There are many drawing primitives in GD. Example uses
imagefilledrectangle(), in which you specify the dimensions of the
rectangle by passing the coordinates of the top-left and bottom-
right corners:
imagefilledrectangle(image, tlx, tly, brx, bry, color);
The Structure of a Graphics Program:
• The next step is to send a Content-Type header to the browser with the
appropriate content type for the kind of image being created.
• Once that is done, we call the appropriate output function. The imagejpeg(),
imagegif(), imagepng(), and imagewbmp() functions create GIF, JPEG, PNG,
and WBMP files from the image, respectively.
imagegif(image [, filename ]);
imagejpeg(image [, filename [, quality ]]);
imagepng(image [, filename ]);
imagewbmp(image [, filename ]);
• If no filename is given, the image is output to the browser; otherwise, it
creates (or overwrites) the image to the given file path.
• The quality argument for JPEGs is a number from 0 (worst-looking) to 100
(best-looking). The lower the quality, the smaller the JPEG file. The default
setting is 75
Basic Drawing Functions:
• The most basic function is imagesetpixel(), which sets the color
of a specified pixel: imagesetpixel(image, x, y, color);
• There are two functions for drawing lines, imageline() and
imagedashedline():
imageline(image, start_x, start_ y, end_x, end_ y, color);
imagedashedline(image, start_x, start_ y, end_x, end_ y, color);
• There are two functions for drawing rectangles, one that simply
draws the outline and one that fills the rectangle with the specified
color:
imagerectangle(image, tlx, tly, brx, bry, color);
imagefilledrectangle(image, tlx, tly, brx, bry, color);
Specify the location and size of the rectangle by passing the
coordinates of the top-left and bottom-right corners.
Basic Drawing Functions:
• You can draw arbitrary polygons with the imagepolygon() and
imagefilledpolygon() functions:
imagepolygon(image, points, number, color);
imagefilledpolygon(image, points, number, color);
• The imagearc() function draws an arc (a portion of an ellipse):
imagearc(image, center_x, center_ y, width, height, start, end, color);
The ellipse is defined by its center, width, and height (height and
width are the same for a circle). The start and end points of the
arc are given as degrees counting counter-clockwise from 3
o’clock. Draw the full ellipse with a start of 0 and an end of 360
Basic Drawing Functions:
• There are two ways to fill in already-drawn shapes. The
imagefill() function performs a flood fill, changing the color of
the pixels starting at the given location. Any change in pixel
color marks the limits of the fill.
imagefill(image, x, y, color);
• The imagefilltoborder() function lets you pass the particular
color of the limits of the fill.
imagefilltoborder(image, x, y, border_color, color);
• The image rotate() function allows you to rotate an image by an
arbitrary angle:
imagerotate(image, angle, background_color);
Example
<?php
$image = imagecreate(200, 200);
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
imagefilledrectangle($image, 50, 50, 150, 150, $black);
$rotated = imagerotate($image, 45, 1);
header("Content-Type: image/png");
imagepng($rotated);
?>
Images with Text
• Often it is necessary to add text to images. GD has built-in
fonts for this purpose.
• The imagestring() function adds text to an image. Specify
the top-left point of the text, as well as the color and the
font (by GD font identifier) to use:
imagestring(image, font_id, x, y, text, color);
• GD identifies fonts by an ID. Five fonts are built-in, and you
can load additional fonts through the imageloadfont()
function.
Example
<?php
$image = imagecreate(200, 200);
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
imagefilledrectangle($image, 50, 50, 150, 150, $black);
imagestring($image, 5, 50, 160, "A Black Box", $black);
header("Content-Type: image/png");
imagepng($image);
?>
Scaling Images
• There are two ways to change the size of an image.
• The imagecopyresized() function is fast but crude, and may lead
to jagged edges in your new images.
imagecopyresized(dest, src, dx, dy, sx, sy, dw, dh, sw, sh);
• The imagecopyre sampled() function is slower, but features pixel
interpolation to give smooth edges and clarity to the resized
image.
imagecopyresampled(dest, src, dx, dy, sx, sy, dw, dh, sw, sh);
• The dest and src parameters are image handles. The point (dx, dy) is
the point in the destination image where the region will be copied.
The point (sx, sy) is the upper-left corner of the source image. The
sw, sh, dw, and dh parameters give the width and height of the copy
regions in the source and destination.
Example
<?php
$source = imagecreatefromjpeg("php.jpg");
$width = imagesx($source);
$height = imagesy($source);
$x = $width / 2;
$y = $height / 2;
$destination = imagecreatetruecolor($x, $y);
imagecopyresampled($destination, $source, 0, 0, 0, 0, $x, $y, $width,
$height);
header("Content-Type: image/png");
imagepng($destination);
?>
Creation of PDF document
• Adobe’s Portable Document Format (PDF) provides a popular way to
get a consistent look, both on screen and when printed, for
documents.
• PHP has several libraries for generating PDF documents.
• PDFlib: The PHP API contains a number of functions for handling PDF
files designed to be used with the PDFlib. Although extensive, this
library is not free for commercial use. A free version called PDFlib
Lite is available for personal use, but is limited in functionality.
• The FPDF library is a set of PHP code you include in your scripts with
the require function, so it doesn’t require any server-side
configuration or support, meaning you can use it even without
support from your host
Why FPDF?:
• What is FPDF?:FPDF is a PHP class which allows to generate PDF
files with pure PHP, that is to say without using the PDFlib
library.
• You are free to download and use this class or customize it to fit
your needs. In addition to being free, it's also simpler to use
than PDFlib. The PDFlib needs to be installed as an extension in
your PHP package, whereas FPDF can just be included in your
PHP script and it's ready to use.
• Download FPDF from https://www.fpdf.org Keep the fpdf.php
file in your working directory, keep the font directory in the
same place.
Creating PDF files
• To get started, you will need to download the FPDF class from the
FPDF Web site and include it in your PHP script like this:
require('fpdf.php’);
• We begin by creating a new FPDF object with:
$pdf= new FPDF();
• Because we want to use the same font throughout the whole
document, we can set it before we create a page.
$pdf->SetFont('Helvetica','B',20);
$pdf->SetTextColor(50,60,100); //RGB values
• Now that that's done, let's set up a page for our PDF documentYou
can pass the AddPage () a parameter of "P" or "L" to specify the page
orientation.
$pdf->AddPage('P');
Creating PDF files
• Now, that we've set up a page, let's insert an image to make it look
nicer and make it a link while we're at it.
$pdf->Image('logo.png',10,20,33,0,' ','http://www.fpdf.org/’);
$pdf->Link(10, 20, 33,33, 'http://www.fpdf.org/’);
• we call the Cell function to print out a cell rectangle along with the
text of our title. We are passing the function the following
parameters; width, height, text, border, ln, align and fill.
$pdf->Cell(100,10,'FPDF Tutorial',1,0,'C',0);
• Finally we want to send the output to a given destination, using the
Output function. The "I" parameter will send the output to the
browser.
$pdf->Output('example1.pdf','I’);
Example
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B’,18);
$pdf->cell(80,10,'Hello World!');
$pdf->Output(); // Send to browser and display
?>

You might also like