From the foreign web site found a php generated thumbnail code, the need for friends can refer to.
The code is as follows |
Copy Code |
/* * 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 was 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 was distributed in the hope that it'll be useful, * but without any WARRANTY; Without even the implied warranty of * merchantability or FITNESS for A particular PURPOSE. See the * GNU general public License-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 for use the script.
The first example below would load a file named Picture.jpg resize it to + pixels wide and pixels high and resave it As Picture2.jpg
The code is as follows |
Copy Code |
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 and the script can work out the required Height for your, just use the Resizetowidth function
The code is as follows |
Copy Code |
Include (' simpleimage.php '); $image = new SimpleImage (); $image->load (' picture.jpg '); $image->resizetowidth (250); $image->save (' picture2.jpg '); ?> |
http://www.bkjia.com/PHPjc/632969.html www.bkjia.com true http://www.bkjia.com/PHPjc/632969.html techarticle from the foreign web site found a php generated thumbnail code, the need for friends can refer to. Code to copy code like this? PHP/* * File:SimpleImage.php * author:simon Jarvis * C ...