The process of creating images from other images follows the same essential steps as creating a new imagethe difference lies in what acts as the image canvas. Previously, you created a new canvas using the ImageCreate() function. When creating an image from a new image, you use the ImageCreateFrom*() family of 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 can see how easy it is to create a new image from an existing one. Figure 14.5 shows the base image.
Figure 14.5. The base image.
Listing 14.5 shows how to use an existing image as the canvas, which then has ellipses drawn on it.
Listing 14.5. Creating a New Image from an Existing Image
Save this listing as imagefrombase.php and place it in the document root of your web server. When accessed, it should look something like Figure 14.6.
Figure 14.6. Drawing on an existing image.
The next example takes this process a few steps forward and utilizes some different image-modification functions. In this case, the existing images are four PNG images, each with a differently colored triangular slice on a gray background. In Listing 14.6, you will stack these images on top of each other and blend them together at each step so that the gray background becomes transparent and the image beneath it shows through.
Listing 14.6. Stacking Images and Making Them Transparent
In line 3, one of the images is selected to be the base image. In this case, it's img1.png. The for loop in lines 811 handles the bulk of the work. Knowing that you have four images and that the first one is already used as the base, that leaves three more images to be stacked and made transparent.
After the new layer is created on line 8, its gray areas are indicated as transparent, and it is merged on top of the base image. As the layers are stacked, the base image contains an increasing number of layers until the total number of four layers is reached. At that point, the image is sent to the browser in lines 1516.
Save this listing as imagestacker.php and place it in the document root of your web server. When accessed, it should look something like Figure 14.7.
Figure 14.7. Stacked transparent images produce a composite.