Simple PHP thumbnails generation function
A simple thumbnail generation function supports image formats such as gif, jpeg, png, and bmp. it supports scaling to a fixed size or proportional scaling. it supports directly inputting the image to a browser or saving the image to a file.
- /**
- * Simple thumbnails generation function (supported Image formats: gif, jpeg, png, and bmp)
- * @ Authorxiaoshuoit@163.com
- * @ Paramstring $ src source image path
- * @ Paramint $ width the width of the thumbnail (proportional scaling is performed only when the height is specified)
- * @ Paramint $ width the height of the thumbnail (proportional scaling is performed only when the width is specified)
- * @ Paramstring $ filename storage path (output directly to the browser when 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, $ 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", 100, null, 'IMG/example_thumb.jpg ');
|