Parse HTML to PHP img

DrYontem

Well-known member
Hi all,
I wanna parse html to a php file for using like that
HTML:
<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

HTML:
<table>
<tr>
<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>
</tr>
<tr>
<td style="text-align:right">SECOND</td>
<td style="text-align:left">LINE</td>
</tr>
<tr>
<td style="text-align:right">THIRD</td>
<td style="text-align:left">LINE</td>
</tr>
 
</table>

any ideas?
 
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.
 
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.
as i said, i tried lot of methods but i couldnt show this line
HTML:
<td rowspan="3"><img src="IMGLINK" alt="" style="align:left" /></td>
 
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?
 
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?
i just edited that line
there is a spesific image file in html
i want to use that html file like php image file
PHP:
<?php
$text = 'html codes';
header("Content-Type: image/png");
$im = @imagecreate(700, 300)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 12, 5, 5,  $text, $text_color);
imagepng($im);
imagedestroy($im);
?>

an example for about php file
but there is NO html parsing system
so, result is attached =(
 

Attachments

  • demo.webp
    demo.webp
    4.2 KB · Views: 14
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 ;)
 
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 ;)
yet....

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 :)
 
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.
 
Top Bottom