[ Index ] |
|
Code source de SPIP 1.8.3 |
1 <?php 2 3 /***************************************************************************\ 4 * SPIP, Systeme de publication pour l'internet * 5 * * 6 * Copyright (c) 2001-2005 * 7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James * 8 * * 9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. * 10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. * 11 \***************************************************************************/ 12 13 14 // 15 // Ce fichier ne sera execute qu'une fois 16 if (defined("_ECRIRE_INC_LOGOS")) return; 17 define("_ECRIRE_INC_LOGOS", "1"); 18 19 20 function cherche_image_nommee($nom, $formats = array ('gif', 'jpg', 'png')) { 21 // _DIR_IMG contient deja le ../ dans ecrire (PREFIX1 22 // if (ereg("^../",$nom)) $nom = substr($nom,3); 23 if (ereg("^" . _DIR_IMG, $nom)) { 24 $nom = substr($nom,strlen(_DIR_IMG)); 25 } 26 $pos = strrpos($nom, "/"); 27 if ($pos > 0) { 28 $chemin = substr($nom, 0, $pos+1); 29 $nom = substr($nom, $pos+1); 30 } else { 31 $chemin = ""; 32 } 33 34 reset($formats); 35 while (list(, $format) = each($formats)) { 36 $d = _DIR_IMG . "$chemin$nom.$format"; 37 if (@file_exists($d)){ 38 return array(_DIR_IMG."$chemin", $nom, $format); 39 } 40 } 41 } 42 43 44 45 function decrire_logo($racine) { 46 global $connect_id_auteur; 47 48 if ($img = cherche_image_nommee($racine)) { 49 list($dir, $racine, $fmt) = $img; 50 $fid = $dir . "$racine.".$fmt; 51 if ($taille = @getimagesize($fid)) 52 $xy = _T('info_largeur_vignette', array('largeur_vignette' => $taille[0], 'hauteur_vignette' => $taille[1])); 53 54 return array("$racine.$fmt", $xy); 55 } 56 return ''; 57 } 58 59 60 function afficher_boite_logo($type, $id_objet, $id, $texteon, $texteoff) { 61 global $options, $spip_display; 62 63 $logon = $type.'on'.$id; 64 $logoff = $type.'off'.$id; 65 66 if ($spip_display != 4) { 67 68 echo "<p>"; 69 debut_cadre_relief("image-24.gif"); 70 echo "<div class='verdana1' style='text-align: center;'>"; 71 $desc = decrire_logo($logon); 72 afficher_logo($logon, $texteon, $desc, $id_objet, $id); 73 74 if ($desc AND $texteoff) { 75 echo "<br /><br />"; 76 $desc = decrire_logo($logoff); 77 afficher_logo($logoff, $texteoff, $desc, $id_objet, $id); 78 } 79 80 echo "</div>"; 81 fin_cadre_relief(); 82 echo "</p>"; 83 } 84 } 85 86 87 function afficher_logo($racine, $titre, $logo, $id_objet, $id) { 88 global $connect_id_auteur; 89 global $couleur_foncee, $couleur_claire; 90 global $clean_link; 91 92 include_ecrire ('inc_admin.php3'); 93 94 $redirect = $clean_link->getUrl(); 95 96 echo "<b>"; 97 echo bouton_block_invisible(md5($titre)); 98 echo $titre; 99 echo "</b>"; 100 echo "<font size=1>"; 101 102 if ($logo) { 103 list ($fichier, $taille) = $logo; 104 $hash = calculer_action_auteur("supp_logo $fichier"); 105 106 echo "<p><center><div><a href='"._DIR_IMG.$fichier."'>"; 107 echo reduire_image_logo(_DIR_IMG.$fichier, 170); 108 echo "</a></div>"; 109 echo debut_block_invisible(md5($titre)); 110 echo $taille; 111 echo "\n<br />[<a href='../spip_image.php3?"; 112 echo "$id_objet=$id&"; 113 echo "image_supp=$fichier&hash_id_auteur=$connect_id_auteur&hash=$hash&redirect=".urlencode($redirect)."'>"._T('lien_supprimer')."</A>]"; 114 echo fin_block(); 115 echo "</center></p>"; 116 } 117 else { 118 $hash = calculer_action_auteur("ajout_logo $racine"); 119 echo debut_block_invisible(md5($titre)); 120 121 echo "\n\n<FORM ACTION='../spip_image.php3' METHOD='POST' 122 ENCTYPE='multipart/form-data'>"; 123 echo "\n<INPUT NAME='redirect' TYPE=Hidden VALUE='$redirect'>"; 124 echo "\n<INPUT NAME='$id_objet' TYPE=Hidden VALUE='$id'>"; 125 echo "\n<INPUT NAME='hash_id_auteur' TYPE=Hidden VALUE='$connect_id_auteur'>"; 126 echo "\n<INPUT NAME='hash' TYPE=Hidden VALUE='$hash'>"; 127 echo "\n<INPUT NAME='ajout_logo' TYPE=Hidden VALUE='oui'>"; 128 echo "\n<INPUT NAME='logo' TYPE=Hidden VALUE='$racine'>"; 129 if (tester_upload()){ 130 echo "\n"._T('info_telecharger_nouveau_logo')."<BR>"; 131 echo "\n<INPUT NAME='image' TYPE=File CLASS='forml' style='font-size:9px;' SIZE=15>"; 132 echo "\n <div align='right'><INPUT NAME='ok' TYPE=Submit VALUE='"._T('bouton_telecharger')."' CLASS='fondo' style='font-size:9px;'></div>"; 133 } else { 134 135 $myDir = opendir(_DIR_TRANSFERT); 136 while($entryName = readdir($myDir)){ 137 if (!ereg("^\.",$entryName) AND eregi("(gif|jpg|png)$",$entryName)){ 138 $entryName = addslashes($entryName); 139 $afficher .= "\n<OPTION VALUE='" . 140 _DIR_TRANSFERT . 141 "$entryName'>$entryName"; 142 } 143 } 144 closedir($myDir); 145 146 if (strlen($afficher) > 10){ 147 echo "\n"._T('info_selectionner_fichier_2'); 148 echo "\n<SELECT NAME='image' CLASS='forml' SIZE=1>"; 149 echo $afficher; 150 echo "\n</SELECT>"; 151 echo "\n <INPUT NAME='ok' TYPE=Submit VALUE='"._T('bouton_choisir')."' CLASS='fondo'>"; 152 } else { 153 echo _T('info_installer_images_dossier'); 154 } 155 156 } 157 echo fin_block(); 158 echo "</FORM>\n"; 159 } 160 161 echo "</font>"; 162 } 163 164 165 // 166 // Creation automatique d'une vignette 167 // 168 169 // Calculer le ratio 170 function image_ratio ($srcWidth, $srcHeight, $maxWidth, $maxHeight) { 171 $ratioWidth = $srcWidth/$maxWidth; 172 $ratioHeight = $srcHeight/$maxHeight; 173 174 if ($ratioWidth <=1 AND $ratioHeight <=1) { 175 $destWidth = $srcWidth; 176 $destHeight = $srcHeight; 177 } else if ($ratioWidth < $ratioHeight) { 178 $destWidth = $srcWidth/$ratioHeight; 179 $destHeight = $maxHeight; 180 } 181 else { 182 $destWidth = $maxWidth; 183 $destHeight = $srcHeight/$ratioWidth; 184 } 185 return array (ceil($destWidth), ceil($destHeight), 186 max($ratioWidth,$ratioHeight)); 187 } 188 189 function creer_vignette($image, $maxWidth, $maxHeight, $format, $destdir, $destfile, $process='AUTO', $force=false, $test_cache_only = false) { 190 global $convert_command, $pnmscale_command; 191 // ordre de preference des formats graphiques pour creer les vignettes 192 // le premier format disponible, selon la methode demandee, est utilise 193 if ($format == 'png') 194 $formats_sortie = array('png','jpg','gif'); 195 else 196 $formats_sortie = array('jpg','png','gif'); 197 198 if ($process == 'AUTO') 199 $process = lire_meta('image_process'); 200 201 // liste des formats qu'on sait lire 202 $formats_graphiques = lire_meta('formats_graphiques'); 203 204 // si le doc n'est pas une image, refuser 205 if (!$force AND !eregi(",$format,", ",$formats_graphiques,")) 206 return; 207 // normalement il a ete cree 208 if ($destdir) { 209 $destdir = creer_repertoire(_DIR_IMG, $destdir); 210 } 211 $destination = _DIR_IMG . $destdir . $destfile; 212 # spip_log("$dir $destination"); 213 // chercher un cache 214 foreach (array('gif','jpg','png') as $fmt) 215 if (@file_exists($destination.'.'.$fmt)) { 216 $vignette = $destination.'.'.$fmt; 217 if ($force) @unlink($vignette); 218 } 219 220 if ($test_cache_only AND !$vignette) return; 221 222 // utiliser le cache ? 223 if (!$test_cache_only) 224 if ($force OR !$vignette OR (@filemtime($vignette) < @filemtime($image))) { 225 226 $creation = true; 227 // calculer la taille 228 if ($srcsize = @getimagesize($image)) { 229 $srcWidth=$srcsize[0]; 230 $srcHeight=$srcsize[1]; 231 list ($destWidth,$destHeight) = image_ratio($srcWidth, $srcHeight, $maxWidth, $maxHeight); 232 } else if ($process == 'convert' OR $process == 'imagick') { 233 $destWidth = $maxWidth; 234 $destHeight = $maxHeight; 235 } else { 236 spip_log("echec $process sur $image"); 237 return; 238 } 239 240 // Si l'image est de la taille demandee (ou plus petite), simplement 241 // la retourner 242 if ($srcWidth 243 AND $srcWidth <= $maxWidth AND $srcHeight <= $maxHeight) { 244 $vignette = $destination.'.'.preg_replace(',^.*\.,', '', $image); 245 @copy($image, $vignette); 246 } 247 248 // imagemagick en ligne de commande 249 else if ($process == 'convert') { 250 $format = $formats_sortie[0]; 251 $vignette = $destination.".".$format; 252 $commande = "$convert_command -size $destWidth}x$destHeight} ./$image -geometry $destWidth}x$destHeight} +profile \"*\" ./".escapeshellcmd($vignette); 253 spip_log($commande); 254 exec($commande); 255 if (!@file_exists($vignette)) { 256 spip_log("echec convert sur $vignette"); 257 return; // echec commande 258 } 259 } 260 else 261 // imagick (php4-imagemagick) 262 if ($process == 'imagick') { 263 $format = $formats_sortie[0]; 264 $vignette = "$destination.".$format; 265 $handle = imagick_readimage($image); 266 imagick_resize($handle, $destWidth, $destHeight, IMAGICK_FILTER_LANCZOS, 0.75); 267 imagick_write($handle, $vignette); 268 if (!@file_exists($vignette)) { 269 spip_log("echec imagick sur $vignette"); 270 return; 271 } 272 } 273 else if ($process == "netpbm") { 274 $format_sortie = "jpg"; 275 $vignette = $destination.".".$format_sortie; 276 $pnmtojpeg_command = ereg_replace("pnmscale", "pnmtojpeg", $pnmscale_command); 277 if ($format == "jpg") { 278 279 $jpegtopnm_command = ereg_replace("pnmscale", "jpegtopnm", $pnmscale_command); 280 281 exec("$jpegtopnm_command $image | $pnmscale_command -width $destWidth | $pnmtojpeg_command > $vignette"); 282 if (!@file_exists($vignette)) { 283 spip_log("echec netpbm-jpg sur $vignette"); 284 return; 285 } 286 } else if ($format == "gif") { 287 $giftopnm_command = ereg_replace("pnmscale", "giftopnm", $pnmscale_command); 288 exec("$giftopnm_command $image | $pnmscale_command -width $destWidth | $pnmtojpeg_command > $vignette"); 289 if (!@file_exists($vignette)) { 290 spip_log("echec netpbm-gif sur $vignette"); 291 return; 292 } 293 } else if ($format == "png") { 294 $pngtopnm_command = ereg_replace("pnmscale", "pngtopnm", $pnmscale_command); 295 exec("$pngtopnm_command $image | $pnmscale_command -width $destWidth | $pnmtojpeg_command > $vignette"); 296 if (!@file_exists($vignette)) { 297 spip_log("echec netpbm-png sur $vignette"); 298 return; 299 } 300 } 301 } 302 // gd ou gd2 303 else if ($process == 'gd1' OR $process == 'gd2') { 304 305 // Recuperer l'image d'origine 306 if ($format == "jpg") { 307 $srcImage = @ImageCreateFromJPEG($image); 308 } 309 else if ($format == "gif"){ 310 $srcImage = @ImageCreateFromGIF($image); 311 } 312 else if ($format == "png"){ 313 $srcImage = @ImageCreateFromPNG($image); 314 } 315 if (!$srcImage) { 316 spip_log("echec gd1/gd2"); 317 return; 318 } 319 // Choisir le format destination 320 // - on sauve de preference en JPEG (meilleure compression) 321 // - pour le GIF : les GD recentes peuvent le lire mais pas l'ecrire 322 # bug : gd_formats contient la liste des fichiers qu'on sait *lire*, 323 # pas *ecrire* 324 $gd_formats = lire_meta("gd_formats"); 325 foreach ($formats_sortie as $fmt) { 326 if (ereg($fmt, $gd_formats)) { 327 if ($format <> "gif" OR $GLOBALS['flag_ImageGif']) 328 $destFormat = $fmt; 329 break; 330 } 331 } 332 333 if (!$destFormat) { 334 spip_log("pas de format pour $image"); 335 return; 336 } 337 338 // Initialisation de l'image destination 339 if ($process == 'gd2' AND $destFormat != "gif") 340 $destImage = ImageCreateTrueColor($destWidth, $destHeight); 341 if (!$destImage) 342 $destImage = ImageCreate($destWidth, $destHeight); 343 344 // Recopie de l'image d'origine avec adaptation de la taille 345 $ok = false; 346 if (($process == 'gd2') AND function_exists('ImageCopyResampled')) { 347 if ($format == "gif") { 348 // Si un GIF est transparent, 349 // fabriquer un PNG transparent 350 $transp = imagecolortransparent($srcImage); 351 if ($transp > 0) $destFormat = "png"; 352 } 353 if ($destFormat == "png") { 354 // Conserver la transparence 355 if (function_exists("imageAntiAlias")) imageAntiAlias($destImage,true); 356 @imagealphablending($destImage, false); 357 @imagesavealpha($destImage,true); 358 } 359 $ok = @ImageCopyResampled($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight); 360 } 361 if (!$ok) 362 $ok = ImageCopyResized($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight); 363 364 // Sauvegarde de l'image destination 365 $vignette = "$destination.$destFormat"; 366 $format = $destFormat; 367 if ($destFormat == "jpg") 368 ImageJPEG($destImage, $vignette, 70); 369 else if ($destFormat == "gif") 370 ImageGIF($destImage, $vignette); 371 else if ($destFormat == "png") 372 ImagePNG($destImage, $vignette); 373 374 ImageDestroy($srcImage); 375 ImageDestroy($destImage); 376 } 377 } 378 379 $size = @getimagesize($vignette); 380 $retour['width'] = $largeur = $size[0]; 381 $retour['height'] = $hauteur = $size[1]; 382 $retour['fichier'] = $vignette; 383 $retour['format'] = $format; 384 $retour['date'] = @filemtime($vignette); 385 386 387 // renvoyer l'image 388 return $retour; 389 } 390 391 392 393 // 394 // Retourner taille d'une image 395 // pour les filtres |largeur et |hauteur 396 // 397 function taille_image($img) { 398 399 if (eregi("width *= *['\"]?( *[0-9]+ *)", $img, $regs)) 400 $srcWidth = intval(trim($regs[1])); 401 if (eregi("height *= *['\"]?( *[0-9]+ *)", $img, $regs)) 402 $srcHeight = intval(trim($regs[1])); 403 404 // recuperer le nom du fichier 405 if (eregi("src='([^']+)'", $img, $regs)) $logo = $regs[1]; 406 if (!$logo) $logo = $img; 407 408 if (!$srcWidth 409 AND $srcsize = @getimagesize($logo)) 410 $srcWidth = $srcsize[0]; 411 412 if (!$srcHeight 413 AND $srcsize = @getimagesize($logo)) 414 $srcHeight = $srcsize[1]; 415 416 return array($srcHeight, $srcWidth); 417 418 } 419 420 421 422 423 // 424 // Reduire la taille d'un logo 425 // [(#LOGO_ARTICLE||reduire_image{100,60})] 426 // 427 428 // Cette fonction accepte en entree un nom de fichier ou un tag <img ...> 429 430 function reduire_image_logo($img, $taille = -1, $taille_y = -1) { 431 432 // Determiner la taille x,y maxi 433 if ($taille == -1) { 434 $taille = lire_meta('taille_preview'); 435 if (!$taille) 436 $taille = 150; 437 } 438 if ($taille_y == -1) 439 $taille_y = $taille; 440 441 if ($taille == 0 AND $taille_y > 0) 442 $taille = 100000; # {0,300} -> c'est 300 qui compte 443 else 444 if ($taille > 0 AND $taille_y == 0) 445 $taille_y = 100000; # {300,0} -> c'est 300 qui compte 446 else if ($taille == 0 AND $taille_y == 0) 447 return ''; 448 449 // recuperer le nom du fichier 450 if ($src = extraire_attribut($img, 'src')) 451 $logo = $src; 452 else 453 $logo = $img; 454 if (!$logo) return ''; 455 456 // Si c'est une image distante, la recuperer (si possible) 457 if (!$local = copie_locale($logo)) { 458 spip_log("pas de version locale de $logo"); 459 return $img; 460 } 461 $logo = $local; 462 463 464 $attributs = ''; 465 466 // preserver le name='...' et le mettre en alt le cas echant 467 if ($name = extraire_attribut($img, 'name')) { 468 $attributs .= ' name="'.entites_html($name).'"'; 469 $attributs_alt = ' alt="'.entites_html($name).'"'; 470 } 471 // si un alt (meme vide) etait present, le recuperer 472 if (($alt = extraire_attribut($img, 'alt')) !== NULL) 473 $attributs_alt = ' alt="'.entites_html($alt).'"'; 474 475 $attributs .= $attributs_alt; 476 477 // attributs deprecies. Transformer en CSS 478 if ($espace = extraire_attribut($img, 'hspace')) 479 $attributs .= " style='margin: $espace" . "px; border-width: 0px;'"; 480 else 481 $attributs .= " style='border-width: 0px;' class='spip_logos'"; 482 // attribut deprecie mais equivalent CSS pas clair 483 if ($align = extraire_attribut($img, 'align')) 484 $attributs .= " align='$align'"; 485 486 if (eregi("(.*)\.(jpg|gif|png)$", $logo, $regs)) { 487 if ($i = cherche_image_nommee($regs[1], array($regs[2]))) { 488 list(,$nom,$format) = $i; 489 if ($taille_origine = @getimagesize($logo)) { 490 list ($destWidth,$destHeight, $ratio) = image_ratio( 491 $taille_origine[0], $taille_origine[1], $taille, $taille_y); 492 493 // Creer effectivement la vignette reduite 494 $suffixe = '-'.$destWidth.'x'.$destHeight; 495 $preview = creer_vignette($logo, $taille, $taille_y, 496 $format, ('cache'.$suffixe), $nom.$suffixe); 497 if ($preview) { 498 $logo = $preview['fichier']; 499 $destWidth = $preview['width']; 500 $destHeight = $preview['height']; 501 } 502 503 if (!_DIR_RESTREINT) 504 $date = '?date='.filemtime($logo); 505 return "<img src='$logo$date' width='$destWidth' height='$destHeight'$attributs />"; 506 } 507 } 508 } 509 } 510 511 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Thu Feb 22 22:27:47 2007 | par Balluche grâce à PHPXref 0.7 |