Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Manipulating Existing Images (0 Comments)
Admin: Posted Date: April 4, 2010

Another function of the GD Graphics Library is the ability to manipulate images that have already been created and are available on your site. Instead of the ImageCreate() and Image* functions, the ImageCreateFrom() function is used here.

Manipulating Existing Images

Another function of the GD Graphics Library is the ability to manipulate images that have already been created and are available on your site.

Instead of the ImageCreate() and Image* functions, the ImageCreateFrom() function is used here. There are 3 different ways to open the 3 image types supported by GD:

Note: The ImageCreateFromGif() function is supported from GD1.6 or earlier, the ImageCreateFromJpeg() function is supportted from GD1.8 or greater and ImageCreateFromPng() is supported from GD1.3 or greater.

Opening an existing PNG:

<?
// Open an existing PNG
$img_png = ImageCreateFromPng("imagename.png");
?>

Opening an existing GIF:

<?
// Open an existing GIF
$img_gif = ImageCreateFromGif("imagename.gif");
?>

Opening an existing JPG:

<?
// Open an existing JPG:
$img_jpg = ImageCreateFromJpeg("imagename.jpg");
?>

This allows you to create GD streams so you can manipulate them. Once you have created the string, you can manipulate them as if you had created the image from scratch. For example, this script will add text to the image:

<?php
header("Content-type: image/png");
if (($img = ImageCreateFromPng("imagename.png")) == "")
{    
echo "Error - Could not open image. Arghh!";
exit;
}

$red = ImageColorAllocate($img, 255, 0, 0);
ImageString($img, 3, 20, 10, "Text", $red): ImagePng($img);
?>

This wil add the text "Text" onto the image imagename.png. As you can see, this function has lots of uses!

 

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: