Tải bản đầy đủ (.pdf) (5 trang)

Plug in PHP 100 POWER SOLUTIONS- P21 ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (328.45 KB, 5 trang )


66
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

66
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_ImageResize($image, $w, $h)
{
$oldw = imagesx($image);
$oldh = imagesy($image);
$temp = imagecreatetruecolor($w, $h);
imagecopyresampled($temp, $image, 0, 0, 0, 0,
$w, $h, $oldw, $oldh);
return $temp;
}
Make Thumbnail
Many thumbnail programs exist that will take a large image and reduce it to a thumbnail
for you, often supporting working in batches. But what about turning user uploaded
images into thumbnails? Obviously you don’t want to simply send a large image to the
browser and have HTML resize it since the quality wouldn’t be great, and your bandwidth
would go through the roof. So you need something to handle this process on the fly, which
is where this plug-in comes in handy.
With it you specify a source image and the maximum dimensions allowed for the new
thumbnail. The function will then resize the image, retaining the aspect ratio, so that
whichever of the height or width is the larger dimension is then set to the new maximum
size, and the other is reduced in proportion. Figure 4-3 shows the smiley face image, from
the previous plug-in, used as a thumbnail source for two smaller thumbnail images.
FIGURE 4-3 The Make Thumbnail plug-in has been used to make two different thumbnails of a smiley face.

13


C h a p t e r 4 : I m a g e H a n d l i n g
67
C h a p t e r 4 : I m a g e H a n d l i n g
67
About the Plug-in
This plug-in accepts an image to be converted into a thumbnail and the new maximum
width or height. It takes these arguments:
� $image A GD image to be transformed
� $max The new maximum width or height (whichever is the greater dimension)
Variables, Arrays, and Functions
$w
Integer representing the image’s current width
$h
Integer representing the image’s current height
$thumbw
Integer representing the thumbnail’s new width
$thumbh
Integer representing the thumbnail’s new height
How It Works
To create the new thumbnail image, this plug-in accepts a GD image object and then sets $w
and $thumbw to its width, and $h and $thumbh to its height. Next it looks at these values to
find out which dimension is the larger. If $w is greater than $h, then the image is wider than
it is high, so the new width will take the value in $max, and thus $thumbh, the smaller
thumbnail height, is set to the maximum dimension value of $max divided by the original
image’s width, in $w, and multiplied by its height, in $h.
So, for example, if the original image’s width is 1200 pixels, the height is 1000, and the
new maximum dimension size is 100 pixels, the following formula is applied:
Thumbnail Height = 100 / 1200 × 1000
This becomes:
Thumbnail Height = 0.0833 × 1000

Which results in:
Thumbnail Height = 83.33
Therefore, if the new width is to be 100 pixels, then the new height must be 83.33 pixels
(which will be rounded down to 83). Similarly, if the height is greater than the width, then
the height will be set to the value in $max and the width will be set to a percentage of that
height.
In both cases, a test is made to see whether $max is already smaller than the new height
or width, because if it’s not then the image is already of thumbnail size.
A final test is made for whether the value in $max is less than that in $h. If so this means
that the requested new image size is smaller than the original, and that the width and height
required are equal, so $thumbw and $thumbh are both set to the value in $max. In this case, the
thumbnail will be square and so both $thumbw and $thumbh are assigned the value in $max.
With all the calculations over, the previous plug-in, PIPHP_ImageResize(), is called
to perform the resizing. The image that is returned from this plug-in is returned to the
calling code.

68
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

68
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
How to Use It
To create a thumbnail, you pass the function PIPHP_MakeThumbnail() a GD image object
and the maximum value of the greater dimension for the thumbnail. For example, the
following code loads in the image in test.jpg using the imagecreatefromjpeg() function,
and then passes it to the plug-in, along with a maximum dimension of 100. The function
then returns the new thumbnail to the string variable $thumb, which is then saved to the file
thumb.jpg using the imagejpeg() function.
$image = imagecreatefromjpeg("test.jpg");
$thumb = PIPHP_MakeThumbnail($image, 100);

imagejpeg($thumb, "thumb.jpg");
You can also output the thumbnail straight to the browser by first sending the correct
header, like this:
$image = imagecreatefromjpeg("test.jpg");
header("Content-type: image/jpeg");
imagejpeg(PIPHP_MakeThumbnail($image, 100));
Because this plug-in also uses the PIPHP_ImageResize() function, you will need both
of these in (or included by) your program file.
The Plug-in
function PIPHP_MakeThumbnail($image, $max)
{
$thumbw = $w = imagesx($image);
$thumbh = $h = imagesy($image);

if ($w > $h && $max < $w)
{
$thumbh = $max / $w * $h;
$thumbw = $max;
}
elseif ($h > $w && $max < $h)
{
$thumbw = $max / $h * $w;
$thumbh = $max;
}
elseif ($max < $w)
{
$thumbw = $thumbh = $max;
}

return PIPHP_ImageResize($image, $thumbw, $thumbh);

}
Image Alter
The PHP GD library is so powerful that it can perform a variety of image manipulations you
would normally only find in a graphics program. In fact, you could probably build quite an
advanced image editor using them. This plug-in goes someway towards that by providing

14
C h a p t e r 4 : I m a g e H a n d l i n g
69
C h a p t e r 4 : I m a g e H a n d l i n g
69
14 different image transformations you can apply to your graphics, and Figure 4-4 shows just
one of these, Edge Detect, in use.
About the Plug-in
This plug-in accepts an image to be converted, along with the transformation required.
It takes these arguments:
� $image A GD image to be transformed
� $effect The transformation to apply, between 1 and 14:
$effect
Action
1 Sharpen
2 Blur
3 Brighten
4 Darken
5 Increase contrast
6 Decrease contrast
7 Grayscale
8 Invert
9 Increase red
10 Increase green

11 Increase blue
12 Edge detect
13 Emboss
14 Sketchify
FIGURE 4-4 This photograph has been modified by passing it through the Image Alter plug-in.

70
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

70
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
� None
How It Works
To select between the available transformation effects, the plug-in uses a large switch
statement that supports 14 different cases to apply to the supplied GD image object. It then
calls the relevant function with the required parameters and returns the new image.
How to Use It
To perform an Edge Detect transformation on a file called photo.jpg, as shown in Figure 4-4,
you could use the following code, which will load a GD image object using the
imagecreatefromjpeg() function, and save the transformed image with the function
imagejpeg(), using the filename photo2.jpg:
$image = imagecreatefromjpeg("photo.jpg");
$copy = PIPHP_ImageAlter($image, 12);
imagejpeg($copy, "photo2.jpg");
Or to output the transformed image directly to a browser, you could use the following
code to output the correct header first:
$image = imagecreatefromjpeg("photo.jpg");
header("Content-type: image/jpeg");
imagejpeg(PIPHP_ImageAlter($image, 12));

The Plug-in
function PIPHP_ImageAlter($image, $effect)
{
switch($effect)
{
case 1: imageconvolution($image, array(array(-1, -1, -1),
array(-1, 16, -1), array(-1, -1, -1)), 8, 0);
break;
case 2: imagefilter($image,
IMG_FILTER_GAUSSIAN_BLUR); break;
case 3: imagefilter($image,
IMG_FILTER_BRIGHTNESS, 20); break;
case 4: imagefilter($image,
IMG_FILTER_BRIGHTNESS, -20); break;
case 5: imagefilter($image,
IMG_FILTER_CONTRAST, -20); break;
case 6: imagefilter($image,
IMG_FILTER_CONTRAST, 20); break;
case 7: imagefilter($image,
IMG_FILTER_GRAYSCALE); break;
case 8: imagefilter($image,
IMG_FILTER_NEGATE); break;
case 9: imagefilter($image,
IMG_FILTER_COLORIZE, 128, 0, 0, 50); break;

×