You are on page 1of 42

Images in PHP

By Priti Kudal
Graphics
● Image,Shapes,Colors,Fonts
Before executing image and graphics of php on XAMPP, Perform following:
1. Goto XAMPP control panel ,Click Config, Select PHP(php.ini) file
2. In that file,search or find (CTRL+F key) “gd “ word,remove semicolon from
that line=
;extension=gd

Make it as by removing “;”


extension=gd
3. Save File
4. Stop and Start XAMPP Services again.
imagecreate()

is used to create a new


Syntax: image

Imageobject imagecreate(
$width, $height);
Warning: imagepng(): gd-png error: no colors in
palette in
imagecolorallocate()
used to set the color in an image.
and returns a color which is

Syntax: given in RGB format.

int imagecolorallocate (
$image, $red, $green, $blue );
<?php
header("Content-Type: image/png");

w h
$im = imagecreate(500, 500);
$col = imagecolorallocate($im,30,50,10);
imagepng($im);
?>
RGB
imagestring()
used to draw the string
horizontally.
This function draws the string
Syntax: at given position.
bool imagestring( $image,
$font, $x, $y, $string, $color );
<?php
header("Content-Type: image/png");
w h
$im = imagecreate(500, 500);
$col = imagecolorallocate($im,30,50,10);

RGB
$textcol = imagecolorallocate($im, 255, 0,0);
imagestring($im, 12, 0, 0, 'Hello world!', $textcol);
imagepng($im);
?>
Font size X coord Y coord
getimagesize()
used to get the size of an image.
This function accepts the filename
as a parameter and determines the
image size and returns the
dimensions with the file type and
height/width of image.
Syntax:
array getimagesize( $filename,
$image_info );
<?php
$s = getimagesize("C:/xampp/htdocs/i1.png");
print_r($s);
Array ( [0] => 1473 [1] => 830
[2] => 3 [3] => width="1473" height="830"
[bits] => 8 [mime] => image/png )
used to get the index of specified
color in the palette of the image.
imagecolorexact()
In created image files only used
colors in the image are resolved.
Syntax: Colors present only in the palette are
not resolved.
int imagecolorexact ( $image,
$red, $green, $blue );
<?php
$im = imagecreatefrompng('C:/xampp/htdocs/i1.png');
$colors = Array();
$colors[] = imagecolorexact($im, 255, 0, 0);
$colors[] = imagecolorexact($im, 0, 0, 0);
$colors[] = imagecolorexact($im, 255, 255, 255);
$colors[] = imagecolorexact($im, 100, 255, 52);
print_r($colors);
?>
Array ( [0] => 16711680 [1] => 0 [2] =>
16777215 [3] => 6618932 )
imagecopy() used to copy the image or part of
image. This function returns true on
success or false on failure.
Syntax:
bool imagecopy ( $dst_image,
$src_image, $dst_x, $dst_y, $src_x,
$src_y, $src_w, $src_h );
<?php
$src = imagecreatefrompng('C:/xampp/htdocs/i1.png');
$dest = imagecreate(1000, 640);
imagecopy($dest, $src,200, 150, 420, 230, 480, 340);
header('Content-Type: image/png');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src);
?>
used to crop an image to the
imagecrop() given rectangle. This function
crops an image to the given
rectangular area and returns the
resulting image. The given image
Syntax:
is not modified.
resource imagecrop( $image,
$rect );
<?php
header('Content-Type: image/png');

$im = imagecreatefrompng('C:/xampp/htdocs/i1.png');
$im2 = imagecrop($im, ['x' => 0, 'y' =>0, 'width'
=>600, 'height' =>500]);

imagepng($im2, 'cropped.png');
?>
Bowser will not display output.
Check the same folder where original image is ,there itself cropped
image will be saved with given name
After double clicking on that image,Cropped image
will be shown:
imagefill() used to fill the image with
the given color.
This function performs a
flood fill starting at the
Syntax: given coordinate (top left is
0, 0) with the given color in
bool imagefill( $image, $x, $y,
the image.
$color );
<?php
$im = imagecreate(100, 100);
// sets background to red
$red = imagecolorallocate($im, 255, 0, 0);
imagefill($im, 0, 0, $red);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
imageflip()

Flip an image horizontally,


Syntax: vertically or both horizontally
and vertically using the given
bool imageflip( $image,
$mode ); mode.

IMG_FLIP_HORIZONTAL – Flips the image


horizontally.
IMG_FLIP_VERTICAL – Flips the image vertically.
IMG_FLIP_BOTH – Flips the image both horizontally and
vertically.
<?php
$im =
imagecreatefrompng('C:/xampp/htdocs/i1.png');
imageflip($im, IMG_FLIP_VERTICAL);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Summary - Images in PHP
- Functions related to image
Thank You

You might also like