The process of creating images from other images follows the same essential steps as creating a new image-the difference lies in what acts as the image canvas. In the previous section, you created a new canvas using the imagecreate() function. When creating an image from a new image, you'll use the imagecreatefrom...() functions.
You can create images from existing GIFs, JPEGs, PNGs, and plenty of other image types. The functions used to create images from these formats are called imagecreatefromgif(), imagecreatefromjpg(), imagecreatefrompng(), and so forth. In the next example you'll see how easy it is to create a new image from an existing one. The base image is shown in Figure 8.5.
Figure 8.5: The base image from which you'll create other images
The following code shows you how to create the new canvas from the base image, allocate the color $white to the image, and draw three filled ellipses at a point on the image, as shown in Figure 8.6.
Figure 8.6: New image with ellipses
<? $im = imagecreatefromjpeg ("baseimage.jpg"); $white = imagecolorallocate ($im, 255, 255, 255); imagefilledellipse($im, 75, 70, 20, 20, $white); imagefilledellipse($im, 150, 70, 20, 20, $white); imagefilledellipse($im, 225, 70, 20, 20, $white); header("Content-type: image/jpeg"); imagejpeg($im); ?>
If you can contain your excitement, take a look at the next code, which takes this process a few steps forward and utilizes some different image modification functions.
In Figure 8.7, you can see four PNG images-differently colored (just take my word for it) triangular slices on a gray background.
Figure 8.7: PNG slices
In this example, the gray was defined as an alpha channel when the PNG was created (not using PHP, although you could). The goal of this code is to stack these slices on top of one another, blending them together at each step so that the alpha channel becomes transparent and the bottom image shows through.
First, select one image as a base image, in this case sqaure1a.png:
$baseimage = ImageCreateFromPNG("square1a.png");
Next, blend the bottom image:
Repeat the process for the second sqaure:
$image2 = ImageCreateFromPNG("square1b.png"); ImageAlphaBlending($image2, true);
But now copy this second image stream to the first image stream:
ImageCopy($baseimage, $image2, 0, 0, 0, 0, 150, 150);
Repeat the process with the remaining two slices, and then output the image stream. Your code should look something like this:
<? $baseimage = ImageCreateFromPNG("square1a.png"); ImageAlphaBlending($baseimage, true); $image2 = ImageCreateFromPNG("square1b.png"); ImageAlphaBlending($image2, true); ImageCopy($baseimage, $image2, 0, 0, 0, 0, 150, 150); $image3 = ImageCreateFromPNG("square1c.png"); ImageAlphaBlending($image3, true); ImageCopy($baseimage, $image3, 0, 0, 0, 0, 150, 150); $image4 = ImageCreateFromPNG("square1d.png"); ImageAlphaBlending($image4, true); ImageCopy($baseimage, $image4, 0, 0, 0, 0, 150, 150); header("Content-type: image/png"); ImagePNG($baseimage); ?>
The resulting image is shown in Figure 8.8, you'll note that it doesn't have the gray background surrounding the colored areas, as those have been alpha-blended away.
Figure 8.8: Copying images with alpha blending
If you had instead plopped these images on top of each other without blending the alpha channel, you would have ended up with what's shown in Figure 8.9, which is just the fourth image sitting on top of the others. You can't see the others, as they're obscured by the top image, which has no transparent parts.
Figure 8.9: Copying images without alpha blending