. 35
( 132 .)


— The offset width to take from the source image

— The offset height to take from the source image


resource imagecreate (int x_size, int y_size)

This function creates a new image with the given dimensions, and returns its


resource imagecreatefromstring (string image)

This function creates a new image from the image stream given in the argument
string, and returns its identifier.

int imagepng (resource image [, string filename])

Given an image identifier, imagepng() outputs an image stream, in PNG format,
to either the user™s browser or, if it is given a file name as a second argument, to the
specified file.


int imagesx (resource image)

int imagesy (resource image)

These two functions return the dimensions of the referenced image: imagesx()
returns the width, and imagesy() returns the height.
Chapter 6: PHP™s Built-in Functions 183


int imagetypes (void)

This function checks the configuration of the GD library that PHP is using to see
which image types are supported. It returns a bitmask containing a value that cor-
responds to the AND sum of the constants that represent each image type. You can
test for PNG support, therefore, with code like the following:

if (imagetypes() & IMG_PNG)
echo “PNG support is enabled.”;


int exif_imagetype (string filename)

exif_imagetype() examines an image file and determines the type of image it
is, returning a numeric code, or FALSE if it is not a recognizable image type. PHP
must be compiled with ” enable-exif.
Refer to the online PHP manual (http://www.php.net/manual/en/ref.image.
php) for a list of image types and their codes.


string exif_thumbnail (string filename [, int width [, int height [,
int imagetype]]])

If an image contains an embedded thumbnail, this function will retrieve the
thumbnail as a data stream. For example:

$thumb= exif_thumbnail($imagefile, 40, 50);
echo $thumb;

If the preceding code is stored as a file called thumbnail.php, a subsequent HTML
call to this file as an image will display the thumbnail:

<img src=”thumbnail.php” height=”50” width=”40”>

Mail function
If you have Sendmail or another suitable email program installed on your system,
this function will take all the fuss out of sending email from your PHP pages.
184 Part II: Working with PHP

Sendmail is the program most commonly used with PHP™s mail function, but
qmail with Sendmail wrappers will work, and Pegasus (http://pegasus.
usa.com/) can apparently work on Windows (though we haven™t tested it).

The mail() function sends an email from your PHP script.

bool mail (string to, string subject, string message [, string
additional_parameters] )

Your basic email will look like this:

mail(“name@domain.com”,”Subject Text”, “The complete message goes here”);

And if you want to get a little fancier and include a From and a Cc:, use the

mail(“jay@trans-city.com”,”Test Message”, “Here I am”,
“From: Jay G\r\nCc: webmonkey@trans-city.com\r\nReply-to:

Additional parameters have been added in the fourth argument, and the differ-
ent parameters are separated by line feeds and newlines (\r\n).

If you want to set up a large email system, don™t use PHP. Better tools are out
there. This function is intended for sending an occasional email from within
PHP scripts.

If you™d like to send attachments in your PHP email, check out this excellent
article at phpbuilder.com: http://phpbuilder.com/columns/

URL functions
If you™ve ever looked at a query string, you may have noticed that the text you
entered into your form fields has been changed. For examples, spaces are turned
into plus signs (+) and each ampersand (&) becomes %26. Many other characters are
encoded. (All non-alphanumeric characters other than the hyphen (-), underscore
(_), and dot (.) are replaced by a percent sign (%) and two characters).
Chapter 6: PHP™s Built-in Functions 185

On occasion you will need to encode or decode text. For that you will use the
functions below.

This function encodes a string so that it™s URL-ready. Most often you will use this
function if you want to send variable information to another page.

string urlencode(string str)

For example:

$myvar=”this string with weird &* stuff”;
$encoded = urlencode($myvar);
header(“Location: http://www.mydomain.com?var=$encoded”);

Notice that this code snippet has only encoded the values of a querystring ele-
ment. If you were to urlencode the entire URL, you would not be happy with the
results. The result of this code


is http%3A%2F%2Fwww.mydomain.com.

This function undoes the encoding process. It™s usually unnecessary because the
variable created from your GET or POST data is decoded in your variables.

string urldecode(string str)

This function returns a string in which all non-alphanumeric characters except the
hyphen, underscore, and dot have been replaced with a percent (%) sign followed by
two characters.

string rawurlencode(string str)

This is the encoding described in RFC 1738 for protecting literal characters from
being interpreted as special URL delimiters, and for protecting URLs from being
mangled by transmission media with character conversions (like some email sys-
tems). For historical reasons, spaces are encoded as plus (+) signs.

This function unencodes according to the same provisions as rawurlencode().

string rawurldecode(string str)
186 Part II: Working with PHP

This function encodes a specified string in such a way as to eliminate all possible
control characters, meaning the string can be sent over any transmission medium
without the need to worry that the string could be interpreted as a command.

string base64_encode(string str)

This function undoes what base64_encode() does. That is, it converts the encoded
string back into its original form.

string base64_decode(string str)

Error functions
PHP gives you a high level of control over how errors are handled and reported to
the user. Here is an overview of some of the important error-handling functions that
are offered.

This function sets the level of error reporting.

int error_reporting ([int level])

Possible levels of reporting are:

— 0 ” No reporting

— 1 ” Errors

— 2 ” Warnings

— 4 ” Parse errors

— 8 ” Notices

You are encouraged to use the predefined named constants instead of raw num-
bers: E_ERROR, E_WARNING, E_PARSE, E_NOTICE, E_ALL, and so on. See the online PHP
documentation (http://www.php.net/manual/en/function.error-reporting.
php) for more details about levels of error reporting.
If you want a particular expression to cast no errors, you can prefix it with the @


int error_log (string message [, int message_type [, string
destination [, string extra_headers]]])
Chapter 6: PHP™s Built-in Functions 187

If you want to log a custom error message, this is the function to use. It can send
a message to the server™s logger (as specified in the error_log directive), to an
email address, or to a file.


string set_error_handler (callback error_handler)

In scripts in which errors must be handled by a custom function,
set_error_handler() enables you to specify that function. All error messages (as
controlled by error_reporting()) are sent to the specified function.

trigger_error() and user_error()

void trigger_error (string error_msg [, int error_type])
void user_error (string error_msg [, int error_type])

These two functions are synonymous. Both send a user-level error message.

Output buffering
Output buffering is the process of writing the results of your script to a temporary
buffer. Instead of being sent out over the Web the results will gather in a buffer,
where you can manipulate them if you wish.
Probably the most common use of output buffering is to ensure that you don™t
get errors caused by sending headers after text has been sent to the browser. To pre-
vent this from happening you can start a buffer, write some of an HTML page to the
buffer, and then, given a specific condition, write a header (maybe a cookie), and
then output the rest of the page. When you flush the buffer, the contents will be
written to the browser without error.

If you are frequently using buffering to prevent headers from causing
errors, rethink your page logic. Decisions first, output second.

People have also been playing with using output buffering to gzip page con-
tents. In browsers that are capable of unzipping, the page can be downloaded a lot
faster this way. However, given browser craziness, we don™t recommend this.



. 35
( 132 .)