• Apfeltalk ändert einen Teil seiner Allgemeinen Geschäftsbedingungen (AGB), das Löschen von Useraccounts betreffend.
    Näheres könnt Ihr hier nachlesen: AGB-Änderung
  • Viele hassen ihn, manche schwören auf ihn, wir aber möchten unbedingt sehen, welche Bilder Ihr vor Eurem geistigen Auge bzw. vor der Linse Eures iPhone oder iPad sehen könnt, wenn Ihr dieses Wort hört oder lest. Macht mit und beteiligt Euch an unserem Frühjahrsputz ---> Klick

PHP Thumbnail direkt im Code erstellen

hubionmac

Tydemans Early Worcester
Registriert
25.06.04
Beiträge
393
Hallo,

ich würde gerne eine Thumbnail Übersicht eines Verzeichnisses mit Bildern erzeugen, nur möchte ich dazu das Script nichts abspeichern lassen (Thumnails als Datei abspeichern bzw. cachen), sondern das PHP-Script die Thumbnails direkt in den HTML-Code einfließen lassen.
Ich bin mir nicht sicher, wie das geht, aber ich erinnere mich an eine Funktion Bilder direkt im HTML-Code einzubinden (als Binärstream oder so)
Hätte da jemand eine gute Idee :-D
 

_lux_

Bismarckapfel
Registriert
23.11.07
Beiträge
75
Moin,

vor dem Problem stand ich auch mal. Der Schlüssel zum Erfolg ist, die Generierung des Thumbnails in ein extra Script auszulagern und dieses dann als Bild einzubinden.
In dem Script musst du dann auch den entsprechenden Content-Type im HTTP-Header setzen.

das folgende Script macht genau das, was du willst:

http://www.phpclasses.org/browse/package/2687.html

leider musst du dich da glaube ich erst registrieren, deswegen hab ich mir mal erlaubt, das hier zu posten.

Code:
<?php
/*********************************
ThumbnailCreator
Programmed by : Daniel Thul
E-mail        : [email protected]
*********************************/
/*SoMe InFoS:
The index must be a number between 0 and 101
If you choose for example $thumb->index = "2", the Thumbnail will be as half as greate as the original picture
For savety is cared enough, because this class will work only with datas from the type image/jpeg or image/jpg
and it checks if the index is a number and between 0 and 101. If the variable picture is no image the script will
not work and end with exit(). If $index is not a number or not between 0 and 101 it will be set to 2.
Everybody can use this class freely and give it around, but with a note, so that everybody can see who is tha author and his E-Mail adress (Daniel Thul, [email protected])
If you use this class, I would like if you write me an E-Mail, because I would like to see if anybody uses my class.
Feel free to report me bugs or ideas for improvements. :-)*/

class thumb{
        var $picture;
        var $index;

        function create(){
        $picture = $this->picture;
        $index = $this->index;
        $test = pathinfo($picture);

        if (file_exists($picture)){
        $test[extension] = strtolower($test[extension]);
           if ($test[extension] == 'jpg' || $test[extension] == 'jpeg'){
           $do = TRUE;
           }
           else{
           $do = FALSE;
           }
        }
        else{
        $do = FALSE;
        }

        if ($do){
           if (isset($index) && $index > 0 && $index < 101){
              $index = str_replace(",", ".", $index);
           }
           else{
                $index = 2;
           }

        $imageinfo = GetImageSize($picture);
        $thumbheight = $imageinfo[1]/$index;
        $thumbwidth = $imageinfo[0]/$index;
        $height = explode(".", $thumbheight);
        $width = explode(".", $thumbwidth);
        header("Content-type: image/jpeg");
        $dest_img = ImageCreate($width[0], $height[0]);
        $src_img = ImageCreateFromJpeg($picture);
        ImageCopyResized($dest_img, $src_img, 0, 0, 0, 0, $width[0], $height[0], ImageSX($src_img), ImageSY($src_img));
        ImageJpeg($dest_img);
        ImageDestroy($dest_img);
        ImageDestroy($src_img);
        }
        else{
        exit();
        }
      }
}
?>

und...

Code:
<?
require("class.thumb.php");//Here the class itself is included
$thumb = new thumb;//The class is started
//If you want to test this class, you need a picture called "bild.jpg"
$thumb->picture = "bild.jpg";//Here you save the name of the picture
/*The index must be a number between 0 and 101
If you choose for example $thumb->index = "2", the Thumbnail will be as half as greate as the original picture
For savety is cared enough, because this class will work only with datas from the type image/jpeg or image/jpg
and it checks if the index is a number and between 0 and 101. If the variable picture is no image the script will
not work and end with exit(). If $index is not a number or not between 0 and 101 it will be set to 2.
Everybody can use this class freely and give it around, but with a note, so that everybody can see who is tha author and his E-Mail adress (Daniel Thul, [email protected])
If you use this class, I would like if you write me an E-Mail, because I would like to see if anybody uses my class.
Feel free to report me bugs or ideas for improvements. :-)*/
$thumb->index = "1.5";//Here the index is set
$thumb->create();//This function creates the thumbnail and sends the picture with a header for the type "image/jpeg" to the browser
?>

ich hatte das damals glaube ich noch ein wenig insofern angepasst, dass auch andere dateien als jpeg verwendet werden können. ist aber auch nur ein wenig abfrage-aufwand...
 

hubionmac

Tydemans Early Worcester
Registriert
25.06.04
Beiträge
393
cool, das funktioniert super... jetzt muss ich es nur noch hinbekommen die thumbnails in eine HTML-Datei zu bekommen und so eine Liste erstellen zu können... hättest Du da vielleicht auch einen Tip?
 

_lux_

Bismarckapfel
Registriert
23.11.07
Beiträge
75
versteh ich nicht ganz - du willst eine gallerie erstellen?
 

Bananenbieger

Golden Noble
Registriert
14.08.05
Beiträge
25.515
Dynamisch Thumbnails generieren ist auf "normalem" Webspace eine ganz schlechte Idee (Lastet den Server ungemein aus).
 

_lux_

Bismarckapfel
Registriert
23.11.07
Beiträge
75
naja ich denke, das kommt auf den Traffic an. Bei mir gings damals auch ohne Einbußen. Bei vielen Besuchern sollte man das natürlich nicht tun.
 

Bananenbieger

Golden Noble
Registriert
14.08.05
Beiträge
25.515
Kommt eher darauf an, wieviele Bilder er pro Galerie hat und in welcher Auflösung diese vorliegen. Selbst ein High-End-Server geht bei mehr als 100 1-Megapixel-Bildern in die Knie, wenn das Script nicht vernünftig programmiert ist.
 

hubionmac

Tydemans Early Worcester
Registriert
25.06.04
Beiträge
393
ich brauche das für den BackOffice Bereich. Da gibt es nicht soviele zugriffe.
Ich will den Server nur nicht mit Thumbnail-Dateien zumüllen, wo ich sie nicht brauche.
Idee ist es ein Verzeichnis mit Bilder über den Browser zu verwalten. Dateien auflisten, löschen und hochladen.
So dass ich den FTP-Client nicht mehr so oft einsetzten muss und wenn ich ein best. Bild löschen muss, auch weiss wie das aussieht.


Jetzt stellt sich nur ein bestimmtes Problem:
Sind die Bilder größer als ein MB streikt die Funktion und gibt kein Bild aus. Wo sitzt denn da das Limit? Irgendwo in der PHP-INI?