Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP: Thumbnails

Empfohlene Antworten

Veröffentlicht

Hallo, erst mal.

Was benötige ich alles, um eine Bildergalerie mit PHP zu erstellen.

Aufbau: Startseite zeigt Bilder (jeweils 30) verkleinert an (Thumbnails). Wird auf eins geklickt, wird es in Orginalgröße dargestellt. Müssen noch irgendwelche Dateien mit eingelinkt werden? hat evtl. jemand einen Bsp code? Das geht doch mit Thums oder?

also die Thumbnails würde ich, sofern sie nicht bereits vorhanden sind, von PHP erstellen lassen.

Dazu rate ich dir die Onlinedokumentation der entsprechenden Imagefunktionen auf PHP.net anzusehen. Dort in den Kommentaren sind nützliche Beispiele.

Ich such ja grad ne Funktion, die mir aus den jpg Thumbnails erzeugt. So was wie Create Tumbnails. Das macht doch die Funktion imagecreatefromjpeg (für jpgs) oder?

Ich such ja grad ne Funktion, die mir aus den jpg Thumbnails erzeugt. So was wie Create Tumbnails

selbst ist der mann.

bei php ist das nicht dabei.

Musst dir aus den vorhandenen funktionen selber basteln!

oder du nimmst meine so lange:


<?php
// Image-Basis-Objekt
class Image {
var $sFile;
var $oImage = null;

function Image($sFile) {
$this->sFile = strtolower($sFile);
$this->oImage = $this->getImageResource();
}

function getImageResource() {
trigger_error("Unsupported imageformat! File: '". $this->getFile() ."'", E_USER_ERROR);
}

function getImageHeight() {
return imagesy($this->getImageResource());
}

function getImageWidth() {
return imagesx($this->getImageResource());
}

function getFile() {
return $this->sFile;
}

function getFileName() {
return substr(strrchr($this->getFile(), "/"), 1);
}

function getFilePath() {
return substr($this->getFile(), 0, strrpos($this->getFile(), "/") + 1 );
}

function getImage() {
trigger_error("Unsupported imageformat! File: '". $this->getFile() ."'", E_USER_ERROR);
}

function getThumbnail(&$sDestination, $iWidth = null, $iHeight = null) {
if ($sDestination { strlen($sDestination) - 1 } != "/") $sDestination .= "/";
$sDestination .= str_replace(array("..", "."), array("__", "_"), $this->getFilePath());

$sFileLocation = $sDestination . $this->getFileName();

// Wenn schon ein thumbnail vorhanden ist, den Pfad zu diesem zurückgeben
if ( file_exists( $sFileLocation)) {
$sDestination = $sFileLocation;
return null;
}

$oSrcImg = $this->getImageResource();
$iImgWidth = $this->getImageWidth();
$iImgHeight = $this->getImageHeight();

// Ordnerstruktur anlegen
$this->makedir($sDestination);

$sDestination = $sFileLocation;

if ($iImgHeight > $iImgWidth) {
// Hochkanntbilder proportional verkleinern
$iHeight = $iWidth;
$iWidth = null;
}

if (is_null($iWidth) && is_null($iHeight) || ($iImgWidth <= $iWidth && $iImgHeight <= $iHeight)) {
// Weder Höhe noch Breite wurden angegeben oder
// Die Höhe und Breite des Original Images ist schon kleiner als das Thumbnail das erstellt würde
// => pfad auf das Original Image zurückgeben
$sDestination = $this->getFile();
return null;
} else if (is_null($iWidth)) {
// Breite wurde angegebene, dazu die Höhe errechen
$iWidth = ($iImgWidth / $iImgHeight) * $iHeight;
} else if (is_null($iHeight)) {
// Höhe wurde angegeben, dazu die Breite errechnen
$iHeight = ($iImgHeight / $iImgWidth) * $iWidth;
}

$oThumbnail = imagecreatetruecolor($iWidth, $iHeight);
imagecopyresized($oThumbnail, $oSrcImg, 0, 0, 0, 0, $iWidth, $iHeight, $iImgWidth, $iImgHeight);

return $oThumbnail;
}

function makedir($sDir) {
$aDir = explode("/", $sDir);
$sParentDir = "";

foreach ($aDir as $sDirPart) {
if ( $sDirPart == "") continue;

$sCurrentDir = $sParentDir . $sDirPart . "/";

if (!is_dir($sCurrentDir)) {
mkdir($sCurrentDir);
}

$sParentDir = $sCurrentDir;
}
}
}
/* GIF Bilder sind mit der GD-Lib nicht mehr möglich!

// GIF-Format Objekt
class ImageGif extends Image {
function getImageResource() {
if (is_null($this->oImage)) {
$this->oImage = imagecreatefromgif($this->getFile());
}

return $this->oImage;
}

function getImage() {
return imagegif($this->getImageResource(), $this->getFile());
}

function getThumbnail($sDestinationDir, $iWidth = null, $iHeight = null) {
$oThumbnail = parent :: getThumbnail($sDestinationDir, $iWidth, $iHeight);
return imagegif($oThumbnail, $sDestinationDir.$this->getFileName());
}
}
*/

// JPG-Format Objekt
class ImageJpg extends Image {
function getImageResource() {
if (is_null($this->oImage)) {
$this->oImage = imagecreatefromjpeg($this->getFile());
}

return $this->oImage;
}

function getImage() {
return imagejpeg($this->getImageResource(), $this->getFile());
}

function getThumbnail($sDestination, $iWidth = null, $iHeight = null) {
$oThumbnail = parent :: getThumbnail($sDestination, $iWidth, $iHeight);

if ( !is_null($oThumbnail)) {
imagejpeg($oThumbnail, $sDestination);
}

return $sDestination;
}
}


// PNG-Format Objekt
class ImagePng extends Image {
function getImageResource() {
if (is_null($this->oImage)) {
$this->oImage = imagecreatefrompng($this->getFile());
}

return $this->oImage;
}

function getImage() {
return imagepng($this->getImageResource(), $this->getFile());
}

function getThumbnail($sDestination, $iWidth = null, $iHeight = null) {
$oThumbnail = parent :: getThumbnail($sDestination, $iWidth, $iHeight);

if ( !is_null($oThumbnail)) {
imagepng($oThumbnail, $sDestination);
}

return $sDestination;
}
}

// Factory zum erstellen von Image-Objekten
class ImageFactory {
function getImage( $sFileName) {
if (!file_exists($sFileName)) {
trigger_error("File '".$sFileName."' does not exist!", E_USER_ERROR);
}

$sFileType = strtolower(substr(strrchr($sFileName, "."), 1));

switch ($sFileType) {
case "jpeg" :
case "jpg" : return new ImageJpg($sFileName);
// case "gif" : return new ImageGif($sFileName);
case "png" : return new ImagePng($sFileName);

default : return new Image($sFileName);
}
}
}
?>
[/PHP]

PS: Gif Bilder sind seit der neuen GD-Lib wieder möglich. Du brauchst halt die entsprechende Version!

Danke. Mit anderen Worten, ich muss die GD-Lib noch installieren?

korrekt :D

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.