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

Plug in PHP 100 POWER SOLUTIONS- P22 pps

5 249 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 368,33 KB

Nội dung

C h a p t e r 4 : I m a g e H a n d l i n g 71 C h a p t e r 4 : I m a g e H a n d l i n g 71 case 10: imagefilter($image, IMG_FILTER_COLORIZE, 0, 128, 0, 50); break; case 11: imagefilter($image, IMG_FILTER_COLORIZE, 0, 0, 128, 50); break; case 12: imagefilter($image, IMG_FILTER_EDGEDETECT); break; case 13: imagefilter($image, IMG_FILTER_EMBOSS); break; case 14: imagefilter($image, IMG_FILTER_MEAN_REMOVAL); break; } return $image; } Image Crop This plug-in lets you crop a portion from an image by passing it as a GD image object, along with the top-left x and y coordinates, and the width and height to crop. Figure 4-5 shows a 285 × 214 pixel image, which has been cropped starting 100 pixels in from the left and 0 pixels from the top, with dimensions of 110 × 140 pixels. About the Plug-in This plug-in accepts a GD image from which a portion is to be cropped, along with details about the crop offset and dimensions. If any arguments are out of the image bounds, then FALSE is returned. It takes these arguments: � $image A GD image to be transformed � $x Offset from the left of the image FIGURE 4-5 Images are easily cut down to size using the Image Crop plug-in. 15 72 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 72 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 � $y Offset from the top of the image � $w The width to crop � $h The height to crop Variables, Arrays, and Functions $temp GD image copy of the cropped image $tw Integer containing the width of the passed image $th Integer containing the height of the passed image How It Works This plug-in works by creating a new GD image object of the dimensions supplied in $w and $h using the imagecreatetruecolor() function. This blank image is stored in $temp. Then, the imagecopyresampled() function is called, passing the required arguments to copy a portion of the image supplied in $image, starting at the offset $x pixels in and $y pixels down (and with a width and height of $w by $h), into the image held in $temp, which is then returned. How to Use It To crop a section out of an image, you need to first place the image in a GD image object and then call the PIPHP_ImageCrop() function with the required parameters, like this: $image = imagecreatefromjpeg("photo.jpg"); $copy = PIPHP_ImageCrop($image, 100, 0, 110, 140); if (!$copy) echo "Crop failed: Argument(s) out of bounds"; else imagejpeg($copy, "photo1.jpg"); This code creates a GD image object in $image by loading it in from the file photo. jpg using the imagecreatefromjpeg() function. Then, the plug-in is called with the top-left corner of the crop and the dimensions to use, the returned result of which is assigned to $copy. The cropped image is then saved as the file photo1.jpg using the imagejpeg() function. Note that arguments passed with values outside the image bounds will result in FALSE being returned so you can check for this and issue an appropriate message. To output the resulting cropped image to a browser, you can use the following code instead, which as long as there wasn’t an error, first sends the correct header: $image = imagecreatefromjpeg("photo.jpg"); $copy = PIPHP_ImageCrop($image, 100, 0, 110, 140); if ($copy != FALSE) { header("Content-type: image/jpeg"); imagejpeg(); } C h a p t e r 4 : I m a g e H a n d l i n g 73 C h a p t e r 4 : I m a g e H a n d l i n g 73 The Plug-in function PIPHP_ImageCrop($image, $x, $y, $w, $h) { $tw = imagesx($image); $th = imagesy($image); if ($x > $tw || $y > $th || $w > $tw || $h > $th) return FALSE; $temp = imagecreatetruecolor($w, $h); imagecopyresampled($temp, $image, 0, 0, $x, $y, $w, $h, $w, $h); return $temp; } Image Enlarge I’ve already covered a couple of image resizing plug-ins in this chapter, including Image Resize and Image Thumbnail. So, you may wonder why the need for yet another? The reason is that a standard enlargement, even if it resamples the original image rather than merely resizing the pixels, will still result in a pixelated blow up. And the more you enlarge an image, the more it will pixelate. For example, imagine increasing the size of an image by a factor of ten in each dimension; this results in the contents of every original pixel now occupying 100 pixels. Even with resampling the pixels nearby, this will still result in an exceedingly blocky enlargement with only the edges of each block of 100 pixels showing any differences. Now imagine resizing by just doubling each dimension, which results in the data from each original pixel only occupying four pixels. With resampling of the surrounding pixels, this new group of four will contain averaged values from similar pixels, and therefore pixelation will be minimized as the color and brightness information is spread out smoothly. And that’s how this plug-in works. To achieve a smoother enlargement it resamples an original image upwards just a little at a time, spreading the color and brightness smoothly at each enlargement until the desired final dimensions are reached. If you look closely at Figure 4-6, you’ll see that an original thumbnail of 100 × 75 pixels has been resampled in a single pass to 285 × 214 pixels, and that this eightfold increase in size has introduced substantial pixelation into the left-hand enlargement. The increase in size is calculated by multiplying each pair of dimensions together and then dividing the larger result by the smaller. Therefore, 100 × 75 is 7,500, and 285 × 214 is 60,990, and so 60,990 / 7,500 gives an enlargement amount of 8.132 times. The image on the right, however, was passed through the Image Enlarge plug-in and, as you can see from the insets, there is almost no pixelation. Instead, the blockiness has been replaced with even transitions of color and brightness. Of course, the image appears a little blurred, but what do you expect from creating picture data out of thin air? The new picture is eight times the size and therefore comprises over 85 percent made up (or interpolated) data. But this plug-in even gives you control over that because you can specify the amount of smoothing to apply to get just the right balance between pixelation and blurring. 16 74 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 74 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 About the Plug-in This plug-in accepts a GD image to enlarge, along with details about the new dimensions and amount of smoothing. It takes these arguments: � $image A GD image to be enlarged � $w The new width � $h The new height � $smoothing The amount of smoothing (0 = minimum, 90 = Maximum) Variables, Arrays, and Functions $oldw Integer representing the image’s current width $oldh Integer representing the image’s current height $step Float representing the amount of each enlargement $max Integer representing the number of steps to take $ratio Float representing the new width relative to the height $j Temporary counter to track iterations FIGURE 4-6 Even with resampling, enlarging a picture causes pixelation—but this plug-in helps reduce it, as shown by the zoomed-in insets. C h a p t e r 4 : I m a g e H a n d l i n g 75 C h a p t e r 4 : I m a g e H a n d l i n g 75 How It Works This plug-in first makes a note of the image’s current dimensions, placing them in $oldw and $oldh, and then calculates the step size between each of the enlargements. This is derived by multiplying the value of π (3.1415927) by the amount of smoothing required. You may ask, “Why this formula?” Well, I have to be honest here. I tried dozens of different step sizes until it occurred to me to enter π, and then the amount of smoothing increased substantially. Without being able to explain why, I suspect it has something to do with sines and cosines and the resampling routines used by the GD library. Anyway, armed with these values, a for loop then iterates through all the steps, enlarging the original image a little at a time by passing it to plug-in number 12, Image Resize. Because each step is a floating point number, the final image will be close to, but rarely exactly, the new dimensions required. Therefore, before returning the final enlargement, PIPHP_ImageResize() is called one last time to ensure the exact size needed is returned. How to Use It To enlarge an image with this plug-in, you must already have it stored as a GD image object, which you then pass to PIPHP_ImageEnlarge(), along with the new width and height, and a smoothing level, like this: $image = imagecreatefromjpeg("icon.jpg"); $image = PIPHP_ImageEnlarge($image, 285, 214, 15); imagejpeg($image, "enlarged.jpg"); Here the image icon.jpg is loaded into memory using imagecreatefromjpeg() and then passed to the plug-in, with requested new dimensions of 285 × 214 pixels and a smoothing level of 15. The returned enlargement is then saved using the filename enlarged.jpg with the imagejpeg() function. The enlargement could equally be output directly to the browser like this: $image = imagecreatefromjpeg("icon.jpg"); header("Content-type: image/jpeg"); imagejpeg(PIPHP_ImageEnlarge($image, 285, 214, 15)); Because the plug-in PIPHP_ImageResize() is called by this plug-in, you will need to ensure you have it already copied to, or included by, your program. CAUTION Because this plug-in requires multiple iterations of a time-intensive resampling function, it’s not recommended for on-the-fly conversion of images on a production server, and is much better suited for running as part of a background or housekeeping image management process, or for use on a personal PHP installation. The Plug-in function PIPHP_ImageEnlarge($image, $w, $h, $smoothing) { $oldw = imagesx($image); $oldh = imagesy($image); $step = 3.1415927 * ((100 - $smoothing) / 100); . of the cropped image $tw Integer containing the width of the passed image $th Integer containing the height of the passed image How It Works This plug- in works by creating a new GD image object. block of 100 pixels showing any differences. Now imagine resizing by just doubling each dimension, which results in the data from each original pixel only occupying four pixels. With resampling. then the amount of smoothing increased substantially. Without being able to explain why, I suspect it has something to do with sines and cosines and the resampling routines used by the GD library. Anyway,

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

TỪ KHÓA LIÊN QUAN