DeFlup
Legacy Member
'k had al eens een thumbnail generator gemaakt, maar niet in een class. En met dat'k in de week is een tutorial over classes had bekeken heb 'k beslist om der een class van te maken. Wat ik van jullie vraag is of je mn class eens wilt bekijken en me eventueel kan zeggen wat ik moet verbeteren of wat ik kan toevoegen.
Je kan de code ook hier bekijken Link
dank bij voorbaat
PHP:
<?
/* Thumbnail Class
Created by Flupke <[email protected]>
Usage:
** Create new thumbnail
$thumb = new thumbnail();
** Setting the file that has to be resized
$thumb->set_original_file("image.jpg");
** Setting the size
$thumb->set_size(400,200);
** Setting the size (if you use this the class will determine wether it will use the parameter for the height or the width, that
depends on the size of the image
$thumb->set_autosize(200);
** Setting the quality (1-100)
Setting the quality can be used to change the filesize of the thumbnail.
$thumb->set_quality(75);
** Setting the output file
$thumb->set_output_file("thumb_image.jpg");
** Save the thumbnail
$thumb->save_thumbnail();
*/
define('THUMBNAIL_STANDARD_QUALITY', 75);
class thumbnail {
var $img;
function set_original_file ($filename) {
// Check if file exists
if ( !file_exists($filename) ) {
echo "<b>[Error]</b>: File was not found";
exit();
}
// Lookup imagefile extension
$this->img['format']= strtoupper(ereg_replace(".*\.(.*)$","\\1",$filename));
// Opening a new file
switch ( $this->img['format'] ) {
case "JPEG":
case "JPG": {
$this->img['src'] = imagecreatefromjpeg($filename);
break;
}
case "PNG": {
$this->img['src'] = imagecreatefrompng($filename);
break;
}
case "GIF": {
$this->img['src'] = imagecreatefromgif($filename);
break;
}
case "WMBP": {
$this->img['src'] = imagecreatefromwmbp($filename);
break;
}
default: {
echo "<b>[Error]</b>: Unsupported extension";
exit();
}
}
// Lookup image size
list($this->img['width'], $this->img['height'], $this->img['type'], $this->img['attr']) = getimagesize($filename);
}
function set_quality ($quality) {
// Set quality
if ( $quality < 100 || $quality <= 1 ) $quality = THUMBNAIL_STANDARD_QUALITY;
$this->img['quality'] = $quality;
}
function set_size ($width, $height) {
// Check if the sizes are proper values
if ( $width <= 0 || $width == '' || $height <= 0 || $height == '' ) {
echo "<b>[Error]</b>: You didn't specify a proper width or height";
exit();
}
else {
$this->img['new_width'] = $width;
$this->img['new_height'] = $height;
// Disable autosize
$this->img['autosize'] = '';
}
}
function set_autosize($size) {
// Checking size
if ( $size <= 0 || $size == "" ) {
echo "<b>[Error]</b>: You didn't specify a proper size";
exit();
}
else {
$this->img['autosize'] = $size;
// Disable own size
$this->img['new_width'] = '';
$this->img['new_height'] = '';
}
}
function set_output_file ($filename) {
// Lookup imagefile extension
$this->img['new_format']= strtoupper(ereg_replace(".*\.(.*)$","\\1",$filename));
// Opening a new file
switch ( $this->img['new_format'] ) {
case "JPG":
case "JPEG":
case "PNG":
case "GIF":
case "WMBP":
break;
default: {
echo "<b>[Error]</b>: Unsupported extension";
exit();
}
}
// Setting output file
$this->img['dest_file'] = $filename;
}
function save () {
// Checking the sizes
if ( $this->img['new_width'] == '' && $this->img['new_height'] == '' && $this->img['autosize'] == '' ) {
echo "<b>[Error]</b>: You forgot to specify the size of the thumbnail";
exit();
}
// Checking input file
elseif ( $this->img['format'] == '' ) {
echo "<b>[Error]/b>: You forgot to specify an input filename";
exit();
}
// Checking output file
elseif ( $this->img['new_format'] == '' ) {
echo "<b>[Error]/b>: You forgot to specify an output filename";
exit();
}
else {
// Determine size
if ( $this->img['autosize'] != '' ) {
if ( $this->img['width'] >= $this->img['height'] ) {
$this->img['thumb_width'] = $this->img['autosize'];
$this->img['thumb_height'] = ($this->img['autosize']/$this->img['width'])*$this->img['height'];
}
else {
$this->img['thumb_height'] = $this->img['autosize'];
$this->img['thumb_width'] = ($this->img['autosize']/$this->img['height'])*$this->img['width'];
}
}
else {
$this->img['thumb_height'] = $this->img['new_height'];
$this->img['thumb_width'] = $this->img['new_width'];
}
// Create empty image
$this->img['thumb'] = imageCreateTrueColor($this->img['thumb_width'],$this->img['thumb_height']);
// Copy image
imagecopyresampled($this->img['thumb'],$this->img['src'], 0, 0, 0, 0, $this->img['thumb_width'], $this->img['thumb_height'], $this->img['width'], $this->img['height']);
// Save image
switch ($this->img['new_format']) {
case "JPG":
case "JPEG": {
if ( $this->img['quality'] == '' ) $this->img['quality'] = THUMBNAIL_STANDARD_QUALITY;
imagejpeg($this->img['thumb'], $this->img['dest_file'], $this->img['quality']);
break;
}
case "GIF": {
imagegif($this->img['thumb'], $this->img['dest_file']);
break;
}
case "PNG": {
imagepng($this->img['thumb'], $this->img['dest_file']);
break;
}
case "WBMP": {
imagewbmp($this->img['thumb'], $this->img['dest_file']);
break;
}
}
// End image
imagedestroy($this->img['thumb']);
}
}
}
$test = new thumbnail();
$test->set_original_file("images/dSC00616.jpg");
$test->set_output_file("images/thumb_dSC006161.jpg");
$test->set_autosize(300);
$test->set_quality(75);
$test->save();
?>
Je kan de code ook hier bekijken Link
dank bij voorbaat

. Het is idd veel veiliger om een binaire controle (adhv de fileheader) te doen zoals in de getimagesize functie zit ingebakken ipv te vertrouwen op de extensie...
, want als php5 ingeburgerd geraakt zal je zowel je klasses al hebben als deftig het php5 oop model kunnen gebruiken.