or
Sign in to turn on 1-Click ordering.
or
Amazon Prime free trial required. Sign up when you check out. Learn more
More Buying Choices
Have one to sell? Sell yours here
or
Get a £0.25 Amazon.co.uk Gift Card
PHP in a Nutshell (In a Nutshell (O'Reilly))
 
 
Tell the Publisher!
I’d like to read this book on Kindle

Don't have a Kindle? Get your Kindle here, or download a FREE Kindle Reading App.

PHP in a Nutshell (In a Nutshell (O'Reilly)) [Paperback]

Paul Hudson
4.7 out of 5 stars  See all reviews (7 customer reviews)
RRP: £22.99
Price: £14.94 & this item Delivered FREE in the UK with Super Saver Delivery. See details and conditions
You Save: £8.05 (35%)
o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
In stock.
Dispatched from and sold by Amazon.co.uk. Gift-wrap available.
Only 2 left in stock--order soon (more on the way).
Want guaranteed delivery by Saturday, June 2? Choose Express delivery at checkout. See Details
‹  Return to Product Overview

Product Description

Review

"...there is no unnecessary waffle, but plenty of code to get your teeth into. It's a desktop guide for quick reference, so not all your queries will be answered, but it will certainly point you in the right direction. An indispensable guide to PHP programming." .net, May 2006

Product Description

Now installed on more than 20 million Internet domains around the world, PHP is an undisputed leader in web programming languages. Database connectivity, powerful extensions, and rich object-orientation are all reasons for its popularity, but nearly everyone would agree that, above all, PHP is one of the easiest languages to learn and use for developing dynamic web applications. The ease of development and simplicity of PHP, combined with a large community and expansive repository of open source PHP libraries, make it a favorite of web designers and developers worldwide.

PHP in a Nutshell is a complete reference to the core of the language as well as the most popular PHP extensions. This book doesn't try to compete with or replace the widely available online documentation. Instead, it is designed to provide depth and breadth that can't be found elsewhere. PHP in a Nutshell provides the maximum information density on PHP, without all the fluff and extras that get in the way. The topic grouping, tips, and examples in this book complement the online guide and make this an essential reference for every PHP programmer. This book focuses on the functions commonly used by a majority of developers, so you can look up the information you need quickly. Topics include:

  • Object-oriented PHP
  • Networking
  • String manipulation
  • Working with files
  • Database interaction
  • XML
  • Multimedia creation
  • Mathematics

Whether you're just getting started or have years of experience in PHP development, PHP in a Nutshell is a valuable addition to your desk library.

From the Publisher

PHP in a Nutshell is a complete reference to the core of the language as well as the most popular PHP extensions. The topic grouping, tips, and examples in this book make it an essential reference for every PHP programmer. It covers the functions commonly used by a majority of developers, so you can look up the information you need quickly. Whether you're just getting started or have years of experience in PHP development, PHP in a Nutshell is a valuable addition to your desk library.

About the Author

Paul Hudson, an avid PHP programmer, is Deputy Editor of the popular European Linux journal Linux Format, and author of the publication's PHP tutorial section. He is the author of Fedora 4 Unleashed and of the online book Practical PHP Programming available at http://www.hudzilla.org.

Excerpted from PHP in a Nutshell by Paul Hudson. Copyright © 2005. Reprinted by permission. All rights reserved.

Chapter 16 Manipulating Images

Lots of people stereotype PHP as only being suitable for outputting text, but that ’s not true —you can use PHP to create complex and dynamic pictures using the GD image extension. This chapter covers many of the GD functions that will allow you to make your own images for your site, either from scratch or by using existing images.

For image manipulation purposes,PHP ships with its own copy of the popular GD library. You used to have to get your own copy of GD and hope it was compatible with your PHP version.This is no longer the case. The copy of GD that ships with PHP will work with that version of PHP.

Getting Started

An important PHP function when working with images is header().This outputs a HTTP header of your choice;in this situation,we will be sending the content-type header,which tells web browsers what kind of content they can expect through the connection.Popular content types include text/plain for plain text documents;text/html for most web pages;and image/*,where the *is png ,jpeg , gif ,or MIME types for other picture formats.

As header()sends HTTP headers,it must be used before you send any content through. This is a core HTTP rule —no headers can be sent after content.This is the same thing that stops you from using cookies after you have sent content. The header()function is covered in more detail in Chapter 20,but for now,we will just work with this one aspect of it.

Creating a new image is done with the imagecreate()function,which has two parameters: the height and width of the image you wish to create.This will return false if it failed to create an image,which is usually the result of a lack of memory; otherwise,it will return the image as a resource for you to use in other image functions. To free up this image ’s memory, pass that resource into imagedestroy() as its only parameter.

Once you have your image resource,it is yours to play with all you want.PHP provides a selection of functions for you to use to manipulate the image.When you are done,you just choose your output format and the picture is finished.

To output the picture,you call one of several functions.If you want to convert it to PNG format,you call imagepng().This function takes two parameters,which are the image resource to use and a filename to save the picture as (optional).If you don ’t provide the second parameter,imagepng()sends the PNG-formatted picture straight to output, which is usually a visitor to your site.

To choose JPEG,you call the imagejpeg()function,which takes three parameters —the same two as imagepng(),plus the quality you wish to use for the picture. The quality,a number between 0 (lowest quality,smallest file)and 100 (highest quality,largest file),is optional,as is the filename parameter.If you want to set the quality without specifying a filename,just provide an empty string (‘’)as the filename.

The most basic image script looks like this:

$image =imagecreate(400,300);
//do stuff to the image
imagejpeg($image,'',75);
imagedestroy($image);

Save that as picture1.php .As most of your pictures will probably be referenced from a web page,we will also make a companion web page.Save this as phppicture.html :

PHP Art

PHP woz 'ere:

Open up your web browser and load in phppicture.html —you should see a largeblack box for the image,as shown in Figure 16-1.

Be sure not to have anything outside the PHP code block, not even an empty line or a space. Everything outside the PHP block is sent to the browser as part of the picture, and even having a single space character at the end of the file will cause problems.

The next step is to add a little color in place of the "do stuff to the image " comment, so we need imagecolorallocate()(note that you must use U.S.spellings for these function names).This new function takes four parameters:the image resource you are choosing a color for,then three integers between 0 and 255 —one each for the red value, then green value,and the blue value of the color. You can also specify these colors in hexadecimal format (e.g.,0xff)rather than decimal.

The first color you allocate is automatically used as the background color for your image,so this next piece of code is a minor modification of the last script to include color information:

$image =imagecreate(400,300);
$gold =imagecolorallocate($image,255,240,00);
imagepng($image);
imagedestroy($image);

Save that over picture1.php ,and refresh phppicture.html —you should see the black square replaced by a yellow square.

Don ’t worry about deallocating colors,as they are just numbers and not resources,meaning they don ’t use up any special memory. If you really want to deallocate a color (perhaps if you ’re workingwith a paletted image),use the imagecolordeallocate()function.

Choosing a Format

For high-quality images with many colors or a lot of detail,the JPEG format is preferred.JPEG saves in true color and allows you to set the compression ratio in order to get the best trade-off between size and quality.PNGs,on the other hand,work best as a replacement for GIFs,and as such,work well using limited colors. They also offer alpha transparency and quite small file sizes.

So,put as simply as possible:for photographs,prefer JPEGs,and for everything else,prefer PNGs.Just as an aside,and at the risk of starting a flame war, the colorcorrect pronunciations are "ping,""jay-peg,"and "jif."Note that WBMP is not Windows Bitmap,as you might have first thought —it stands for Wireless Bitmap and is designed for use in limited bandwidth situations.

‹  Return to Product Overview

Amazon.co.uk Privacy Statement Amazon.co.uk Delivery Information Amazon.co.uk Returns & Exchanges