Page 1 sur 1

fonction test image

Posté : 25 sept. 2009, 12:59
par omelhor
Bonjour,

Débutant en php, je cherche a faire un fonction trop compliwza pour moi !!!
Pouvez-vous m'aider svp ?

En gros voila ce que je cherche a faire
Le but est de supprimé toute image dont la largeur est supérieur à 600px

En gros cela donne ça :

1)
J'ai un texte dans lequel je cherche la présence d'une image avec la balise "<img" :

2)
J'extrait la valeur du width à l'aide de la fonction php getimagesize()
Si width supérieur à 600 je supprime l'image
exemple de code à supprimer
<img title=\"rrrrrrrrrrrrrrrrrr\" alt=\"rrrrrrrrr\" src=\"http://www.testimage.net/images/stories ... timage.jpg\" /><br />

Exemple de texte à traiter :

<<<<<<<<<
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr aaaaaaaaaaaaaa fggggg5555555555
<img title=\"rrrrrrrrrrrrrrrrrr\" alt=\"rrrrrrrrr\" src=\"http://www.testimage.net/images/stories ... timage.jpg\" /><br />
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr aaaaaaaaaaaaaa fggggg5555555555
aaaaaaaaaaaaaa fggggg5555555555
<img title=\"rrrrrrrrrrrrrrrrrr\" alt=\"rrrrrrrrr\" src=\"http://www.testimage.net/images/stories ... image2.jpg\" /><br />
aaaaaaaaaaaaaa fggggg5555555555

>>>>>>>>>>>




Merci,
José

Re: fonction test image

Posté : 25 sept. 2009, 18:24
par Aureusms
Tes images sont sur ton serveur ou pas ?

Re: fonction test image

Posté : 25 sept. 2009, 19:31
par omelhor
Ba non,
En fait je recupere un flux rss , et je l'affiche.
Exemple de texte :
<<<
Exemple de texte à traiter :

Code : Tout sélectionner

'Mandanda jusqu\'en 2014 !','http://www.om-infos.net/news/divers/250-mandanda-jusquen-2014-',20090916192314,'<div><img title=\"5ème journée de Ligue 1 : Le Mans 1 - 2 Marseille\" alt=\"5ème journée de Ligue 1 : Le Mans 1 - 2 Marseille\" src=\"http://www.om-infos.net/images/stories/news/mandanda-prolonge_1.jpg\" /><br />Le <strong>gardien marseillais</strong> <strong>Steve Mandanda</strong> a ajouté <strong>deux années</strong> à son contrat le liant avec l\'OM.</div> <p><a href=\"http://www.om-infos.net/news/divers/250-mandanda-jusquen-2014-\">Lire la suite...</a></p>','OM-INFOS.NET','')ZXYK;'Marseille 1 - 2 Milan AC','http://www.om-infos.net/resultats/europe/249-marseille-1-2-milan-ac',20090915225039,'<div><img title=\"1ère journée de Ligue des Champions : Marseille 1 - 2 Milan AC\" alt=\"1ère journée de Ligue des Champions : Marseille 1 - 2 Milan AC\" src=\"http://www.om-infos.net/images/stories/resultats/ldc-0910-j01_1.jpg\" /><br /><strong>L\'OM</strong> s\'est fait cueillir à froid par un <strong>Milan AC</strong> très réaliste. Les <strong>Marseillais </strong>méritaient pourtant mieux ...</div>','OM-INFOS.NET','')ZXYK; 'Le Mans 1 - 2 Marseille','http://www.om-infos.net/resultats/france/248-le-mans-1-2-marseille',20090912211136,'<div><img src=\"http://www.om-infos.net/images/stories/resultats/ligue1-0910-j05_2.jpg\" alt=\"5ème journée de Ligue 1 : Le Mans 1 - 2 Marseille\" title=\"5ème journée de Ligue 1 : Le Mans 1 - 2 Marseille\" /><br /><strong>Marseille</strong> a su faire le nécessaire pour obtenir une <strong>victoire précieuse</strong> face à une très bonne équipe du <strong>Mans</strong>.</div> <p><a href=\"http://www.om-infos.net/resultats/france/248-le-mans-1-2-marseille\">Lire la suite...</a></p>','OM-INFOS.NET','')ZXYK;Le Mans - Marseille : l\'Avant-Match','http://www.om-infos.net/news/divers/247-le-mans-marseille--lavant-match',20090912183757,'<div><img alt=\"5ème journée de Ligue 1 : Le Mans - Marseille\" src=\"http://www.om-infos.net/images/stories/news/ligue1-0910-j05_1.jpg\" /><br />L\'<strong>OM</strong> affronte <strong>Le Mans </strong>avant de débuter la <strong>Ligue des Champions</strong> mardi.</div> <p><a href=\"http://www.om-infos.net/news/divers/247-le-mans-marseille--lavant-match\">Lire la suite...</a></p>','OM-INFOS.NET','') >>>
Merci
José

Re: fonction test image

Posté : 27 sept. 2009, 22:13
par blof
Bonsoir,
<?php

$txt = "'Mandanda jusqu\'en 2014 !','http://www.om-infos.net/news/divers/250-mandanda-jusquen-2014-',20090916192314,'<div><img title=\"5ème journée de Ligue 1 : Le Mans 1 - 2 Marseille\" alt=\"5ème journée de Ligue 1 : Le Mans 1 - 2 Marseille\" src=\"http://www.om-infos.net/images/stories/news/mandanda-prolonge_1.jpg\" />
Le <strong>gardien marseillais</strong> <strong>Steve Mandanda</strong> a ajouté <strong>deux années</strong> à son contrat le liant avec l\'OM.</div>
<p><a href=\"http://www.om-infos.net/news/divers/250-mandanda-jusquen-2014-\">Lire la suite...</a></p>','OM-INFOS.NET','')ZXYK;'Marseille 1 - 2 Milan AC','http://www.om-infos.net/resultats/europe/249-marseille-1-2-milan-ac',20090915225039,'<div><img title=\"1ère journée de Ligue des Champions : Marseille 1 - 2 Milan AC\" alt=\"1ère journée de Ligue des Champions : Marseille 1 - 2 Milan AC\" src=\"http://www.om-infos.net/images/stories/resultats/ldc-0910-j01_1.jpg\" />
<strong>L\'OM</strong> s\'est fait cueillir à froid par un <strong>Milan AC</strong> très réaliste. Les <strong>Marseillais </strong>méritaient pourtant mieux ...</div>','OM-INFOS.NET','')ZXYK;
'Le Mans 1 - 2 Marseille','http://www.om-infos.net/resultats/france/248-le-mans-1-2-marseille',20090912211136,'<div><img src=\"http://www.om-infos.net/images/stories/resultats/ligue1-0910-j05_2.jpg\" alt=\"5ème journée de Ligue 1 : Le Mans 1 - 2 Marseille\" title=\"5ème journée de Ligue 1 : Le Mans 1 - 2 Marseille\" />
<strong>Marseille</strong> a su faire le nécessaire pour obtenir une <strong>victoire précieuse</strong> face à une très bonne équipe du <strong>Mans</strong>.</div>
<p><a href=\"http://www.om-infos.net/resultats/france/248-le-mans-1-2-marseille\">Lire la suite...</a></p>','OM-INFOS.NET','')ZXYK;Le Mans - Marseille : l\'Avant-Match','http://www.om-infos.net/news/divers/247-le-mans-marseille--lavant-match',20090912183757,'<div><img alt=\"5ème journée de Ligue 1 : Le Mans - Marseille\" src=\"http://www.om-infos.net/images/stories/news/ligue1-0910-j05_1.jpg\" />
L\'<strong>OM</strong> affronte <strong>Le Mans </strong>avant de débuter la <strong>Ligue des Champions</strong> mardi.</div>
<p><a href=\"http://www.om-infos.net/news/divers/247-le-mans-marseille--lavant-match\">Lire la suite...</a></p>','OM-INFOS.NET','')
>>>";

preg_match_all('/<img .*src=\\"(.+)\\".*>/isU', $txt, $v);
// le tableau $v[0] contient toutes les balises <img> complètes.
// le tableau $v[1] contient toutes les URL des images.
// http://lumadis.be/regex/tuto_pcre.php

foreach($v[1] as $i => $img)
{
	$taille = getimagesize($img);
	// $taille[0] correspond à la largeur de l'image.

	if( $taille[0] > 600 )
	{
		$troplarge[] = $v[0][$i];
		// le tableau $troplarge contient toutes les balises <img> dont l'image a une largeur > à 600px
	}
}

$txt = str_replace($troplarge, '', $txt);
// on supprime les balises <img> qu'on ne souhaite pas.

echo $txt;

?>
( dans l'exemple, toutes les images ont une largeur supérieure à 600 px )

Re: fonction test image

Posté : 29 sept. 2009, 11:29
par Aureusms
Bon allons au plus simple :

Il faut d'abord que tu récupères toutes les balises <img /> avec un REGEX et preg_split ().
regarde vers http://www.siteduzero.com/tutoriel-3-14 ... regex.html (je ne suis pas expert en REGEX)

<?php
$tab = preg_split("#^<img(*)/>$#",$texte);
?>