Plug in PHP 100 POWER SOLUTIONS- P73 ppsx

5 183 0
Plug in PHP 100 POWER SOLUTIONS- P73 ppsx

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

Thông tin tài liệu

326 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 326 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 "happiness. That to secure these rights, governments " . "are instituted among men, deriving their just powers " . "from the consent of the governed."; $from = 'English'; $to = 'German'; echo "<b>Original</b>: $text<br /><br />"; echo "<i>Translated from $from to $to:</i><br /><br />"; $result = PIPHP_GoogleTranslate($text, $from, $to); if (!$result) echo "Translation failed."; else echo "<b>Translation</b>: $result"; Because many languages use accented and other unusual characters, the translated text may require the correct UTF-8 encoding to be in place to ensure it displays correctly. In the preceding example, this is done with the tag: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> Some text is then assigned to $text, the two languages to translate from and to are stored in $from and $to, and after a little introductory text, the plug-in is called and its result is placed in $result. Upon failure, an error message is displayed, otherwise the translation is output. Rather than rely on Google’s API every time a non-English user visits your web site, I recommend you cache the first of each translation and serve the copy up on future visits by users of that language. It will keep your web site running fast and keep Google happy. The Plug-in function PIPHP_GoogleTranslate($text, $lang1, $lang2) { $langs = array( 'arabic' => 'ar', 'bulgarian' => 'bg', 'simplified chinese' => 'zh-cn', 'traditional chinese' => 'zh-tw', 'croatian' => 'hr', 'czech' => 'cs', 'danish' => 'da', 'dutch' => 'nl', 'english' => 'en', 'finnish' => 'fi', 'french' => 'fr', 'german' => 'de', 'greek' => 'el', 'hindi' => 'hi', 'italian' => 'it', 'japanese' => 'ja', 'korean' => 'ko', 'polish' => 'pl', 'portuguese' => 'pt', 'romanian' => 'ro', 'russian' => 'ru', 'spanish' => 'es', 'swedish' => 'sv'); C h a p t e r 1 2 : D i v e r s e S o l u t i o n s 327 C h a p t e r 1 2 : D i v e r s e S o l u t i o n s 327 $lang1 = strtolower($lang1); $lang2 = strtolower($lang2); $root = 'http://ajax.googleapis.com/ajax/services'; $url = $root . '/language/translate?v=1.0&q='; if (!isset($langs[$lang1]) || !isset($langs[$lang1])) return FALSE; $json = @file_get_contents($url . urlencode($text) . '&langpair='. $langs[$lang1] . '%7C' . $langs[$lang2]); if (!strlen($json)) return FALSE; $result = json_decode($json); return $result->responseData->translatedText; } Corner Gif Displaying content in a table with rounded borders can make it look more professional, but usually you need to create a different set of images to achieve this for each color palette used. This plug-in solves the problem by generating the GIF images needed on the fly, as you can see in Figure 12-9, which shows the top-left corner of a table (enlarged) as returned by the plug-in. About the Plug-in This plug-in creates corner and edge GIFs for building a table with rounded borders. Upon success, it returns a GD image containing the constructed GIF. On failure, it returns an unknown value or an unknown image. It requires the following arguments: • $corner An identifier for the image to create, out of: tl, t, tr, l, r, bl, b, and br for top-left, top, top-right, left, right, bottom-left, bottom, and bottom-right • $border The color of the border as a six-digit hexadecimal number • $bground The color of the background as a six-digit hexadecimal number FIGURE 12-9 A top-left corner GIF for a table (shown enlarged) as created by this plug-in 98 328 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 328 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 Variables, Arrays, and Functions $data Array containing a pixel map for the image $image GD image to be returned $bcol GD background color $fcol GD foreground color $tcol GD transparent color PIPHP_GD_FN1() PHP function to convert a six-digit hex number to a GD color How It Works When a corner GIF is required, this plug-in uses the pre-populated array in $data to create the top-left hand GIF and then rotates it if necessary. It does this by creating a new GD image in $image using the imagecreatetruecolor() function, and then creating three colors to use, $bcol, $fcol, and $tcol for the background, foreground, and transparent colors, as passed in the arguments $border and $bground. The image is then filled with the transparent color, ready for the main colors. The if (strlen($corner) == 2) statement simply checks whether a corner piece has been requested by seeing whether $corner has one or two letters. If it’s two, then a corner is wanted because $corner must contain one of the strings tl, tr, bl, or br, and so each of the pixels in the image that match those in the $data array is populated with either $bcol or $fcol, depending on whether the array has a value of 1 or 2, with a 0 indicating that a pixel should be left alone as it will be transparent. If $corner has only one letter, then it must contain one of the strings t, l, r, or b, so an edge piece was requested and therefore two rectangles are created in the background and foreground colors. Actually, the first rectangle is a line and represents the border, while the other fills in the rest of the area with the background color. Next a switch statement looks at the type of image that was requested in $corner, and if necessary, rotates the image before it is returned, with returned images being typically no more than about 50 bytes. How to Use It This plug-in is best used to create a self-contained program to return a GIF image, which is what the following code does: $corner = $_GET['c']; $border = $_GET['b']; $bground = $_GET['f']; $result = PIPHP_CornerGif($corner, $border, $bground); if ($result) { header('Content-type: image/gif'); imagegif($result); } This code accepts three GET arguments: c, b, and f for corner, border, and fill. It then passes these to the plug-in, and if an image is successfully returned, the correct header to C h a p t e r 1 2 : D i v e r s e S o l u t i o n s 329 C h a p t e r 1 2 : D i v e r s e S o l u t i o n s 329 preface sending of a GIF image is output, followed by sending the image in GIF format by calling the imagegif() function. The preceding example code will be used by the plug-in 99, Rounded Table, so type it into a new program file, and then also add the plug-in code below to it, and save the result as corner.php, ensuring you also include the opening <?php and closing ?> tags. Alternatively, you can download plug-ins.zip from the companion web site at www.pluginphp.com, and will find corner.php in the /12 folder. A typical call to the program will then look like the following, which will result in the image displayed in Figure 12-9 (if you enlarge it): corner.php?c=tl&b=444444&f=dedede Here a top-left corner has been requested by the parameter c=tl, the background color has been set to 444444 by the parameter b=444444, and the foreground color has been set to dedede by the parameter f=dedede. Remember to also paste in or otherwise include the function PIPHP_GD_FN1(), which this plug-in relies on. The Plug-in function PIPHP_CornerGif($corner, $border, $bground) { $data = array(array(0, 0, 0, 0, 0), array(0, 0, 0, 1, 1), array(0, 0, 1, 2, 2), array(0, 1, 2, 2, 2), array(0, 1, 2, 2, 2)); $image = imagecreatetruecolor(5, 5); $bcol = PIPHP_GD_FN1($image, $border); $fcol = PIPHP_GD_FN1($image, $bground); $tcol = PIPHP_GD_FN1($image, 'ffffff'); imagecolortransparent($image, $tcol); imagefill($image, 0 , 0, $tcol); if (strlen($corner) == 2) { for ($j = 0 ; $j < 5 ; ++$j) { for ($k = 0 ; $k < 5 ; ++ $k) { switch($data[$j][$k]) { case 1: imagesetpixel($image, $j, $k, $bcol); break; case 2: imagesetpixel($image, $j, $k, $fcol); break; } } } } 330 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 330 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 else { imagefilledrectangle($image, 0, 0, 4, 0, $bcol); imagefilledrectangle($image, 0, 1, 4, 4, $fcol); } switch($corner) { case 'tr': case 'r': $image = imagerotate($image, 270, $tcol); break; case 'br': case 'b': $image = imagerotate($image, 180, $tcol); break; case 'bl': case 'l': $image = imagerotate($image, 90, $tcol); break; } return $image; } function PIPHP_GD_FN1($image, $color) { return imagecolorallocate($image, hexdec(substr($color, 0, 2)), hexdec(substr($color, 2, 2)), hexdec(substr($color, 4, 2))); } Rounded Table With this plug-in, not only do you get the GIFs needed to create rounded table corners in any colors, you also get the HTML code, too. Figure 12-10 shows it being used to display some monologue from a Shakespeare play to good effect. FIGURE 12-10 All the corners of this table have been neatly rounded. 99 . Table, so type it into a new program file, and then also add the plug- in code below to it, and save the result as corner .php, ensuring you also include the opening < ?php and closing ?> tags can download plug- ins.zip from the companion web site at www.pluginphp.com, and will find corner .php in the /12 folder. A typical call to the program will then look like the following, which will. parameter f=dedede. Remember to also paste in or otherwise include the function PIPHP_GD_FN1(), which this plug- in relies on. The Plug- in function PIPHP_CornerGif($corner, $border, $bground)

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

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

Tài liệu liên quan