Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 891713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:55:57+00:00 2026-05-15T13:55:57+00:00

What I want is a function I can run on user input that will

  • 0

What I want is a function I can run on user input that will intelligently find and add the width and height attributes to any <img> tag in a blob of HTML so as to avoid page-reflowing issues while images load.

I am writing the posting script for a PHP forum, where a user’s input is sanitised and generally made nicer before writing it to the database for later display. As an example of what I do to make things nicer, I have a script that inserts alt attributes into images like so:

Here are two images: <img src="http://example.com/image.png"> <img src="http://example.com/image2.png">

which, upon sanitising by the posting script, becomes

Here are two images: <img src="http://example.com/image.png" alt="Posted image"> <img src="http://example.com/image2.png" alt="Posted image">

(This makes it validate under HTML 4 strict, but maybe isn’t in the spirit of the alt attribute—alas!)

So, for my function, I have a vague idea that the server will need to run getimagesize() on each external image it finds in the block of HTML, then apply the attributes that function generates to each and every <img> tag it runs into. I assume that this function has been written before, but I have had no luck on Google or php.net docs. Do I have to start from scratch, or is somebody aware of a (relatively) robust function that I can use or adapt to do this job?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:55:58+00:00Added an answer on May 15, 2026 at 1:55 pm

    You’re right about getimagesize(). You can simply do something like this:

    $img = 'image2.png';
    $info = getimagesize($img);
    printf('<img src="%s" %s>', $img, $info[3]);
    

    If the image is hosted at a remote location, you’ll have to download all the images though (the function takes care of it), so you might want to cache the result to speed things up on subsequent requests.

    Edit: Just saw that you have a string containing various <img> elements. This should do the trick:

    <?php
    $html = <<<EOF
    something <img src="https://www.google.com/images/logos/ssl_logo_lg.gif"> hello <img src="https://mail.google.com/mail/images/2/5/logo1.png">
    EOF;
    
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    
    foreach ($dom->getElementsByTagName('img') as $img) {
        list($width, $height) = getimagesize($img->getAttribute('src'));
        $img->setAttribute('width', $width);
        $img->setAttribute('height', $height);
    }
    
    $xpath = new DOMXpath($dom);
    $newDom = new DOMDocument();
    foreach ($xpath->query('//body/p')->item(0)->childNodes as $node) {
        $newDom->appendChild($newDom->importNode($node, true));
    }
    
    $newHtml = $newDom->saveHTML();
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 499k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer you simply place the socket path in the <host/> bit… May 16, 2026 at 12:28 pm
  • Editorial Team
    Editorial Team added an answer Assuming, that both lists are of equal length and that… May 16, 2026 at 12:28 pm
  • Editorial Team
    Editorial Team added an answer Google has been experimenting with this feature in the developer… May 16, 2026 at 12:28 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I want to use jQuery to submit my form and i can do that
I want to delete the records which i select, and run.html will refresh, how
I am developing an HTA that will run as a kiosk in our reception
I am designing a regular expression tester in HTML and JavaScript. The user will
Firstly, I was wondering if there was some kind of built in function that
I am brand new to Jquery and have run into something that if answered
Is it possible to call a method that is an attribute? I mean, I
I have been able to create php function to determine a list of zip
Hi guys I have a function which accepts this as a parameter - '
I'm trying to make Vim run the command 'CommandTFlush' whenever a new file is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.