fork download
  1. <?php
  2.  
  3. $img = '<img src="http://w...content-available-to-author-only...t.no/bilde.jpg" width="2000" height="1400" />';
  4.  
  5. $doc = new DOMDocument;
  6. $doc->loadHTML($img);
  7.  
  8. $node = $doc->getElementsByTagName("img")->item(0);
  9. $height = intval($node->getAttribute('height'));
  10. $width = intval($node->getAttribute('width'));
  11.  
  12. if ($width > 1000) {
  13. $oldwidth = $width;
  14. $width = 1000;
  15. $ratio = $width / $oldwidth;
  16. $height = $height * $ratio;
  17. } elseif ($width < 500) {
  18. $oldwidth = $width;
  19. $width = 500;
  20. $ratio = $width / $oldwidth;
  21. $height = $height * $ratio;
  22. }
  23.  
  24. $node->setAttribute('height', intval($height));
  25. $node->setAttribute('width', intval($width));
  26.  
  27. $img = $doc->saveHTML($node);
  28.  
  29. echo $img;
  30.  
  31. ?>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
<img src="http://w...content-available-to-author-only...t.no/bilde.jpg" width="1000" height="700">