1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Parse HTML to PHP img

Discussion in 'General PHP and MySQL Discussions' started by Tilkißey, Mar 10, 2013.

  1. Tilkißey

    Tilkißey Well-Known Member

    Hi all,
    I wanna parse html to a php file for using like that
    <img src="myfile.php" alt=""/>
    i tried a lot of methods about that but i have a big problem (or i think its big)
    my html file includes html img tag
    there is my html file's example

    <td rowspan="3"><img src="./cover.jpg" alt="" style="align:left" /></td>
    <td style="text-align:right">FIRST</td>
    <td style="text-align:left">LINE</td>
    <td style="text-align:right">SECOND</td>
    <td style="text-align:left">LINE</td>
    <td style="text-align:right">THIRD</td>
    <td style="text-align:left">LINE</td>
    any ideas?
  2. Chris D

    Chris D XenForo Developer Staff Member

    As long as myfile.php either echos out a valid image URL or returns the raw bits of an image, there shouldn't be any problems.
  3. Tilkißey

    Tilkißey Well-Known Member

    as i said, i tried lot of methods but i couldnt show this line
    <td rowspan="3"><img src="IMGLINK" alt="" style="align:left" /></td>
  4. Chris D

    Chris D XenForo Developer Staff Member

    What is IMGLINK? Is it "myfile.php"?

    Do you get any errors? If you View Source what is in place of IMGLINK?

    What are the contents of myfile.php?
  5. Tilkißey

    Tilkißey Well-Known Member

    i just edited that line
    there is a spesific image file in html
    i want to use that html file like php image file
    'html codes';
    header("Content-Type: image/png");
    $im = @imagecreate(700300)
    or die(
    "Cannot Initialize new GD image stream");
    $background_color imagecolorallocate($im255255255);
    $text_color imagecolorallocate($im000);
    imagestring($im1255,  $text$text_color);
    an example for about php file
    but there is NO html parsing system
    so, result is attached =(

    Attached Files:

  6. Luke F

    Luke F Well-Known Member

    You can't render html to an image like that, you would need a rendering engine. I recommend wkhtmltoimage, which implements webkit and has a solid php api. Just be sure to implement caching ;)
  7. Tilkißey

    Tilkißey Well-Known Member

    can u give me an example please?
  8. Adam Howard

    Adam Howard Well-Known Member


    php has had this in their backlog for a while. No ETA as on when it will or if ever it will be included.

    Although I wouldn't hold your breath on it.... Its been in the backlog since the original php 6.0 backlogs, which have been deferred several times now (years).

    I hold out hope though that one day we'll be able to embed more into php without a 3rd party engine :)
  9. feldon30

    feldon30 Well-Known Member

    There are a couple of solutions if you are hell-bent on going HTML -> Image. Imagemagick and Ghostscript come to mind. However in my opinion, converting HTML to an Image is going to be temperamental and prone to lots of configuration issues. You'll need to make sure all your ducks are in a row with several moving parts to get it working reliably.

    Have you considered writing an Imagemagick script? It is basically like a command line version of Photoshop. You can add text of all different fonts, colors, sizes, including letterspacing, kerning, styling, etc. with pixel accuracy. You can upload Truetype or OpenType fonts so you always know your text will display properly. You can build up layers of colors, shapes, gradients, and images. You can overlay transparent PNGs all with opacity settings, tints, ink adjustments (Lighten, Darken, Screen, etc. like Photoshop). Basically anything you can do in Photoshop, you can do in Imagemagick and get the result as a transparent 32-bit PNG, a JPEG, or other format.

    There are two ways to use Imagemagick:
    • PHP offers a lightweight (but limited) Imagick module which lets you do some Imagemagick functions without leaving PHP.
    • PHP can access the much more powerful Imagemagick through the commandline, but your server must allow exec().
    Note: ImageMagick is not a CPU-light task. A complex IM script can take up to 1/2 to 1 second to execute, which is a very long time for a web server to be occupied. If you are going to get hundreds or thousands of pageviews of a "generated" image, you really should build image caching into your script so that you are only generating a new image once every, say, 5 minutes or 1 hour.
    Tilkißey likes this.

Share This Page