Subversion Repositories eFlore/Applications.cel

Compare Revisions

Ignore whitespace Rev 671 → Rev 672

/trunk/jrest/lib/ImageRecreation.php
1,7 → 1,7
<?php
Class ImageRecreation {
private $droits = 0755;
private $droits = 0705;
private $formats = array('CRX2S','CXS','CS','CRS','XS','S','M','L','XL','X2L','X3L');
const MODE_GD = 'gd';
const MODE_IMAGEMAGICK = 'imagemagick';
9,13 → 9,12
private $verbose = true;
 
public function ImageRecreation($config) {
 
$this->config=$config;
public function __construct($config) {
$this->config = $config;
$this->mode = self::MODE_GD;
if (extension_loaded('imagick')) {
putenv("MAGICK_TEMPORARY_PATH=".$this->config['cel_db']['chemin_stockage_temp']);
putenv('MAGICK_TEMPORARY_PATH='.$this->config['cel_db']['chemin_stockage_temp']);
$this->mode = self::MODE_IMAGEMAGICK;
}
}
25,14 → 24,11
}
public function regenererMiniaturesIntervalle($params) {
$id_debut = $params[0];
$id_fin = $params[1];
 
if (is_numeric($id_debut) && is_numeric($id_fin)) {
for ($i = $id_debut; $i <= $id_fin; $i++) {;
$tab_param = array($i);
$this->regenererMiniaturesPourId($tab_param);
}
40,7 → 36,6
}
public function regenererMiniaturesPourId($params) {
 
$id = $params[0];
 
if (!is_numeric($id)) {
64,12 → 59,10
}
public function itererRecursivement($dossier) {
// on ne parse que le dossier des images originales
$dossiers_a_exclure = $this->getFormats();
foreach (new DirectoryIterator($dossier) as $fichier_ou_dossier) {
if ($fichier_ou_dossier->isDot()) {
continue;
}
79,11 → 72,8
}
if ($fichier_ou_dossier->isDir()) {
$this->itererRecursivement($fichier_ou_dossier->getPathname());
} else {
$nom_fichier = $fichier_ou_dossier->getFilename();
$infos_image_originale = $this->obtenirImageEtInfosPourChemin($fichier_ou_dossier->getPathname());
91,7 → 81,6
// creation de miniatures pour chacuns des formats définis
foreach($this->formats as $format) {
$this->creerEtStockerMiniatureFichierImageSelonFormat($id, $infos_image_originale, $format);
}
}
99,7 → 88,6
}
public function creerMiniatureImageSelonFormat($infos_image_originale, $format = 'O') {
if ($format == 'O') {
// format original : rien à faire
$image_redimensionnee = $infos_image_originale['image'];
106,7 → 94,6
} else {
if ($this->estUnFormatRogne($format)) {
if ($this->mode == self::MODE_IMAGEMAGICK) {
// si l'on dispose de la librairie imageMagick
// on applique l'algorithme d'auto détection de sujets
129,7 → 116,6
}
public function creerEtStockerMiniatureFichierImageSelonFormat($id ,$infos_image_originale, $format = 'O') {
$image_redimensionnee = $this->creerMiniatureImageSelonFormat($infos_image_originale, $format);
$taux_compression = $this->renvoyerTauxCompressionPourPoids($infos_image_originale['poids_octets']);
139,17 → 125,16
}
public function creerImageRedimensionnee($infos_image_originale, $hauteur_redimension, $largeur_redimension) {
$image_redimensionnee = imagecreatetruecolor($largeur_redimension, $hauteur_redimension);
imagecopyresampled($image_redimensionnee,
$infos_image_originale['image'],
0, 0,
0, 0,
$largeur_redimension,
$hauteur_redimension,
$infos_image_originale['largeur'],
$infos_image_originale['hauteur']
$infos_image_originale['image'],
0, 0,
0, 0,
$largeur_redimension,
$hauteur_redimension,
$infos_image_originale['largeur'],
$infos_image_originale['hauteur']
);
return $image_redimensionnee;
156,7 → 141,6
}
public function creerMiniature($informations_images, $format) {
$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
$taille_image_redimensionnee = $this->calculerTailleImage($informations_images, $taille_reference_pour_format['hauteur']);
166,7 → 150,6
}
public function creerMiniatureCarree($informations_image, $format) {
$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
$cote_carre = $taille_reference_pour_format['largeur'];
174,18 → 157,18
$taille_redimensionnee_avec_rapport = $this->calculerTailleImage($informations_image, $taille_reference_pour_format['hauteur']);
if ($this->estPaysage($informations_image)) {
$debut_largeur_a_copier = 0 ;
$debut_hauteur_a_copier = ($cote_carre - $taille_redimensionnee_avec_rapport['hauteur'])/2 ;
$debut_largeur_a_copier = 0 ;
$debut_hauteur_a_copier = ($cote_carre - $taille_redimensionnee_avec_rapport['hauteur'])/2 ;
} else {
$debut_largeur_a_copier = ($cote_carre - $taille_redimensionnee_avec_rapport['largeur'])/2 ;
$debut_hauteur_a_copier = 0 ;
$debut_largeur_a_copier = ($cote_carre - $taille_redimensionnee_avec_rapport['largeur'])/2 ;
$debut_hauteur_a_copier = 0 ;
}
$image_carre_blanc_cible = $this->renvoyerEtCreerImageCarreeBlancheSelonFormat($cote_carre);
imagecopy($image_carre_blanc_cible, $image_redimensionnee_avec_rapport,
$debut_largeur_a_copier ,$debut_hauteur_a_copier, 0, 0,
$taille_redimensionnee_avec_rapport['largeur'], $taille_redimensionnee_avec_rapport['hauteur']
$debut_largeur_a_copier ,$debut_hauteur_a_copier, 0, 0,
$taille_redimensionnee_avec_rapport['largeur'], $taille_redimensionnee_avec_rapport['hauteur']
);
 
return $image_carre_blanc_cible;
192,58 → 175,57
}
public function creerMiniatureCarreeRognee($informations_image, $format) {
$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
$cote_carre = $taille_reference_pour_format['largeur'];
$cote_carre_non_redimensionne = 0;
if ($this->estPaysage($informations_image)) {
$cote_carre_non_redimensionne = $informations_image['hauteur'];
$debut_largeur_a_copier = ($informations_image['hauteur'] - $cote_carre)/2 ;
$cote_carre_non_redimensionne = $informations_image['hauteur'];
$debut_largeur_a_copier = ($informations_image['hauteur'] - $cote_carre)/2 ;
$debut_hauteur_a_copier = 0;
if($debut_largeur_a_copier <= 0) {
$debut_largeur_a_copier = 0;
}
$nb_pixels_largeur_a_copier = $cote_carre_non_redimensionne;
$nb_pixels_hauteur_a_copier = $cote_carre_non_redimensionne;
} else {
$cote_carre_non_redimensionne = $informations_image['largeur'];
$debut_largeur_a_copier = 0 ;
$debut_hauteur_a_copier = ($informations_image['largeur'] - $cote_carre)/2;
if($debut_hauteur_a_copier <= 0) {
$debut_hauteur_a_copier = 0;
if($debut_largeur_a_copier <= 0) {
$debut_largeur_a_copier = 0;
}
$nb_pixels_largeur_a_copier = $cote_carre_non_redimensionne;
$nb_pixels_hauteur_a_copier = $cote_carre_non_redimensionne;
} else {
$cote_carre_non_redimensionne = $informations_image['largeur'];
$debut_largeur_a_copier = 0 ;
$debut_hauteur_a_copier = ($informations_image['largeur'] - $cote_carre)/2;
if($debut_hauteur_a_copier <= 0) {
$debut_hauteur_a_copier = 0;
}
$nb_pixels_largeur_a_copier = $cote_carre_non_redimensionne;
$nb_pixels_hauteur_a_copier = $cote_carre_non_redimensionne;
}
$nb_pixels_largeur_a_copier = $cote_carre_non_redimensionne;
$nb_pixels_hauteur_a_copier = $cote_carre_non_redimensionne;
}
$image_carre_temporaire = imagecreatetruecolor($cote_carre_non_redimensionne, $cote_carre_non_redimensionne);
imagecopyresampled($image_carre_temporaire,
$informations_image['image'],
0, 0,
$debut_largeur_a_copier,
$debut_hauteur_a_copier,
$cote_carre_non_redimensionne,
$cote_carre_non_redimensionne,
$nb_pixels_largeur_a_copier,
$nb_pixels_hauteur_a_copier
$informations_image['image'],
0, 0,
$debut_largeur_a_copier,
$debut_hauteur_a_copier,
$cote_carre_non_redimensionne,
$cote_carre_non_redimensionne,
$nb_pixels_largeur_a_copier,
$nb_pixels_hauteur_a_copier
);
$image_redimensionnee = imagecreatetruecolor($cote_carre, $cote_carre);
imagecopyresampled($image_redimensionnee,
$image_carre_temporaire,
0, 0,
0, 0,
$cote_carre,
$cote_carre,
$cote_carre_non_redimensionne,
$cote_carre_non_redimensionne
$image_carre_temporaire,
0, 0,
0, 0,
$cote_carre,
$cote_carre,
$cote_carre_non_redimensionne,
$cote_carre_non_redimensionne
);
 
return $image_redimensionnee;
250,7 → 232,6
}
public function stockerFichierEtCreerMiniatures($fichier, $id) {
$chemin_base_fichier = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, 'O');
$nom_fichier = $this->convertirIdBddVersNomFichier($id, 'O');
286,7 → 267,6
}
public function stockerImageExterne($chemin_fichier_temp, $chemin_destination) {
if(is_uploaded_file($chemin_fichier_temp)) {
$deplacement = move_uploaded_file($chemin_fichier_temp, $chemin_destination);
} else {
297,20 → 277,13
}
public function creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format) {
$chemin_sur_serveur_final = $this->obtenirDossierPourFormat($id, $format);
$chemin_sur_serveur_final = $this->obtenirDossierPourFormat($id,$format);
if (!file_exists($chemin_sur_serveur_final))
{
if (!file_exists($chemin_sur_serveur_final)) {
umask(0);
if (mkdir($chemin_sur_serveur_final,$this->droits, true)) {
chmod($chemin_sur_serveur_final,$this->droits);
}
else
{
if (!mkdir($chemin_sur_serveur_final, $this->droits, true)) {
$erreur = 'ERROR : probleme durant l\'écriture du dossier '.$format.' \n' ;
echo $erreur;
$this->logger('CEL_bugs',$erreur);
$this->logger('CEL_bugs', $erreur);
return false;
}
}
319,19 → 292,15
}
public function obtenirDossierPourFormat($id, $format) {
$chemin_base = $this->config['cel_db']['chemin_images'];
$chemin_sur_serveur = $chemin_base ;
$chemin_sur_serveur = $chemin_base;
$id = sprintf('%09s', $id) ;
$id = wordwrap($id, 3 , '_', true) ;
$id = sprintf('%09s', $id);
$id = wordwrap($id, 3 , '_', true);
$niveauDossier = split("_", $id) ;
list($dossierNiveau1, $dossierNiveau2) = explode('_', $id);
$dossierNiveau1 = $niveauDossier[0] ;
$dossierNiveau2 = $niveauDossier[1] ;
$chemin_sur_serveur_final = $chemin_sur_serveur.'/'.$dossierNiveau1.'/'.$dossierNiveau2.'/'.$format;
return $chemin_sur_serveur_final;
338,7 → 307,6
}
public function obtenirCheminImageOriginale($id_image) {
$nom = $this->convertirIdBddVersNomFichier($id_image, 'O');
$dossier = $this->obtenirDossierPourFormat($id_image,'O');
346,17 → 314,14
}
public function obtenirImageEtInfosPourId($id_image) {
$chemin_image_o = $this->obtenirCheminImageOriginale($id_image);
return $this->obtenirImageEtInfosPourChemin($chemin_image_o);
}
public function obtenirImageEtInfosPourChemin($chemin_fichier) {
$image_et_infos = false;
if(file_exists($chemin_fichier)) {
if (file_exists($chemin_fichier)) {
$image_et_infos = array();
list($image_et_infos['largeur'], $image_et_infos['hauteur']) = getimagesize($chemin_fichier);
$image_et_infos['poids_octets'] = filesize($chemin_fichier);
368,38 → 333,30
}
public function obtenirDimensionsPourFormat($format) {
$dimensions = array('largeur' => 0, 'hauteur' => 0);
$dimensions = array('largeur' => 0, 'hauteur' => 0);
if (isset($this->config['cel_db']['format_'.$format])) {
$format_largeur_hauteur = split('_', $this->config['cel_db']['format_'.$format]);
$dimensions['largeur'] = $format_largeur_hauteur[0];
$dimensions['hauteur'] = $format_largeur_hauteur[1];
list($dimensions['largeur'], $dimensions['hauteur']) = explode('_', $this->config['cel_db']['format_'.$format]);
}
return $dimensions;
}
public function calculerTailleImage($informations_images, $taille_max) {
$HL_redimension = array();
$HL_redimension = array();
if ($this->estPaysage($informations_images)) {
$rapport = $informations_images['hauteur']/$informations_images['largeur'] ;
$HL_redimension['largeur'] = round($taille_max) ;
$HL_redimension['hauteur'] = round($taille_max*$rapport) ;
if ($this->estPaysage($informations_images)) {
$rapport = $informations_images['hauteur']/$informations_images['largeur'] ;
$HL_redimension['largeur'] = round($taille_max) ;
$HL_redimension['hauteur'] = round($taille_max*$rapport) ;
} else {
$rapport = $informations_images['largeur']/$informations_images['hauteur'] ;
$HL_redimension['hauteur'] = round($taille_max) ;
$HL_redimension['largeur'] = round($taille_max*$rapport) ;
}
return $HL_redimension;
} else {
$rapport = $informations_images['largeur']/$informations_images['hauteur'] ;
$HL_redimension['hauteur'] = round($taille_max) ;
$HL_redimension['largeur'] = round($taille_max*$rapport) ;
}
return $HL_redimension;
}
public function getFormats() {
407,12 → 364,10
}
public function estUnFormatCarre($format) {
return (strpos($format,'C') === 0);
}
public function estUnFormatRogne($format) {
 
return (strpos($format,'R') === 1);
}
425,7 → 380,6
}
public function renvoyerTauxCompressionPourPoids($poids_octets) {
 
$poids_max_octets = $this->config['cel_db']['taille_max'];
$ratio_compression = 100 ;
438,8 → 392,7
}
public function convertirIdBddVersNomFichier($id, $format, $extension = 'jpg') {
// creation du format original
// creation du format original
$id_avec_zeros = sprintf('%09s', $id) ;
$id_avec_zeros_underscores = wordwrap($id_avec_zeros, 3 , '_', true) ;
449,7 → 402,6
}
public function convertirBaseNomFichierVersIdBdd($nom_fichier, $formats) {
$nom_fichier_sans_extension = trim($nom_fichier, '.jpg');
foreach($formats as $format) {
465,7 → 417,6
}
public function ecrireImageSurDisque($image_binaire, $id, $format, $compression = 100) {
umask(0);
$chemin_sur_serveur_final = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format);
481,7 → 432,6
}
public function ecrireImageSurDisqueAvecMeta($image_a_stocker, $compression = 100) {
 
$image_a_stocker = new Imagick($image_a_stocker);
// l'utilisation d'image magick préserve les métadonnées lors d'une recompression
495,7 → 445,6
}
public function renvoyerEtCreerImageCarreeBlancheSelonFormat($cote) {
$image_blanche = imagecreatetruecolor($cote, $cote);
$blanc = imagecolorallocate($image_blanche, 255, 255, 255);
imagefilledrectangle($image_blanche, 0, 0, $cote, $cote, $blanc);
508,7 → 457,6
}
public function detruireImageSurDisque($id) {
$formats = $this->getFormats();
// on detruit aussi l'image originale
541,8 → 489,7
* $informations_image - le tableau d'informations sur l'image tel que renvoyé par la fonction obtenirImageEtInfosPourChemin
* $format - le format (ex. : CS, XS, XL, CRS)
*/
function opticrop($informations_image, $format) {
 
public function opticrop($informations_image, $format) {
umask(0);
$nom_temp = md5(time());
603,8 → 550,7
// crop width to target AR
$wcrop0 = round($ar*$hauteur_image_originale);
$hcrop0 = $hauteur_image_originale;
}
else {
} else {
// crop height to target AR
$wcrop0 = $largeur_image_originale;
$hcrop0 = round($largeur_image_originale/$ar);
687,6 → 633,5
return $image_sortie;
}
}
?>