New file |
0,0 → 1,262 |
<?php |
|
class OdsImageEspece extends JrestService { |
|
private $droits = 0755; |
const PREFIXE = 'get'; |
|
public function OdsImageEspece($config) { |
|
$this->config=$config; |
} |
|
/** |
* Méthode appelée avec une requête de type GET. |
* |
*/ |
function getElement($param = array()) { |
|
$type = $param[0]; |
|
if ($type == '*' || is_numeric($type)) { |
$info = $this->getElementParDefaut($param); |
} else { |
$methode = self::PREFIXE.$type; |
|
echo $methode; |
if (method_exists($this, $methode)) { |
array_shift($param); |
$info = $this->$methode($param); |
} else { |
$this->messages[] = "Le type d'information demandé '$type' n'est pas disponible."; |
} |
} |
|
// Envoi sur la sortie standard |
echo 'OK'; |
} |
|
public function getRecreationMiniatures() { |
|
$dossier = $this->config['appli']['chemin_stockage_images_especes']; |
|
$formats = array('CXS','XS','S','M'); |
|
$dossiers_a_exclure = array(); |
|
foreach (new DirectoryIterator($dossier) as $fichier_ou_dossier) { |
|
if($fichier_ou_dossier->isDot() || $fichier_ou_dossier->isDir()) { |
continue; |
} |
|
if(in_array($fichier_ou_dossier->getBasename(), $dossiers_a_exclure)) { |
continue; |
} |
|
$nom_fichier = $fichier_ou_dossier->getFilename(); |
|
$infos_image_originale = $this->obtenirImageEtInfosPourChemin($fichier_ou_dossier->getPathname()); |
|
// creation de miniatures pour chacuns des formats définis |
foreach($formats as $format) { |
$this->creerEtStockerMiniatureFichierImageSelonFormat($nom_fichier, $infos_image_originale, $format); |
} |
} |
} |
|
public function creerEtStockerMiniatureFichierImageSelonFormat($nom_fichier ,$infos_image_originale, $format = 'O') { |
|
if($format == 'O') { |
// format original : rien à faire |
$image_redimensionnee = $infos_image_originale['image']; |
|
} else { |
// le format carre et une image redimensionnée en gardant son ratio, insérée dans un carré blanc |
if($this->estUnFormatCarre($format)) { |
$image_redimensionnee = $this->creerMiniatureCarree($infos_image_originale, $format); |
|
} else { |
// la minature est une image redimensionnée en gardant son ratio |
$image_redimensionnee = $this->creerMiniature($infos_image_originale, $format); |
} |
} |
|
$taux_compression = 100; |
if($format == 'O') { |
$taux_compression = $this->renvoyerTauxCompressionPourPoids($infos_image_originale['poids_octets']); |
} |
$this->ecrireImageSurDisque($image_redimensionnee, $nom_fichier, $format, $taux_compression); |
|
return true; |
} |
|
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, |
$debut_largeur_a_copier, |
$debut_hauteur_a_copier, |
$largeur_redimension, |
$hauteur_redimension, |
$infos_image_originale['largeur'], |
$infos_image_originale['hauteur'] |
); |
|
return $image_redimensionnee; |
} |
|
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']); |
$image_redimensionnee = $this->creerImageRedimensionnee($informations_images, $taille_image_redimensionnee['hauteur'], $taille_image_redimensionnee['largeur']); |
|
return $image_redimensionnee; |
} |
|
public function creerMiniatureCarree($informations_images, $format) { |
|
$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format); |
$cote_carre = $taille_reference_pour_format['largeur']; |
|
$image_redimensionnee_avec_rapport = $this->creerMiniature($informations_images, $format); |
$taille_redimensionnee_avec_rapport = $this->calculerTailleImage($informations_images, $taille_reference_pour_format['hauteur']); |
|
if($this->estPaysage($informations_images)) { |
$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 ; |
} |
|
$image_carre_cible = $this->renvoyerEtCreerImageCarreeSelonFormat($cote_carre); |
|
imagecopy($image_carre_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'] |
); |
|
return $image_carre_cible; |
} |
|
public function obtenirImageEtInfosPourChemin($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); |
$image_et_infos['image'] = imagecreatefromjpeg($chemin_fichier); |
|
return $image_et_infos; |
} |
|
public function obtenirDimensionsPourFormat($format) { |
|
$dimensions = array('largeur' => 0, 'hauteur' => 0); |
|
if(isset($this->config['appli']['format_'.$format])) { |
|
$format_largeur_hauteur = split('_', $this->config['appli']['format_'.$format]); |
|
$dimensions['largeur'] = $format_largeur_hauteur[0]; |
$dimensions['hauteur'] = $format_largeur_hauteur[1]; |
} |
|
return $dimensions; |
|
} |
|
public function calculerTailleImage($informations_images, $taille_max) { |
|
$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) ; |
|
} 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 estUnFormatCarre($format) { |
|
return (strpos($format,'C') === 0); |
} |
|
public function estPaysage($informations_images) { |
return $informations_images['largeur'] > $informations_images['hauteur']; |
} |
|
public function estPortait($informations_images) { |
return $informations_images['largeur'] < $informations_images['hauteur']; |
} |
|
public function renvoyerTauxCompressionPourPoids($poids_octets) { |
|
$poids_max_octets = $this->config['appli']['taille_max']; |
|
$ratio_compression = 100 ; |
|
if($poids_octets >= $poids_max_octets) { |
$ratio_compression = 85 ; |
} |
|
return $ratio_compression; |
} |
|
public function ecrireImageSurDisque($image, $nom_fichier, $format, $compression) { |
|
umask(0); |
|
$chemin_sur_serveur_final = $this->config['appli']['chemin_stockage_images_especes'].'/'.$format; |
|
if(!is_dir($chemin_sur_serveur_final)) { |
mkdir($chemin_sur_serveur_final,$this->droits); |
} |
|
if(file_exists($chemin_sur_serveur_final.'/'.$nom_fichier)) { |
unlink($chemin_sur_serveur_final.'/'.$nom_fichier); |
} |
|
imagejpeg($image, $chemin_sur_serveur_final.'/'.$nom_fichier); |
chmod($chemin_sur_serveur_final.'/'.$nom_fichier,$this->droits); |
|
} |
|
public function renvoyerEtCreerImageCarreeSelonFormat($cote) { |
|
$r = $g = $b = 255; |
|
if(isset($this->config['appli']['couleur_fond_carre'])) { |
$rgb = $this->config['appli']['couleur_fond_carre']; |
|
$r = base_convert(substr($rgb,0,2), 16, 10); |
$g = base_convert(substr($rgb,2,2), 16, 10); |
$b = base_convert(substr($rgb,4,2), 16, 10); |
} |
|
$image_couleur = imagecreatetruecolor($cote, $cote); |
$couleur = imagecolorallocate($image_couleur, $r, $g, $b); |
imagefilledrectangle($image_couleur, 0, 0, $cote, $cote, $couleur); |
|
return $image_couleur; |
} |
|
public function detruireImageEnMemoire($image) { |
imagedestroy($image); |
} |
} |
?> |