thumbnailerator

phadej 21.09.04 03:51

tekee kansion gif png jpg kuvista pikkukuvia toiseen kansioon. ensimm'inen kevyt scripta tänne.

 Tekstiversio  Arvo: 2 (8 ääntä)  Äänestä: +  -
<?php

##################################################################################
# thumbnailerator 0.1                                                            #
# Author: Oleg Grenrus                                                           #
# Created: 2004-09-21                                                            #
# Description:                                                                   #
# Create png thumbnails from all png, jpeg and gif images in current directory   #
# Requiments:                                                                    #
# GD lib 2.x with GIF support                                                    #
##################################################################################

# CONFIG
$image_dir = '.';                     # image dir
$thumb_dir = 'thumbs';                # thumb dir. NOTICE NO SLASH
$thumb_max_size = 150;                # thumbnail side max size

# SCRIPT
# making thumb directory if not exists
if(file_exists($thumb_dir)){
    if(!is_dir($thumb_dir)){
        unlink($thumb_dir);
        mkdir($thumb_dir);
    }
} else {
    mkdir($thumb_dir);
}

$handle = @opendir($image_dir);
while($file = readdir($handle)){
    # file is file
    if(is_file($file)){

        # changes extension to png
        if(preg_match('#^(.+)\.(.+?)$#',$file,$matches)){
            $thumbnail = $thumb_dir . '/' . $matches[1] . '.png';
        }else{
            $thumbnail = $thumb_dir . '/'$file . '.png';
        }

        # making thumb
        imagethumb($file,$thumbnail,$thumb_max_size);
    }
}

function imagethumb($src,$dest,$size) {
    if($src_info = @getimagesize($src)){
        # src image type
        # delete case 1: if don't have gif support
        switch($src_info[2]){
            case 1:
            $img_src = imagecreatefromgif($src);
            break;
        case 2:
            $img_src = imagecreatefromjpeg($src);
            break;
        case 3:
            $img_src = imagecreatefrompng($src);
            break;
        default:
            return false; # not gif, jpeg or png
        }

        # dest image size
        if($src_info[0]<=$size && $src_info[1]<=$size){ # do not magnify large images;
            $dest_info[0]=$src_info[0];
            $dest_info[1]=$src_info[1];
        }elseif($src_info[0]>=$src_info[1]){
            $dest_info[0]=$size;
            $dest_info[1]=$size*$src_info[1]/$src_info[0];
        }else{
            $dest_info[0]=$size*$src_info[0]/$src_info[1];
            $dest_info[1]=$size;
        }
        # destination image
        $img_dest = imagecreatetruecolor($dest_info[0],$dest_info[1]);

        # resampling
        imagecopyresampled($img_dest, $img_src, 0, 0, 0, 0, $dest_info[0], $dest_info[1], $src_info[0], $src_info[1]);

        # output
        if(file_exists($dest)) unlink($dest); # delete old thumbnail (?)
        imagepng($img_dest,$dest);

        # Cleaning
        imagedestroy($img_dest);
        imagedestroy($img_src);
        return true;
    } else {
        return false; # not even a picture
    }
}

?>

Entropia 04:11 21.9.04 
Näitä on jo 1000 ja 1.
makeuu 07:37 21.9.04 
Entropia kirjoitti:
Näitä on jo 1000 ja 1.
Nyt 1000 ja 2
Meitsi 18:03 21.9.04 
ei eroa edukseen muista kuhan thumbnailereista... siitä miinus