ThumbCreator Skript

Die erste Funktion erzeugt die Thumbnails in der gewünschten Größe ohne das Bild zu verzerren. Das bedeutet im Einzelnen, dass es die Bilder abschneidet.

Die zweite Funktion lässt das ursprüngliche Verhältnis von Höhe und Breite gleich und verkleinert das Bild nur.

thumbs.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php

function createThumb($imageName$thumbWidth=125$thumbHeight=125$thumbName=''){
    
    if(
$thumbName=='') {
        
$p strrpos($imageName"/");
        
$file substr($imageName$p+1);
        
$path substr($imageName0$p);
        
$thumbName $path."/s/".$file;
    }

    if(
strstr($imageName,'.jpg')) {
        
$img imagecreatefromjpeg$imageName );
        
$width imagesx$img );
        
$height imagesy$img );

        
$tmp_img imagecreatetruecolor$thumbWidth$thumbHeight );
        
        
$xw = (($thumbWidth/$thumbHeight)*$height); 
        
$xh = (($thumbHeight/$thumbWidth)*$width);
        
$s_x max(        0, ($width 2) - ($xw/2) );
        
$s_w min($width,$xw);
        
$s_y max(        0, ($height 2) - ($xh/2) );
        
$s_h min($height,$xh);

        
imagecopyresized$tmp_img$img00$s_x$s_y$thumbWidth$thumbHeight$s_w$s_h );
        
imagejpeg($tmp_img$thumbName100 );
    }
}
function 
createThumb2($imageName$thumbWidth=250$thumbName=''){
    
    if(
$thumbName=='') {
        
$p strrpos($imageName"/");
        
$file substr($imageName$p+1);
        
$path substr($imageName0$p);
        
$thumbName $path."/m/".$file;
    }

    if(
strstr($imageName,'.jpg')) {
        
$img imagecreatefromjpeg$imageName );
        
$width imagesx$img );
        
$height imagesy$img );

        
$tmp_img imagecreatetruecolor$thumbWidth$height*($thumbWidth/$width) );
        
imagecopyresized$tmp_img$img0000$thumbWidth$height*($thumbWidth/$width), $width,$height );
        
imagejpeg($tmp_img$thumbName100 );
    }
}

die(
'remove this line');

$dirName '.';
$dir opendir($dirName);
if(!
$dir) die('could not open dir');
while(
false !== ($file readdir($dir)))
    if(!
is_dir($dirName.'/'.$file) && strstr($file'.jpg')) {
        
createThumb($dirName.'/'.$file);
        
createThumb2($dirName.'/'.$file);
        
//echo '<img src="s/'.$file.'"> <img src="m/'.$file.'"> <img src="'.$file.'"><br>';
    
}
closedir($dir);


Beispiele

createThumb:

Frosch Tower Zebra

createThumb2:

Frosch Tower Zebra

Urheber:
Frosh: Franco Andreone
Pferd: André Karwath