Plug in PHP 100 POWER SOLUTIONS- P24 doc

5 138 0
Plug in PHP 100 POWER SOLUTIONS- P24 doc

Đang tải... (xem toàn văn)

Thông tin tài liệu

C h a p t e r 4 : I m a g e H a n d l i n g 81 C h a p t e r 4 : I m a g e H a n d l i n g 81 About the Plug-in This plug-in takes the name of a file to save as a finished GIF, the text and font to use in it, and various details such as color, size, and shadowing. It takes these arguments: � $file The path/filename to save the image � $text The text to create � $font The path/filename of the TrueType font to use � $size The font size � $fore The foreground color in hexadecimal (such as “000000”) � $back The background color (such as “FFFFFF”) � $shadow The number of pixels to offset a shadow underneath the text (0 = no shadow) � $shadowcolor The shadow color (such as “444444”) Variables, Arrays, and Functions $bound Array containing the boundaries required to make room for the text $width Integer containing the text width in pixels calculated from $bound $height Integer containing the text height in pixels calculated from $bound $image Temporary copy of the final image $bgcol The background color identifier created from $back $fgcol The foreground color identifier created from $fore $shcol The shadow color identifier created from $shadowcolor PIPHP_GD_FN1() Function to create color identifiers How It Works To create a GIF image of the correct dimensions to hold the text, the function imagettfbbox() is called with the font, its size, and the text to display as arguments. The result, which contains the x and y coordinates of all four corners, is then stored in the array $bound. Using these, the variables $width and $height are assigned values sufficiently large to accommodate the text and any shadow, as well as a few pixels space all around. Then, a new GD image is created in $image using this width and height. Next, three color identifiers are created in $bgcol, $fgcol, and $shcol using the string values supplied in $fore, $back, and $shadowcolor by calling the function PIPHP_GD_ FN1(), which takes a six-character hexadecimal string and converts it to a color identifier. These identifiers are unique to the $image object and are used to set colors in it. This function is just a helper function to the main plug-in and is not documented because it’s not intended to be called directly by any other code. With the colors prepared, the image is then filled with the background color using the imagefilledrectangle() function. 82 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 82 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s Next, if $shadow is greater than 0, then a shadow needs to be displayed so the imagettftext() function is called to display the text at an offset of two pixels along and two pixels down, and in the correct shadow color. After that, the code for adding the main text itself is called. This is the same as for the shadow text except that no offset is used and the text is created in the foreground color. Finally, the imagegif() function is called to save the finished image using the path/ filename stored in $file. How to Use It To use this plug-in, upload the TrueType file(s) you want to the same folder as the PHP program. In this case, it’s assumed that you have uploaded a font called oldenglish.ttf. You can then create a GIF containing text of your choice like this: PIPHP_GifText("greeting.gif", "Hello there!", "oldenglish.ttf", 26, "ff0000", "ffffff", 1, "444444"); To display the image you then only need to output some HTML code, like this: echo "<img However, to ensure the image is only created the first time it is needed, you will probably want to wrap the above code within an if statement, like this: if (!file_exists("greeting.gif")) { PIPHP_GifText("greeting.gif", "Hello there!", "oldenglish.ttf", 26, "ff0000", "ffffff", 1, "444444"); } The Plug-in function PIPHP_GifText($file, $text, $font, $size, $fore, $back, $shadow, $shadowcolor) { $bound = imagettfbbox($size, 0, $font, $text); $width = $bound[2] + $bound[0] + 6 + $shadow; $height = abs($bound[1]) + abs($bound[7]) + 5 + $shadow; $image = imagecreatetruecolor($width, $height); $bgcol = PIPHP_GD_FN1($image, $back); $fgcol = PIPHP_GD_FN1($image, $fore); $shcol = PIPHP_GD_FN1($image, $shadowcolor); imagefilledrectangle($image, 0, 0, $width, $height, $bgcol); if ($shadow > 0) imagettftext($image, $size, 0, $shadow + 2, abs($bound[5]) + $shadow + 2, $shcol, $font, $text); imagettftext($image, $size, 0, 2, abs($bound[5]) + 2, $fgcol, $font, $text); imagegif($image, $file); } C h a p t e r 4 : I m a g e H a n d l i n g 83 C h a p t e r 4 : I m a g e H a n d l i n g 83 function PIPHP_GD_FN1($image, $color) { return imagecolorallocate($image, hexdec(substr($color, 0, 2)), hexdec(substr($color, 2, 2)), hexdec(substr($color, 4, 2))); } Image Watermark In a similar way to creating GIF images of text, you can also overlay text on an existing image to create watermarks. With the amount of copying and pasting of images across the web, when you have one you would like to protect, sometimes watermarking is the best way. This plug-in provides a variety of options. For example, Figure 4-10 shows a photograph with the word Watermark overlaid in white at a transparency setting of 10 percent. About the Plug-in This plug-in takes the name of a file in which to save a finished GIF, the text and font to use, and various details such as color, size, and shadowing. It takes these arguments: � $fromfile The path/filename of the original image � $tofile The path/filename to save the image FIGURE 4-10 Now you don’t have to load your images into a graphics editor to add watermarks. 20 84 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 84 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s � $type One of gif, jpeg, or png � $quality Quality setting of final image (0 = worst, up to 99 = best) � $text The text to create � $font The path/filename of the TrueType font to use � $size The font size � $fore The foreground color in hexadecimal (such as “000000”) � $opacity The opacity of the watermark (0 = transparent, up to 100 = opaque) Variables, Arrays, and Functions $contents The image contents loaded in from $fromfile $image1 GD image object created from $contents $bound Array containing the boundaries required to make room for the text $width Integer containing the text width in pixels calculated from $bound $height Integer containing the text height in pixels calculated from $bound $image2 GD image object created to hold watermarking text $bgcol The background color identifier, from the string “fedcba” (see the How It Works section) $fgcol The foreground color identifier created from $fore PIPHP_GD_FN1() Function to create color identifiers How It Works This plug-in starts by loading the image referred to by $fromfile into $contents, from where it’s changed to a GD image object and stored in $image1. Then, the array $bound is populated with the result of calling imagettfbbox() to get the coordinates of all the corners needed to create a space big enough to store the watermark text. The width and height of this box are then extracted from $bound into $width and $height, with a few pixels leeway being left in all dimensions. Using this width and height, a new GD image object is created in $image2. Then, two color identifiers are created for the background and foreground colors in $bgcol and $fgcol. This is done using the function PIPHP_GD_FN1(), which is designed for use only by these plug-ins and is not intended to be called directly from your programs. Because the text for watermarking will be transparent, I selected a background color of “fedcba”, which is unlikely to be used as the foreground color. If you do need that as a foreground color, I’m sure you could get away with using “fedcb9” or “fedcbb”, and so on, instead. As I said, the background color must be transparent so it’s passed to the function imagecolortransparent(), and then the entire $image2 rectangle is filled in that color using imagefilledrectangle(). With the background canvas prepared, the text is then written to it in the foreground color, using the font and size specified. C h a p t e r 4 : I m a g e H a n d l i n g 85 C h a p t e r 4 : I m a g e H a n d l i n g 85 At this point the function now has two separate images—the original, and the watermark to add—so it calls the imagecopymerge() function to merge the watermark onto the original image, exactly in the middle, and with an opacity of $opacity. Finally, a switch statement is used to check the image type for being one of GIF, JPEG, or PNG and then calls either imagegif(), imagejpeg(), or imagepng() accordingly to save the image, using the path/filename in $tofile. If the type you wish to save it as is a PNG or JPEG, then the quality setting in $quality is also applied, although a little math is required to manipulate it into the correct form required for the imagepng() function. How to Use It To watermark an image, supply the function PIPHP_ImageWatermark() with the names of a source and destination file, the image type, and the parameters required for font, size, color, and transparency, like this: PIPHP_ImageWatermark("pic.jpg", "wmark.png", "png", 75, "Watermark", "oldenglish.ttf", 90, "ffffff", 10); Here the file pic.jpg is overlaid with a watermark containing the text “Watermark”, using the oldenglish.ttf font with a size of 90, a color of “ffffff” and an opacity value of 10. The file is saved as a PNG image, at a quality setting of 75, using the filename wmark.png. The Plug-in function PIPHP_ImageWatermark($fromfile, $tofile, $type, $quality, $text, $font, $size, $fore, $opacity) { $contents = file_get_contents($fromfile); $image1 = imagecreatefromstring($contents); $bound = imagettfbbox($size, 0, $font, $text); $width = $bound[2] + $bound[0] + 6; $height = abs($bound[1]) + abs($bound[7]) + 5; $image2 = imagecreatetruecolor($width, $height); $bgcol = PIPHP_GD_FN1($image2, "fedcba"); $fgcol = PIPHP_GD_FN1($image2, $fore); imagecolortransparent($image2, $bgcol); imagefilledrectangle($image2, 0, 0, $width, $height, $bgcol); imagettftext($image2, $size, 0, 2, abs($bound[5]) + 2, $fgcol, $font, $text); imagecopymerge($image1, $image2, (imagesx($image1) - $width) / 2, (imagesy($image1) - $height) / 2, 0, 0, $width, $height, $opacity); switch($type) . Functions $bound Array containing the boundaries required to make room for the text $width Integer containing the text width in pixels calculated from $bound $height Integer containing the text height in pixels. required to make room for the text $width Integer containing the text width in pixels calculated from $bound $height Integer containing the text height in pixels calculated from $bound $image2 GD. created in $image using this width and height. Next, three color identifiers are created in $bgcol, $fgcol, and $shcol using the string values supplied in $fore, $back, and $shadowcolor by calling

Ngày đăng: 07/07/2014, 08:20

Từ khóa liên quan

Mục lục

  • Contents

  • Acknowledgments

  • Introduction

  • 1 Building a Development Server

    • Windows XP, Windows Vista, and Windows 7

      • Reinstalling Zend Server CE

      • Upgrading Zend Server CE

      • Windows Security Alerts

      • After Installation

      • Uninstalling

      • Document Root

      • Ubuntu and Debian Linux

        • Uninstalling

        • After Installation

        • Document Root

        • Fedora, RHEL, and CentOS Linux

          • Installing MySQL

          • Uninstalling

          • Document Root

          • Other Versions of Linux

            • Installing MySQL

            • Uninstalling

            • Document Root

            • Mac OS X 10.4 Plus on Intel Chips

              • Document Root

              • Uninstalling

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan