|
Trouver une ressource
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
CRÉE UNE IMAGE DE PILE DE MINIATURES EN UTILISANT LA BIBLIOTHÈQUE GD
Information sur la source
Description
Tout est dans le titre. Regarder la capture pour mieux comprendre. N'hésitez pas à me contacter si vous avez des questions.
Source
- <?php
-
- /**
- * Génére une pile d'images
- *
- * @author Roland Dufour <roland.dufour@multiprog.net>
- */
- class PileOfPictures
- {
- private $imgs = array();
- private $conf = array(
- /**
- * @var int $cacheTime Durée du cache des images de destination (en secondes)
- */
- 'cacheTime' => 0,
-
- /**
- * @var int $w Largeur de l'image de destination (en pixels)
- */
- 'w' => 120,
-
- /**
- * @var int $h Hauteur de l'image de destination (en pixels)
- */
- 'h' => 120,
-
- /**
- * @var bool $destOutput Format de l'image de destination (jpg, gif, png)
- */
- 'destOutput' => 'gif',
-
- /**
- * @var bool $destBkgEnabled L'image de destination doit-elle avoir un fond ? (oui: true | non: false)
- */
- 'destBkgEnabled' => false,
-
- /**
- * @var array $destBkg Couleur du fond de l'image de destination (sous forme array(red, green, blue))
- */
- 'destBkg' => array(255, 255, 255),
-
- /**
- * @var bool $destBrdEnabled L'image de destination doit-elle avoir une bordure ? (oui: true | non: false)
- */
- 'destBrdEnabled' => false,
-
- /**
- * @var array $destBrd Couleur de bordure de l'image de destination (sous forme array(red, green, blue))
- */
- 'destBrd' => array(127, 127, 127),
-
- /**
- * @var bool $tmbBkgEnabled Les miniatures doivent-elles avoir un fond ? (oui: true | non: false)
- */
- 'tmbBkgEnabled' => true,
-
- /**
- * @var array $tmbBkg Couleur du fond de chaque miniature (sous forme array(red, green, blue))
- */
- 'tmbBkg' => array(255, 255, 254),
-
- /**
- * @var bool $tmbBrdEnabled Les miniatures doivent-elles avoir une bordure ? (oui: true | non: false)
- */
- 'tmbBrdEnabled' => true,
-
- /**
- * @var array $tmbBrd Couleur de bordure de chaque miniature (sous forme array(red, green, blue))
- */
- 'tmbBrd' => array(127, 127, 127),
-
- /**
- * @var int $tmbMargin Marge entre la bordure de la miniature et la miniature elle-même
- */
- 'tmbMargin' => 0,
-
- /**
- * @var bool $tmbIsSquare L'image miniature doit-elle être carrée ?
- */
- 'tmbIsSquare' => true,
-
- /**
- * @var int $maxImg Nombre maximum de minatures sur l'image de destination
- */
- 'maxImg' => 10,
-
- /**
- * @var int $marginPictureX Espacement minimum entre chaque images en X
- */
- 'marginPictureX' => 4,
-
- /**
- * @var int $marginPictureY Espacement minimum entre chaque images en Y
- */
- 'marginPictureY' => 3
- );
-
-
- public function __construct($imgs=null){
- if (!is_null($imgs)){
- $this->addImg($imgs);
- }
- }
-
- public function addImg($img){
- if (is_array($img)){
- foreach ($img as $i) {
- if (!$this->addImg($i)){
- return false;
- }
- }
- return true;
- }
-
- if (file_exists($img)){
- $this->imgs[] = $img;
- return true;
- }
- return false;
- }
-
- public function __call($name, $args){
- $access = substr($name, 0, 3);
- $property = substr($name, 3);
-
- if ($access == 'get' && isset($this->conf[$property])){
- return $this->conf[$property];
- }
- if ($access == 'set' && isset($this->conf[$property]) && isset($args[0])){
- $this->conf[$property] = $args[0];
- return true;
- }
- return false;
- }
-
- public function render(){
- $dir = 'img_scripts';
-
- extract($this->conf);
-
-
- if (file_exists($dir.'/'.md5(serialize($this)).'.'.$this->conf['destOutput'])){
- if (filemtime($dir.'/'.md5(serialize($this)).'.'.$this->conf['destOutput']) < time() - $cacheTime){
- unlink($dir.'/'.md5(serialize($this)).'.'.$this->conf['destOutput']);
- }
- else {
- switch ($this->conf['destOutput']) {
- case 'gif':
- header('Content-type: image/gif');
- break;
-
- case 'png':
- header('Content-type: image/png');
- break;
-
- default:
- case 'jpg':
- header('Content-type: image/jpeg');
- break;
- }
- readfile($dir.'/'.md5(serialize($this)).'.'.$this->conf['destOutput']);
- die();
- }
- }
-
- # On détermine le nombre d'images à afficher
- $nb = count($this->imgs) < $maxImg ? count($this->imgs) : $maxImg;
-
- # On recalcule le marginPicture en fonction du nombre d'images à afficher
- $marginPictureX = floor($maxImg / $nb * $marginPictureX);
- $marginPictureY = floor($maxImg / $nb * $marginPictureY);
-
- # Calcul de l'origine X et Y maximum des miniatures
- $maxX = $marginPictureX * ($nb -1);
- $maxY = $marginPictureY * ($nb -1);
-
- # Calcul du pas horizontal et vertical
- if ($nb == 1){
- $pasX = $pasY = 0;
- } else {
- $pasX = floor($maxX / ($nb -1));
- $pasY = floor($maxY / ($nb -1));
- }
-
- # On initialise l'origine de la miniature à 0,0
- $x = $y = 0;
-
- # Création de l'image de destination
- $image = imagecreatetruecolor($w, $h);
- imagealphablending($image, false);
-
- if ($destOutput == 'gif'){
- $trnprt_indx = imagecolorallocate($image, 0, 0, 0);
- imagefill($image, 0, 0, $trnprt_indx);
- imagecolortransparent($image, $trnprt_indx);
- }
-
- if ($destBkgEnabled){
- imagefilledrectangle($image, 0, 0, $w, $h, imagecolorallocate ($image, $destBkg[0], $destBkg[1], $destBkg[2]));
- }
-
- # On boucle chaque miniature
- for ($i=0; $i<$nb; $i++){
- # Création de la miniature
- if ($tmbIsSquare){
- if (($w - $maxX) < ($h - $maxY)){
- $minThumb = imagecreatetruecolor(($w - $maxX), ($w - $maxX));
- } else {
- $minThumb = imagecreatetruecolor(($h - $maxY), ($h - $maxY));
- }
- } else {
- $minThumb = imagecreatetruecolor(($w - $maxX), ($h - $maxY));
- }
-
- # On teste l'extension pour utiliser la fonction imagecreatefrom... correspondante
- $ext = substr($this->imgs[$i], -3);
- if ($ext == 'jpg'){
- $thumb = imagecreatefromjpeg($this->imgs[$i]);
- }
- elseif ($ext == 'gif'){
- $thumb = imagecreatefromgif($this->imgs[$i]);
-
- # Gestion des GIFs transparents
- $trnprt_indx = imagecolortransparent($thumb);
- if ($trnprt_indx >= 0) {
- $trnprt_color = imagecolorsforindex($thumb, $trnprt_indx);
- $trnprt_indx = imagecolorallocate($minThumb, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
- imagefill($minThumb, 0, 0, $trnprt_indx);
- imagecolortransparent($minThumb, $trnprt_indx);
- }
- }
- elseif ($ext == 'png'){
- $thumb = imagecreatefrompng($this->imgs[$i]);
- }
- else {
- continue;
- }
-
- if ($tmbBkgEnabled){
- imagefilledrectangle($minThumb, 0, 0, imagesx($minThumb), imagesy($minThumb), imagecolorallocate($minThumb, $tmbBkg[0], $tmbBkg[1], $tmbBkg[2]));
- }
-
- $size = getimagesize($this->imgs[$i]);
-
- // On gère l'echelle
- if ($size[0] != imagesx($minThumb) && $size[1] != imagesy($minThumb)){
- if ($size[0] > $size[1]){
- $tmpW = $size[0];
- $size[0] = imagesx($minThumb);
- $size[1] = imagesx($minThumb) * $size[1] / $tmpW;
- if ($size[1] > imagesy($minThumb)){
- $tmpH = $size[1];
- $size[1] = imagesy($minThumb);
- $size[0] = imagesy($minThumb) * $size[0] / $tmpH;
- }
- } else {
- $tmpH = $size[1];
- $size[1] = imagesy($minThumb);
- $size[0] = imagesy($minThumb) * $size[0] / $tmpH;
- if ($size[0] > imagesx($minThumb)){
- $tmpW = $size[0];
- $size[0] = imagesx($minThumb);
- $size[1] = imagesx($minThumb) * $size[1] / $tmpW;
- }
- }
- }
-
-
- $mW = (imagesx($minThumb) - $size[0]) /2;
- $mH = (imagesy($minThumb) - $size[1]) /2;
-
-
- # on génére la miniature
- imagecopyresampled($minThumb, $thumb, $tmbMargin + $mW, $tmbMargin + $mH, 0, 0, imagesx($minThumb) -$tmbMargin*2 - $mW*2, imagesy($minThumb) -$tmbMargin*2 - $mH*2, imagesx($thumb), imagesy($thumb));
-
- if ($tmbBrdEnabled){
- imagerectangle($minThumb, 0, 0, imagesx($minThumb)-1, imagesy($minThumb)-1, imagecolorallocate($minThumb, $tmbBrd[0], $tmbBrd[1], $tmbBrd[2]));
- }
-
- # On dispose la miniature sur l'image de destination
- imagecopymerge($image, $minThumb, $x, $y, 0, 0, imagesx($minThumb), imagesy($minThumb), 100);
-
- # On libère de la mémoire
- imagedestroy($thumb);
- imagedestroy($minThumb);
-
- # On déplace l'origine de la miniature suivante
- $x += $pasX;
- $y += $pasY;
- }
-
- if ($destBrdEnabled){
- imagerectangle($image, 0, 0, imagesx($image)-1, imagesy($image)-1, imagecolorallocate($image, $destBrd[0], $destBrd[1], $destBrd[2]));
- }
-
-
- switch ($destOutput) {
- case 'gif':
- header('Content-type: image/gif');
- imagegif($image, 'img_scripts/'.md5(serialize($this)).'.gif');
- imagegif($image);
- break;
-
- case 'png':
- header('Content-type: image/png');
- imagepng($image, 'img_scripts/'.md5(serialize($this)).'.png');
- imagepng($image);
- break;
-
- default:
- case 'jpg':
- header('Content-type: image/jpeg');
- imagejpeg($image, 'img_scripts/'.md5(serialize($this)).'.jpg');
- imagejpeg($image);
- break;
- }
- imagedestroy($image);
-
- // On fait du tri dans les imgs de paniers
- $o = opendir($dir);
- while ($file = readdir($o)){
- if (!is_file($dir.'/'.$file)){
- continue;
- }
- if (filemtime($dir.'/'.$file) < time() - $cacheTime){
- unlink($dir.'/'.$file);
- }
- }
-
- die();
- }
- }
-
- ?>
<?php
/**
* Génére une pile d'images
*
* @author Roland Dufour <roland.dufour@multiprog.net>
*/
class PileOfPictures
{
private $imgs = array();
private $conf = array(
/**
* @var int $cacheTime Durée du cache des images de destination (en secondes)
*/
'cacheTime' => 0,
/**
* @var int $w Largeur de l'image de destination (en pixels)
*/
'w' => 120,
/**
* @var int $h Hauteur de l'image de destination (en pixels)
*/
'h' => 120,
/**
* @var bool $destOutput Format de l'image de destination (jpg, gif, png)
*/
'destOutput' => 'gif',
/**
* @var bool $destBkgEnabled L'image de destination doit-elle avoir un fond ? (oui: true | non: false)
*/
'destBkgEnabled' => false,
/**
* @var array $destBkg Couleur du fond de l'image de destination (sous forme array(red, green, blue))
*/
'destBkg' => array(255, 255, 255),
/**
* @var bool $destBrdEnabled L'image de destination doit-elle avoir une bordure ? (oui: true | non: false)
*/
'destBrdEnabled' => false,
/**
* @var array $destBrd Couleur de bordure de l'image de destination (sous forme array(red, green, blue))
*/
'destBrd' => array(127, 127, 127),
/**
* @var bool $tmbBkgEnabled Les miniatures doivent-elles avoir un fond ? (oui: true | non: false)
*/
'tmbBkgEnabled' => true,
/**
* @var array $tmbBkg Couleur du fond de chaque miniature (sous forme array(red, green, blue))
*/
'tmbBkg' => array(255, 255, 254),
/**
* @var bool $tmbBrdEnabled Les miniatures doivent-elles avoir une bordure ? (oui: true | non: false)
*/
'tmbBrdEnabled' => true,
/**
* @var array $tmbBrd Couleur de bordure de chaque miniature (sous forme array(red, green, blue))
*/
'tmbBrd' => array(127, 127, 127),
/**
* @var int $tmbMargin Marge entre la bordure de la miniature et la miniature elle-même
*/
'tmbMargin' => 0,
/**
* @var bool $tmbIsSquare L'image miniature doit-elle être carrée ?
*/
'tmbIsSquare' => true,
/**
* @var int $maxImg Nombre maximum de minatures sur l'image de destination
*/
'maxImg' => 10,
/**
* @var int $marginPictureX Espacement minimum entre chaque images en X
*/
'marginPictureX' => 4,
/**
* @var int $marginPictureY Espacement minimum entre chaque images en Y
*/
'marginPictureY' => 3
);
public function __construct($imgs=null){
if (!is_null($imgs)){
$this->addImg($imgs);
}
}
public function addImg($img){
if (is_array($img)){
foreach ($img as $i) {
if (!$this->addImg($i)){
return false;
}
}
return true;
}
if (file_exists($img)){
$this->imgs[] = $img;
return true;
}
return false;
}
public function __call($name, $args){
$access = substr($name, 0, 3);
$property = substr($name, 3);
if ($access == 'get' && isset($this->conf[$property])){
return $this->conf[$property];
}
if ($access == 'set' && isset($this->conf[$property]) && isset($args[0])){
$this->conf[$property] = $args[0];
return true;
}
return false;
}
public function render(){
$dir = 'img_scripts';
extract($this->conf);
if (file_exists($dir.'/'.md5(serialize($this)).'.'.$this->conf['destOutput'])){
if (filemtime($dir.'/'.md5(serialize($this)).'.'.$this->conf['destOutput']) < time() - $cacheTime){
unlink($dir.'/'.md5(serialize($this)).'.'.$this->conf['destOutput']);
}
else {
switch ($this->conf['destOutput']) {
case 'gif':
header('Content-type: image/gif');
break;
case 'png':
header('Content-type: image/png');
break;
default:
case 'jpg':
header('Content-type: image/jpeg');
break;
}
readfile($dir.'/'.md5(serialize($this)).'.'.$this->conf['destOutput']);
die();
}
}
# On détermine le nombre d'images à afficher
$nb = count($this->imgs) < $maxImg ? count($this->imgs) : $maxImg;
# On recalcule le marginPicture en fonction du nombre d'images à afficher
$marginPictureX = floor($maxImg / $nb * $marginPictureX);
$marginPictureY = floor($maxImg / $nb * $marginPictureY);
# Calcul de l'origine X et Y maximum des miniatures
$maxX = $marginPictureX * ($nb -1);
$maxY = $marginPictureY * ($nb -1);
# Calcul du pas horizontal et vertical
if ($nb == 1){
$pasX = $pasY = 0;
} else {
$pasX = floor($maxX / ($nb -1));
$pasY = floor($maxY / ($nb -1));
}
# On initialise l'origine de la miniature à 0,0
$x = $y = 0;
# Création de l'image de destination
$image = imagecreatetruecolor($w, $h);
imagealphablending($image, false);
if ($destOutput == 'gif'){
$trnprt_indx = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $trnprt_indx);
imagecolortransparent($image, $trnprt_indx);
}
if ($destBkgEnabled){
imagefilledrectangle($image, 0, 0, $w, $h, imagecolorallocate ($image, $destBkg[0], $destBkg[1], $destBkg[2]));
}
# On boucle chaque miniature
for ($i=0; $i<$nb; $i++){
# Création de la miniature
if ($tmbIsSquare){
if (($w - $maxX) < ($h - $maxY)){
$minThumb = imagecreatetruecolor(($w - $maxX), ($w - $maxX));
} else {
$minThumb = imagecreatetruecolor(($h - $maxY), ($h - $maxY));
}
} else {
$minThumb = imagecreatetruecolor(($w - $maxX), ($h - $maxY));
}
# On teste l'extension pour utiliser la fonction imagecreatefrom... correspondante
$ext = substr($this->imgs[$i], -3);
if ($ext == 'jpg'){
$thumb = imagecreatefromjpeg($this->imgs[$i]);
}
elseif ($ext == 'gif'){
$thumb = imagecreatefromgif($this->imgs[$i]);
# Gestion des GIFs transparents
$trnprt_indx = imagecolortransparent($thumb);
if ($trnprt_indx >= 0) {
$trnprt_color = imagecolorsforindex($thumb, $trnprt_indx);
$trnprt_indx = imagecolorallocate($minThumb, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
imagefill($minThumb, 0, 0, $trnprt_indx);
imagecolortransparent($minThumb, $trnprt_indx);
}
}
elseif ($ext == 'png'){
$thumb = imagecreatefrompng($this->imgs[$i]);
}
else {
continue;
}
if ($tmbBkgEnabled){
imagefilledrectangle($minThumb, 0, 0, imagesx($minThumb), imagesy($minThumb), imagecolorallocate($minThumb, $tmbBkg[0], $tmbBkg[1], $tmbBkg[2]));
}
$size = getimagesize($this->imgs[$i]);
// On gère l'echelle
if ($size[0] != imagesx($minThumb) && $size[1] != imagesy($minThumb)){
if ($size[0] > $size[1]){
$tmpW = $size[0];
$size[0] = imagesx($minThumb);
$size[1] = imagesx($minThumb) * $size[1] / $tmpW;
if ($size[1] > imagesy($minThumb)){
$tmpH = $size[1];
$size[1] = imagesy($minThumb);
$size[0] = imagesy($minThumb) * $size[0] / $tmpH;
}
} else {
$tmpH = $size[1];
$size[1] = imagesy($minThumb);
$size[0] = imagesy($minThumb) * $size[0] / $tmpH;
if ($size[0] > imagesx($minThumb)){
$tmpW = $size[0];
$size[0] = imagesx($minThumb);
$size[1] = imagesx($minThumb) * $size[1] / $tmpW;
}
}
}
$mW = (imagesx($minThumb) - $size[0]) /2;
$mH = (imagesy($minThumb) - $size[1]) /2;
# on génére la miniature
imagecopyresampled($minThumb, $thumb, $tmbMargin + $mW, $tmbMargin + $mH, 0, 0, imagesx($minThumb) -$tmbMargin*2 - $mW*2, imagesy($minThumb) -$tmbMargin*2 - $mH*2, imagesx($thumb), imagesy($thumb));
if ($tmbBrdEnabled){
imagerectangle($minThumb, 0, 0, imagesx($minThumb)-1, imagesy($minThumb)-1, imagecolorallocate($minThumb, $tmbBrd[0], $tmbBrd[1], $tmbBrd[2]));
}
# On dispose la miniature sur l'image de destination
imagecopymerge($image, $minThumb, $x, $y, 0, 0, imagesx($minThumb), imagesy($minThumb), 100);
# On libère de la mémoire
imagedestroy($thumb);
imagedestroy($minThumb);
# On déplace l'origine de la miniature suivante
$x += $pasX;
$y += $pasY;
}
if ($destBrdEnabled){
imagerectangle($image, 0, 0, imagesx($image)-1, imagesy($image)-1, imagecolorallocate($image, $destBrd[0], $destBrd[1], $destBrd[2]));
}
switch ($destOutput) {
case 'gif':
header('Content-type: image/gif');
imagegif($image, 'img_scripts/'.md5(serialize($this)).'.gif');
imagegif($image);
break;
case 'png':
header('Content-type: image/png');
imagepng($image, 'img_scripts/'.md5(serialize($this)).'.png');
imagepng($image);
break;
default:
case 'jpg':
header('Content-type: image/jpeg');
imagejpeg($image, 'img_scripts/'.md5(serialize($this)).'.jpg');
imagejpeg($image);
break;
}
imagedestroy($image);
// On fait du tri dans les imgs de paniers
$o = opendir($dir);
while ($file = readdir($o)){
if (!is_file($dir.'/'.$file)){
continue;
}
if (filemtime($dir.'/'.$file) < time() - $cacheTime){
unlink($dir.'/'.$file);
}
}
die();
}
}
?>
Conclusion
L'enregistrement en cache a été fait un peu rapidement, désolé :). De même, le recours à la fonction extract dans la méthode PileOfPictures::render() est uniquement dû au fait que j'avais codé cette source à l'origine sans la placer sous forme d'objet.
Historique
- 02 juillet 2008 15:43:00 :
- Correction de la source (erreur de copier-coller)
Sources de la même categorie
Sources en rapport avec celle ci
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Enregistrement d'image depuis un sous dossier. [ par needle ]
Salut tous le monde!!! Voila j'expose mon cas: J'upload une image et la redimensionne avant de l'enregistrer sur le serveur (free) pour cela j'utilise
creer un liens permettant de downloader un groupe d'image [ par french4u ]
OK, je suis un grand debutant et je ne sais pas si cela est possible. voila le truc:J'ai une galerie d'image (donc cree en PHP), c'est sympa de regard
affichage d'image avec bdd [ par hary89 ]
bonjour,Je débute en php, j'utilise Dreamweaver. J'ai créé une base de donnée, ma page d'affichage de photo va chercher les noms de fichier dedant. Mo
affichage d'image [ par keryg ]
Le visiteur rempli un formulaire. Les données récuperées (le nom de la personne)doivent permettre l'affichage de la ou des photos le concernant (le no
Compression d'image automatique [ par w_minisplash_w ]
Salut all, C'est le premier message que je dépose alors je vais essayer d'être clair... Je vous expose le problème: Je veux gérer un systè
Besoin d'un script très simple pour afficher une image (ou un diaporama) et pas de fichier HTML en sortie [ par bisou580 ]
Bonjour,Je suis très débutant en php parceque je ne sais rien, à part mettre un commentaire. :)Voila mon problème :Je cherche à faire un script qui fa
reduction image [ par willinfeo ]
Bonjour à tous,Je travaille sur des photos aeriennes avec une resolution de 2000/2000 pixels pour un territoire de 1km/1km.Chacunes des images av
galeries des photos [ par jaafar50 ]
bonjours , bon j déjat crée un site dynamique d'une agence immobilére alors je ve ke sur chaque bloque un ensemble des images alors que sur chaque im
faire album photos des visiteurs [ par toniosss ]
Bonjour tout le monde, je suis tout nouveau en programmation et c'est pour cela que je vient vous demandez votre aide. Je vient de telecharger un scri
IMAGE [ par aurelbzh ]
Voici mon code pour afficher dans un tableau des image 3 par 3 sur mon site le probleme c ke si je ne rentre qu'une image dans la base les deux otre
|
Téléchargements
Logiciels à télécharger sur le même thème :
|