tailieunhanh - Giải pháp thiết kế web động với PHP - p 26
GENERATING THUMBNAIL IMAGES • • imagepng() imagegif() Each function takes as its first two arguments: the image resource and the path to where you want to save it. The imagejpeg() and imagepng() functions take an optional third argument to set the image quality. For imagejpeg(), you set quality by specifying a number in the range of 0 (worst) to 100 (best). If you omit the argument, the default is 75. For imagepng(), the range is 0 to 9. Confusingly, 0 produces the best quality (no compression). Finally, once you have saved the thumbnail, you need to destroy the image resources by passing them. | GENERATING THUMBNAIL IMAGES imagepng imagegif Each function takes as its first two arguments the image resource and the path to where you want to save it. The imagejpeg and imagepng functions take an optional third argument to set the image quality. For imagejpeg you set quality by specifying a number in the range of 0 worst to 100 best . If you omit the argument the default is 75. For imagepng the range is 0 to 9. Confusingly 0 produces the best quality no compression . Finally once you have saved the thumbnail you need to destroy the image resources by passing them to imagedestroy . In spite of its destructive name this function has no effect on the original image or the thumbnail. It simply frees the server memory by destroying the image resources required during processing. PHP Solution 8-4 Generating the thumbnail image This PHP Solution completes the Ps2_Thumbnail class by creating the image resources copying the thumbnail and saving it in the destination folder. Continue working with your existing class definition. Alternatively use in the ch08 folder. 1. The image resource for the original image needs to be specific to its MIME type so start by creating an internal method to select the correct type. Add the following code to the class definition. It s a protected method so put it at the bottom of the page but inside the class s closing curly brace . protected function createImageResource if this- _imageType jpeg return imagecreatefromjpeg this- _original elseif this- _imageType png return imagecreatefrompng this- _original elseif this- _imageType gif return imagecreatefromgif this- _original The checkType method that you created in PHP Solution 8-1 stores the MIME type as jpeg png or gif. So the conditional statement checks the MIME type matches it to the appropriate function and passes the original image as an argument. The method then returns the resulting image resource. 2. Now it s time to define the internal method that does all the .
đang nạp các trang xem trước