1. Trang chủ
  2. » Công Nghệ Thông Tin

Plug in PHP 100 POWER SOLUTIONS- P23 pptx

5 201 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 436,74 KB

Nội dung

76 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 76 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 $max = $w / $step; $ratio = $h / $w; for ($j = $oldw ; $j < $max; $j += $step) $image = PIPHP_ImageResize($image, $j * $step, $j * $step * $ratio); return PIPHP_ImageResize($image, $w, $h); } Image Display I’ve already shown you how to output a JPEG image directly to a browser by sending the correct header. But here’s a plug-in that will output any GIF, JPEG, or PNG image, and if it’s a JPEG or PNG, at whatever quality you choose to achieve the optimum balance between bandwidth use and image quality. For example, Figure 4-7 shows a JPEG image displayed by a PHP program at the default quality setting of 75. About the Plug-in This plug-in accepts a filename to display, the image type, and the quality required. It takes these arguments: � $filename A string containing the path/filename of an image � $type The file type of the image (either gif, jpeg, or png) � $quality The display quality if jpeg or png (0 = lowest, up to 99 = highest quality) FIGURE 4-7 Using this plug-in, you can display images in a variety of formats and quality settings. 17 C h a p t e r 4 : I m a g e H a n d l i n g 77 C h a p t e r 4 : I m a g e H a n d l i n g 77 Variables, Arrays, and Functions $contents Temporary copy of the image loaded from file $filetype Array containing details about the file $mime String containing the image’s type (such as “image/png”) $image GD image object created from $contents How It Works The first thing this plug-in does is load the contents of the file pointed to by $filename into the string variable $contents. Next, if the $type parameter hasn’t been given a value, the calling code wants the output type to remain unchanged, so it’s looked up by calling the imagegetsize() function and the result is saved in the array $filetype. The third element of this array is a string containing the mime file type, so that is extracted and placed in the variable $mime. The correct header is then output followed by the value in $contents, which is the unaltered original image. The die() function is used to send the image because it combines an echo and exit statement in one, so it’s more efficient. The rest of the code is only executed if $type has a value, specifying the output type. In this case, a GD image is created from the file stored in $contents using imagecreatefromstring(), and the chosen mime type header is sent to the browser. Next a switch statement tests $type to see whether it refers to a GIF, JPEG, or PNG image and calls the correct function to display it using either imagegif(), imagejpeg(), or imagepng(). If the file is a JPEG or a PNG file, then the quality setting is applied. For a JPEG, the value passed needs to be between 0 and 99, with 0 being worst and 99 the best. This is exactly how the imagejpeg function expects to receive this value so the value of $quality is passed as-is. But the imagepng() function requires a quality value between 0 and 9, where 0 is the best and 9 the worst, which is the inverse of the former and also one tenth of the value. Therefore, a quick formula is applied to $quality to conform. Using a lower quality setting results in the sent image being smaller with a corresponding saving in bandwidth, whereas a higher setting uses more bandwidth but results in better quality. How to Use It To display a file directly to a browser, just call the plug-in passing the filename, file type, and quality setting as in the following, which outputs a JPEG image in PNG format at a compression level of 50: PIPHP_ImageDisplay("pic.jpg", "png", 50); To display an image in its native format, you can omit the file type argument, as you can with the quality, by replacing these parameters with NULL: PIPHP_ImageDisplay("pic.jpg", NULL, NULL); 78 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 78 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 The Plug-in function PIPHP_ImageDisplay($filename, $type, $quality) { $contents = file_get_contents($filename); if ($type == "") { $filetype = getimagesize($filename); $mime = image_type_to_mime_type($filetype[2]); header("Content-type: $mime"); die($contents); } $image = imagecreatefromstring($contents); header("Content-type: image/$type"); switch($type) { case "gif": imagegif($image); break; case "jpeg": imagejpeg($image, NULL, $quality); break; case "png": imagepng($image, NULL, round(9 - $quality * .09)); break; } } Image Convert This plug-in is similar to the previous one, Image Display, but it saves the new image to disk. Wrapped in suitable code, it’s very handy for automatically changing the type (and quality) of images either singly or in batches. Figure 4-8 shows a 42Kb JPEG image that has been converted to another JPEG of only 8Kb by using a quality setting of 25. FIGURE 4-8 This plug-in converts images to JPEG, GIF, and PNG, and can change the quality setting, too. 18 C h a p t e r 4 : I m a g e H a n d l i n g 79 C h a p t e r 4 : I m a g e H a n d l i n g 79 The second image isn’t too discernibly degraded, but the conversion achieves an 80 percent saving on bandwidth. You can specify the quality setting yourself. If you increase the quality, the image will use more bandwidth. If the image quality is decreased it will use less bandwidth. But remember that you cannot increase the quality above that of the original image. About the Plug-in This plug-in accepts the name of a file to convert, the name of the file it should be saved as, and the quality required. It takes these arguments: � $fromfile String containing the path/filename of an image � $tofile String containing the path/filename to save the new image � $type The file type of the image (either gif, jpeg, or png) � $quality The image quality if JPEG or PNG (0 = lowest, up to 99 = highest quality) Variables, Arrays, and Functions $contents Temporary copy of the image loaded from the file $image GD image object created from $contents How It Works This plug-in loads in the contents of the image referred to by $fromfile into the string variable $contents, from where it creates a GD image object using the imagecreatefromstring() function. Then, a switch statement is used to check whether the new image type required is GIF, JPEG, or PNG, and accordingly calls the imagegif(), imagejpeg(), or imagepng() function, passing the value of $tofile, which holds the path and name of the file to save, and $quality, which describes the quality setting if the image is a JPEG or PNG. As with the previous plug-in, Image Display, the quality setting must be specially calculated for PNG files since the imagepng() function expects the compression setting in a different format than imagejpeg(). How to Use It To convert an image type, call PIPHP_ImageConvert() with the source and destination path and/or filenames, along with the type to convert to and the quality setting to use, as in the following line of code, which converts the image in photo.jpg to a PNG file, and saves it as photo.png, using a compression value of 50: PIPHP_ImageConvert("photo.jpg", "photo.png", "png", 50); The Plug-in function PIPHP_ImageConvert($fromfile, $tofile, $type, $quality) { $contents = file_get_contents($fromfile); 80 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 80 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 $image = imagecreatefromstring($contents); switch($type) { case "gif": imagegif($image, $tofile); break; case "jpeg": imagejpeg($image, $tofile, $quality); break; case "png": imagepng($image, $tofile, round(9 - $quality * .09)); break; } } Note that GIF images do not have a quality setting, so this value will make no difference to the resulting image. Also, to see the differences between before and after, make sure you reload any converted images into your browser so that previous unconverted images are not served up from the cache in place of the converted ones. Gif Text Although web browsers come with a reasonable range of default fonts, they don’t always provide the look you need for a particular web site. In such cases, you usually must resort to calling up a graphics editor and creating logos or headlines there. However, with this plug-in, all you have to do is upload the TrueType fonts you wish to use to your web site. You can then display text in these fonts by having the GD library convert it on the fly to GIF images. Figure 4-9 shows the text “Old English Font” displayed at four different sizes using an Old English TrueType font. FIGURE 4-9 Now you can use any fonts you like on your web pages thanks to this plug-in. 19 . file $filetype Array containing details about the file $mime String containing the image’s type (such as “image/png”) $image GD image object created from $contents How It Works The first thing this plug- in does. corresponding saving in bandwidth, whereas a higher setting uses more bandwidth but results in better quality. How to Use It To display a file directly to a browser, just call the plug- in passing. Works This plug- in loads in the contents of the image referred to by $fromfile into the string variable $contents, from where it creates a GD image object using the imagecreatefromstring() function. Then,

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

TỪ KHÓA LIÊN QUAN