[PHP] Thumbnails
Här är en klass jag satt ihop som skapar thumbnails. (Kräver GD.)
<?php
// PHP ThumbClass creates thumbnails for PHP
// Copyright (C) 2005 Simon Pantzare
//
// PHP ThumbClass 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.
//
// PHP ThumbClass is distributed in the hope that it will 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 for more details.
//
// You should have received a copy of the GNU General Public License
// along with PHP ThumbClass; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class ThumbClass {
var $img;
// options
var $img_dir = './img/';
var $thumb_dir = './img/thumbs/';
var $jpeg_quality = 80;
var $m_width = 100;
var $m_height = 75;
function ThumbClass( $image ) {
$this->img = strtolower( $image );
// $this->create(); // uncomment this line if you want to create a thumbnail automatically
}
function create() {
$filename = $this->img_dir . $this->img;
$file_ending = explode( '.', $this->img );
list($width_orig, $height_orig) = getimagesize($filename); // get new dimensions
if( $this->m_width && ( $width_orig < $height_orig ) ) {
$this->m_width = ( $this->m_height / $height_orig ) * $width_orig;
} else {
$this->m_height = ( $this->m_width / $width_orig ) * $height_orig;
}
// resample
$image_p = imagecreatetruecolor( $this->m_width, $this->m_height );
if( preg_match( "/jpg|jpeg/", $file_ending[1] ) ) { $image = imagecreatefromjpeg( $filename ); }
if( preg_match( "/png/",$file_ending[1] ) ) { $image = imagecreatefrompng( $filename ); }
imagecopyresampled( $image_p, $image, 0, 0, 0, 0, $this->m_width, $this->m_height, $width_orig, $height_orig );
// output
if( preg_match( "/png/",$file_ending[1] ) )
{
imagepng( $image_p, $this->thumb_dir . $this->img );
} else {
imagejpeg( $image_p, $this->thumb_dir . $this->img, $this->jpeg_quality );
}
}
function delete() {
unlink( $this->thumb_dir . $this->img );
}
}
$thumbnail = new ThumbClass( 'my_pic.png' );
// $thumbnail = new ThumbClass( 'my_pic.jpg' );
$thumbnail->create(); // this will create a thumbnail of img/my_pic.png in img/thumbs/
// $thumbnail->delete(); // uncomment this line to delete img/thumbs/my_pic.png
?>
Använd den som du behagar
Posta gärna förslag på förbättringar.
"'We're pro-life.' Eww, you look it! You look like you're filled with life."
UNIX man pages online, GNU/Linux-schemaprogram för LiU