Typical thumbnails generated by php. A php generated thumbnail code found on a foreign website. if you need it, refer to it. The code is as follows: Copy the code? Php ** File: SimpleImage. php * Author: SimonJarvis * C a php generated thumbnail code found on a foreign website. For more information, see.
The code is as follows: |
|
/* * File: SimpleImage. php * Author: Simon Jarvis * Copyright: 2006 Simon Jarvis * Date: 08/11/06 * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php * * This program is free software; you can redistribute it and/or * Modify it under the terms of the GNU General Public License * As published by the Free Software Foundation; either version 2 * Of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * But without any warranty; without even the implied warranty * MERCHANTABILITY or fitness for a special PURPOSE. See * GNU General Public License for more details: * Http://www.gnu.org/licenses/gpl.html * */ Class SimpleImage { Var $ image; Var $ image_type; Function load ($ filename ){ $ Image_info = getimagesize ($ filename ); $ This-> image_type = $ image_info [2]; If ($ this-> image_type = IMAGETYPE_JPEG ){ $ This-> image = imagecreatefromjpeg ($ filename ); } Elseif ($ this-> image_type = IMAGETYPE_GIF ){ $ This-> image = imagecreatefromgif ($ filename ); } Elseif ($ this-> image_type = IMAGETYPE_PNG ){ $ This-> image = imagecreatefrompng ($ filename ); } } Function save ($ filename, $ image_type = IMAGETYPE_JPEG, $ compression = 75, $ permissions = null ){ If ($ image_type = IMAGETYPE_JPEG ){ Imagejpeg ($ this-> image, $ filename, $ compression ); } Elseif ($ image_type = IMAGETYPE_GIF ){ Imagegif ($ this-> image, $ filename ); } Elseif ($ image_type = IMAGETYPE_PNG ){ Imagepng ($ this-> image, $ filename ); } If ($ permissions! = Null ){ Chmod ($ filename, $ permissions ); } } Function output ($ image_type = IMAGETYPE_JPEG ){ If ($ image_type = IMAGETYPE_JPEG ){ Imagejpeg ($ this-> image ); } Elseif ($ image_type = IMAGETYPE_GIF ){ Imagegif ($ this-> image ); } Elseif ($ image_type = IMAGETYPE_PNG ){ Imagepng ($ this-> image ); } } Function getWidth (){ Return imagesx ($ this-> image ); } Function getHeight (){ Return imagesy ($ this-> image ); } Function resizeToHeight ($ height ){ $ Ratio = $ height/$ this-> getHeight (); $ Width = $ this-> getWidth () * $ ratio; $ This-> resize ($ width, $ height ); } Function resizeToWidth ($ width ){ $ Ratio = $ width/$ this-> getWidth (); $ Height = $ this-> getheight () * $ ratio; $ This-> resize ($ width, $ height ); } Function scale ($ scale ){ $ Width = $ this-> getWidth () * $ scale/100; $ Height = $ this-> getheight () * $ scale/100; $ This-> resize ($ width, $ height ); } Function resize ($ width, $ height ){ $ New_image = imagecreatetruecolor ($ width, $ height ); Imagecopyresampled ($ new_image, $ this-> image, 0, 0, 0, 0, $ width, $ height, $ this-> getWidth (), $ this-> getHeight ()); $ This-> image = $ new_image; } } ?> |
Usage
Save the above file as SimpleImage. php and take a look at the following examples of how to use the script.
The first example below will load a file named picture.jpg resize it to 250 pixels wide and 400 pixels high and resave it as picture2.jpg
The code is as follows: |
|
Include ('simpleimage. php '); $ Image = new SimpleImage (); $ Image-> load('picture.jpg '); $ Image-> resize( 250,400 ); $ Image-> save('picture2.jpg '); ?> |
If you want to resize to a specifed width but keep the dimensions ratio the same then the script can work out the required height for you, just use the resizeToWidth function
The code is as follows: |
|
Include ('simpleimage. php '); $ Image = new SimpleImage (); $ Image-> load('picture.jpg '); $ Image-> resizeToWidth (250 ); $ Image-> save('picture2.jpg '); ?> |
Bytes. The code is as follows? Php/** File: SimpleImage. php * Author: Simon Jarvis * C...