Subversion Repositories Sites.tela-botanica.org

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7 david 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
				$ok = @ImageCopyResampled($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
348
			if (!$ok)
349
				$ok = ImageCopyResized($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
350
 
351
			// Sauvegarde de l'image destination
352
			$vignette = "$destination.$destFormat";
353
			$format = $destFormat;
354
			if ($destFormat == "jpg")
355
				ImageJPEG($destImage, $vignette, 70);
356
			else if ($destFormat == "gif")
357
				ImageGIF($destImage, $vignette);
358
			else if ($destFormat == "png")
359
				ImagePNG($destImage, $vignette);
360
 
361
			ImageDestroy($srcImage);
362
			ImageDestroy($destImage);
363
		}
364
	}
365
 
366
	$size = @getimagesize($vignette);
367
	$retour['width'] = $largeur = $size[0];
368
	$retour['height'] = $hauteur = $size[1];
369
	$retour['fichier'] = $vignette;
370
	$retour['format'] = $format;
371
	$retour['date'] = @filemtime($vignette);
372
 
373
 
374
	// renvoyer l'image
375
	return $retour;
376
}
377
 
378
 
379
 
380
//
381
// Retourner taille d'une image
382
// pour les filtres |largeur et |hauteur
383
//
384
function taille_image($img) {
385
 
386
	if (eregi("width *= *['\"]?( *[0-9]+ *)", $img, $regs))
387
		$srcWidth = intval(trim($regs[1]));
388
	if (eregi("height *= *['\"]?( *[0-9]+ *)", $img, $regs))
389
		$srcHeight = intval(trim($regs[1]));
390
 
391
	// recuperer le nom du fichier
392
	if (eregi("src='([^']+)'", $img, $regs)) $logo = $regs[1];
393
	if (!$logo) $logo = $img;
394
 
395
	if (!$srcWidth
396
	AND $srcsize = @getimagesize($logo))
397
		$srcWidth = $srcsize[0];
398
 
399
	if (!$srcHeight
400
	AND $srcsize = @getimagesize($logo))
401
		$srcHeight = $srcsize[1];
402
 
403
	return array($srcHeight, $srcWidth);
404
 
405
}
406
 
407
 
408
 
409
 
410
//
411
// Reduire la taille d'un logo
412
// [(#LOGO_ARTICLE||reduire_image{100,60})]
413
//
414
 
415
// Cette fonction accepte en entree un nom de fichier ou un tag <img ...>
416
 
417
function reduire_image_logo($img, $taille = -1, $taille_y = -1) {
418
 
419
	// Determiner la taille x,y maxi
420
	if ($taille == -1) {
421
		$taille = lire_meta('taille_preview');
422
		if (!$taille)
423
			$taille = 150;
424
	}
425
	if ($taille_y == -1)
426
		$taille_y = $taille;
427
 
428
	if ($taille == 0 AND $taille_y > 0)
429
		$taille = 100000; # {0,300} -> c'est 300 qui compte
430
	else
431
	if ($taille > 0 AND $taille_y == 0)
432
		$taille_y = 100000; # {300,0} -> c'est 300 qui compte
433
	else if ($taille == 0 AND $taille_y == 0)
434
		return '';
435
 
436
	// recuperer le nom du fichier
437
	if ($src = extraire_attribut($img, 'src'))
438
		$logo = $src;
439
	else
440
		$logo = $img;
441
	if (!$logo) return '';
442
 
443
	// Si c'est une image distante, la recuperer (si possible)
444
	if (!$local = copie_locale($logo)) {
445
		spip_log("pas de version locale de $logo");
446
		return $img;
447
	}
448
	$logo = $local;
449
 
450
 
451
	$attributs = '';
452
 
453
	// preserver le name='...' et le mettre en alt le cas echant
454
	if ($name = extraire_attribut($img, 'name')) {
455
		$attributs .= ' name="'.entites_html($name).'"';
456
		$attributs_alt = ' alt="'.entites_html($name).'"';
457
	}
458
	if ($alt = extraire_attribut($img, 'alt'))
459
		$attributs_alt = ' alt="'.entites_html($alt).'"';
460
 
461
	$attributs .= $attributs_alt;
462
 
463
	// attributs deprecies. Transformer en CSS
464
	if ($espace = extraire_attribut($img, 'hspace'))
465
		$attributs .= " style='margin: $espace" . "px; border-width: 0px;'";
466
	else
467
		$attributs .=  " style='border-width: 0px;' class='spip_logos'";
468
	// attribut deprecie mais equivalent CSS pas clair
469
	if ($align = extraire_attribut($img, 'align'))
470
		$attributs .= " align='$align'";
471
 
472
	if (eregi("(.*)\.(jpg|gif|png)$", $logo, $regs)) {
473
		if ($i = cherche_image_nommee($regs[1], array($regs[2]))) {
474
			list(,$nom,$format) = $i;
475
			if ($taille_origine = @getimagesize($logo)) {
476
				list ($destWidth,$destHeight, $ratio) = image_ratio(
477
					$taille_origine[0], $taille_origine[1], $taille, $taille_y);
478
 
479
				// Creer effectivement la vignette reduite
480
				$suffixe = '-'.$destWidth.'x'.$destHeight;
481
				$preview = creer_vignette($logo, $taille, $taille_y,
482
					$format, ('cache'.$suffixe), $nom.$suffixe);
483
				if ($preview) {
484
					$logo = $preview['fichier'];
485
					$destWidth = $preview['width'];
486
					$destHeight = $preview['height'];
487
				}
488
 
489
				if (!_DIR_RESTREINT)
490
					$date = '?date='.filemtime($logo);
491
				return "<img src='$logo$date' width='$destWidth' height='$destHeight'$attributs />";
492
			}
493
		}
494
	}
495
}
496
 
497
?>