Come posso creare una galleria di immagini basata su PHP / MySQL da zero?
Domanda
Questa domanda è stata posta prima di menzionare la lib o include quella che fornisce una galleria funzionale, ma voglio crearne una da zero. Quindi idee su quanto segue
- Le gallerie devono essere caricate usando Form and Browse (questo non riesco a trovare alcun problema, ho solo bisogno che sia lì per delineare i passaggi)
- È necessario creare un'immagine in miniatura quando viene caricato un file.
- Come dovrebbe essere strutturato nel database, ad esempio memorizzato nel database come immagine o nome file
Requirments
- Solo PHP e MySql
Qualche idea? Per favore fatemi sapere se non può essere fatto anche: D
Grazie
Soluzione
Tenterò di rispondere alle tue domande:
Domanda 1
Quella parte è in realtà semplice. Per creare un modulo di caricamento file, il tuo HTML deve apparire così:
<form enctype='multipart/form-data' action='CodeTool.php' method='POST'>
File: <input name='picture' type='file'/>
<input type='submit' value='Upload'/>
</form>
Il tuo modulo deve avere enctype='multipart/form-data'
e method
deve essere POST
. Quindi, per leggere il file di caricamento, puoi semplicemente utilizzare quanto segue. Ho anche aggiunto alcune convalide di base per assicurarmi che il file sia un'immagine.
if(isset($_FILES['picture'])) {
echo "File has been uploaded under temp file " . $_FILES['picture']['tmp_name'];
// Let's check if the file is an image:
$fileData = file_get_contents($_FILES['picture']['tmp_name']);
// Using imagecreatefromstring, that way you don't need to
// guess the image format.
if(($img = @imagecreatefromstring($fileData)) !== FALSE) {
echo " and is a valid image";
} else {
echo " and is not a valid image";
}
}
Domanda 2
Per creare una miniatura, puoi usare GD (o ImageMagick, ma non è incluso nella configurazione predefinita) come tale ... Continuiamo dall'istruzione imagecreatefromstring
if
:
if(($img = @imagecreatefromstring($fileData)) !== FALSE) {
// Let's create a 100x100 thumbnail
$width = imagesx($img);
$height = imagesy($img);
$boxSize = min($width,$height);
$boxX = ($width / 2) - ($boxSize / 2);
$boxY = ($height / 2) - ($boxSize / 2);
$thumb = imagecreatetruecolor(100, 100);
imagecopyresampled($thumb, $img, 0, 0, $boxX, $boxY, 100, 100, $boxSize, $boxSize);
//$thumb is now a 100x100 thumbnail
}
Domanda 3
Qui hai 2 opzioni. È possibile memorizzare le immagini nel file system o nel database. Per memorizzare la tua immagine nel file system, puoi fare quanto segue:
if(($img = @imagecreatefromstring($fileData)) !== FALSE) {
move_uploaded_file($_FILES['picture']['tmp_file'], 'somefile.jpg');
// the code from the previous example
imagejpeg($thumb, 'somefile_thumb.jpg');
}
Personalmente preferisco utilizzare il database per archiviare le immagini in quanto è più facile mantenere l'integrità referenziale e semplifica il backup (esegui il backup del database e il gioco è fatto). È un po 'più lento, ma la differenza non è poi così eccezionale:
if(($img = @imagecreatefromstring($fileData)) !== FALSE) {
// the code from the previous example
$tmp_thumb = tempnam(sys_get_temp_dir(), 'thumb');
imagejpeg($thumb, $tmp_thumb);
$thumbData = file_get_contents($tmp_thumb);
mysql_query("INSERT INTO images (original, thumb) VALUES ('" . mysql_real_escape_string($fileData) . "', '" . mysql_real_escape_string($thumbData) . "');");
}
I campi devono essere BLOB
.
Altri suggerimenti
Quasi sicuramente vorrai archiviare le immagini nel filesystem e quindi fare semplicemente riferimento al nome file \ percorso nella voce DB - mantiene ridotte le dimensioni dei risultati della query, soprattutto se desideri estrarre le informazioni per più immagini . Inoltre, è molto più semplice invocare cose come imagemagick se si desidera utilizzarlo per creare miniature.