Simple generation of thumbnail functions, support image format: GIF, JPEG, PNG and BMP, support zoom to fixed size or zoom, support direct input to browser or save to file.
- /**
- * Simple generation of thumbnail functions (supported image formats: GIF, JPEG, PNG, and BMP)
- * @authorxiaoshuoit @163.com
- * @paramstring $src Source picture path
- * @paramint $width thumbnail width (equal to zoom only when height is specified)
- * @paramint $width thumbnail height (equal to zoom only when width is specified)
- * @paramstring $filename save path (direct output to browser if not specified)
- * @returnbool
- */
- function Simple_thumb ($src, $width = null, $height = NULL, $filename = null) {
- if (!isset ($width) &&!isset ($height))
- return false;
- if (Isset ($width) && $width <= 0)
- return false;
- if (Isset ($height) && $height <= 0)
- return false;
- $size = getimagesize ($SRC);
- if (! $size)
- return false;
- List ($src _w, $src _h, $src _type) = $size;
- $SRC _mime = $size [' MIME '];
- Switch ($src _type) {
- Case 1:
- $img _type = ' gif ';
- Break
- Case 2:
- $img _type = ' jpeg ';
- Break
- Case 3:
- $img _type = ' png ';
- Break
- Case 15:
- $img _type = ' wbmp ';
- Break
- Default:
- return false;
- }
- if (!isset ($width))
- $width = $src _w * ($height/$src _h);
- if (!isset ($height))
- $height = $src _h * ($width/$src _w);
- $imagecreatefunc = ' Imagecreatefrom '. $img _type;
- $src _img = $imagecreatefunc ($SRC);
- $dest _img = Imagecreatetruecolor ($width, $height);
- Imagecopyresampled ($dest _img, $src _img, 0, 0, 0, 0, $width, $height, $src _w, $src _h);
- $imagefunc = ' image '. $img _type;
- if ($filename) {
- $imagefunc ($dest _img, $filename);
- } else {
- Header (' Content-type: '. $src _mime);
- $imagefunc ($dest _img);
- }
- Imagedestroy ($src _img);
- Imagedestroy ($dest _img);
- return true;
- }
- Simple_thumb ("Http://www.baidu.com/img/bdlogo.gif", 100);
- Simple_thumb ("img/example.jpg", +, NULL, ' img/example_thumb.jpg ');
Copy Code |