4

I'd like to ask whether its possible to take a bit of CSS on a page with a PHP extension, and add the PHP header to define the page as a PNG, and have a dynamic image? From what I've heard this isn't possible, but if it isn't, is there a way easier than the conventional method of creating a PHP image?

Edit* To elaborate, I want to do something like draw a bordered 200x200p box in CSS with styling and HTML, and have this HTML/CSS box on a .PHP page, and get this page to become a PNG image with the .PNG extension using the PHP header tag.

Thank You!

Karan

unicornication
  • 635
  • 2
  • 7
  • 26
  • 1
    Your question is a bit unclear of what you want to do. You can dynamically generate CSS with PHP as well as you can dynamically serve a PNG image with PHP. – hakre Dec 26 '11 at 19:50
  • 1
    Apologies, I will elaborate :) – unicornication Dec 26 '11 at 19:51
  • The most common use for dynamic PHP-created images are captchas. There are a lot of howtos and tutorials out there, just google a little bit. – Corubba Dec 26 '11 at 19:55
  • 1
    You want to create a screenshot of the website? – hakre Dec 26 '11 at 19:55
  • Done! Apologies, I'm on mobile and its a pain, haha. Thank you :) – unicornication Dec 26 '11 at 19:55
  • 1
    If i understand you right, this might be helpful for you. http://stackoverflow.com/questions/757675/website-screenshots-using-php – Corubba Dec 26 '11 at 19:57
  • ok so you want to convert the rendered CSS into an image; I think it'll be way easier to just draw the image directly. – Samus_ Dec 26 '11 at 19:57
  • Basically yes, a section of a screenshot of a website :) – unicornication Dec 26 '11 at 19:57
  • Thanks for the link, @bloodyworld! @Samus_ I suppose so, but I hate the positioning involved with the PHP image drawing, and was wondering if it was possible with CSS. Thanks for your responses :) – unicornication Dec 26 '11 at 20:05
  • I'm not so sure there is anything that interfaces PHP which can render CSS3 correctly and return an image. Why do you need this functionality for? Most CSS3 compatible browsers allow the user to right click on a canvas and save it as a png. – MrGlass Dec 26 '11 at 20:05
  • @MrGlass basically I've got a little testing site with a display picture, name, and post count, I'm trying to create externally available user banners as a test, which if they work, ill roll them over to a load of sites I currently have :) Cheers :) – unicornication Dec 26 '11 at 20:08

2 Answers2

3

You can make use of wkhtmltopdf which can convert html to pdf using webkit (qtwebkit) which supports CSS properly. There even exists a PHP binding for it.

You can then convert the PDF file to a PNG (e.g. with ImageMagick) and serve it from within PHP.

Related: Website screenshots using PHP

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
2

If I understand your question properly, I am not sure how this would be possible using only PHP but you could use ImageMagick on server side to convert the HTML page to a png.

egerardus
  • 11,316
  • 12
  • 80
  • 123