You can create graphics image objects from existing image files using these functions:
This is great when you want to embed images in web pages but also want to add something to them yourself. You also can use this function to embed an image of yourself or a logo inside another image. As an example, we'll use imagecreatefromjpeg here, loading an existing JPEG image, image.jpg, and adding a border to it. Here's how you use this function: imagecreatefromjpeg (string filename) This function returns an image identifier representing the image obtained from the given filename, and it returns an empty string on failure. In this example, we plan to read in image.jpg. This image just displays the word "Cool!" against a green background, and we'll add to it by drawing a black border around the image when we create a new JPEG. We'll also create a black drawing color: $image = imagecreatefromjpeg ("image.jpg"); $back_color = imagecolorallocate($image, 200, 200, 200); $draw_color = imagecolorallocate($image, 0, 0, 0); You can use imagesx($image) and imagesy($image) to determine the dimensions of an image, so here's how we draw a black border around the image, using imagerectangle: $image = imagecreatefromjpeg ("image.jpg"); $back_color = imagecolorallocate($image, 200, 200, 200); $draw_color = imagecolorallocate($image, 0, 0, 0); imagerectangle($image, 10, 10, imagesx($image) - 10, imagesy($image) - 10, $draw_color); This all appears in phpimage.php, Example 12. Example 12. Adding to an existing image, phpimage.php<?php $image_height = 100; $image_width = 300; $image = imagecreatefromjpeg ("image.jpg"); $back_color = imagecolorallocate($image, 200, 200, 200); $draw_color = imagecolorallocate($image, 0, 0, 0); imagerectangle($image, 10, 10, imagesx($image) - 10, imagesy($image) - 10, $draw_color); header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); ?> We'll embed the new image in an HTML document, phpimage.html: <HTML><HEAD><TITLE>Using existing images</TITLE></HEAD> <BODY><CENTER> <H1>Using existing images</H1> <BR> <IMG src="/books/1/265/1/html/2/phpimage.php"> </CENTER></BODY> </HTML> You can see the new images, complete with their new border, in Figure 13. Figure 13. Embedding an image. |