A peine testé mais plus générique.

Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 
<?php
class toto {
 private function image($htmlPage,$tagImage,$srcImage,$delimiter) {
  foreach($srcImage as $urlimage) {
   if ($urlimage != '') {
    //---- Recherche de la taille de l'image
    list($longueur,$hauteur)  = getimagesize($urlimage);
    $nouveau  = "$delimiter width=$delimiter$longueur$delimiter height=$delimiter$hauteur";
    $image2   = str_replace($urlimage,$urlimage.$nouveau,$tagImage);
    $htmlPage = str_replace($tagImage,$image2,$htmlPage);
   }
  }
  return $htmlPage;
 } 
 
 public function changeImg($htmlPage) {
  //----  Recherche des balises img
  preg_match_all('/<img[^>]+>/i',$htmlPage, $tagsImage); 
  //----  parcours du résultat
  foreach($tagsImage[0] as $keyImage => $tagImage) {
   //----  On ne prend pas les images qui ont une largeur et une hauteur
   $attrib = preg_match('/((width|height)=(?:(?:"([^"]+)")|(?:\'([^\']+)\')))/i',$tagImage);
   if ($attrib==0) {
    //---- Recherche de l'url de l'image
    preg_match_all('/src=(?:(?:"([^"]+)")|(?:\'([^\']+)\'))/i',$tagImage, $srcImage);
    //---- On traite les images délimitées par un "
    $htmlPage = $this->image($htmlPage,$tagImage,$srcImage[1],'"');
    //---- On traite les images délimitées par un '   
    $htmlPage = $this->image($htmlPage,$tagImage,$srcImage[2],'\'');  
   }
  }
  return $htmlPage;
 }
}
$html = new toto();
file_put_contents('test9585_exemple_apres.html',$html->changeImg(file_get_contents('test9585_exemple.html')));


Fichier avant :
Code html : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
<html>
<head>
</head>
<body>
Exemple
<img alt="toto" title='image1' src='image1.jpg' />
<br/>
<img title='image2' src='image2.jpg' />
<br/>
<img title="image3" src='image3.jpg' />
<br/>
<img src="image4.jpg" title='image4' />
<br/>
<img src="image2.jpg" title='image4' width='100'/>
<br/>
</body>
</html>

Fichier après:
Code html : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
<html>
<head>
</head>
<body>
Exemple
<img alt="toto" title='image1' src='image1.jpg' width='600' height='799' />
<br/>
<img title='image2' src='image2.jpg' width='50' height='50' />
<br/>
<img title="image3" src='image3.jpg' width='350' height='350' />
<br/>
<img src="image4.jpg" width="150" height="150" title='image4' />
<br/>
<img src="image2.jpg" title='image4' width='100'/>
<br/>
</body>
</html>