PNG from CSS using PHP? - php

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

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

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.

Related

code on a jpg image generated dynamically

While I was looking for some info, I came across this image on a forum which i didn't understood how it worked. Its a JPG image which actually tells you your ip address and your browser. I think that maybe it was made by modifying htacess and replacing PHP for JPG, and with PHP generating the image dynamically but I'm not sure about it.
Would this be the way that this works?
Here is the image i saw:
http://www.danasoft.com/sig/475388.jpg
Thanks
It is nothing but etching text on image. There are several options to do it. Using PHP see here and Java see here
yes this image is generating dynamically. It collects your info make a string and use some library to generate that image like in Captcha. php has its own library too for this purpose
Click here

Programmatically create image from web-page or a single DIV

Is there any way to programmatically create (client or server side (PHP)) a image from a specific DIV or a complete (web) page? I'm currently creating a web-site for free coupons and the idea is when the end-user clicks on the "Print" button, the app opens a new tab/window with all the selected coupons as a single image (JPG, PNG or etc..) in A4 format ready for printing. Each coupons has it's own data (Article name, price, description etc..) so I need it to be done programmatically over a coupon-template I designed.
I do not ask you to write code for me, just to suggest a solution I could use/develop. If not already exist, I will upload/publish it for free :)
Update: I did it with the PHP GD library :) Still not satisfied with the idea to use Images instead of PDF, because each printing results with different Coupon sizes (images) on different PC's. That's why PDF may would be a better solution. You can see/test it on demo.svikuponi.ba - Just select a few Coupons and click the PRINTAJ button above.
You cannot create image from div for sure but yes you can create dynamic images in php using its gd library.
Following links will help:
http://php.net/manual/en/function.imagecreate.php
http://phptutorial.info/learn/create_images/
Here is a great way for you to create images on the client side: http://smus.com/screen-capture-for-chrome-os
You can take this and create a web app that will work nicely on webkit (for other browsers - I'll look at JS polyfills).
Did anyone mention html2canvas and/or jsfeedback ?
It create a page screenshot completely in javascript, then you can send to the server via ajax..
Obviously, CSS support lack some things.
In php, there is many image related functions like imagettftext() in GD library
for details, check this out http://php.net/manual/en/book.image.php
if GD is not enough, you can try imagick as well
for the template, you can try creating a true color handle in php from your file(image) and add the text part or something else with all kinds of effects and bar codes etc.
but in your case, i would suggest dynamic PDF creation since it would better with formatting instead of plain image, the pdf lib :
http://www.fpdf.org/
you could easily have a background image of your token/voucher and overlay the text using some php variables.
i believe it is possible to create a unique bar-code with php imaging too.
It is possible to get a screenshot from a webpage, but this is quite a hassle. You need to start a webbrowser to render the page and get a screenshot from that.
You are probably better of by parsing some specification and feeding it to a couple of GD or Imagick functions. This is less versatile, but easier to manage.

Is there a way to output html div to an image using php?

I have a div on my page which I want to output to a jpeg file. Is this possible using PHP?
I know there's the GD library for creating images in PHP but I'm not sure if it can do what I want. Because I also want to put a barcode on the image (I used a jQuery plugin to do this on the html page) .
Is this possible (if it's possible can you give me an idea how to implement it) or is there a better approach to this? Thanks!
As far as I know the functionality you want is not supported by GD. You might want to search for a library that does that. The problem is that the browser renders the HTML and CSS, but GD would need to know how to parse and render HTML and CSS (and Javascript) in order to turn HTML into an image. It might be a better idea, depending on what you really want to do, to create an image in GD and stick it in the HTML DIV, and then you won't have to do any conversion.
If there is a way to export a section of HTML to an image in a browser, then you could use PHP to control a server-side browser and export it that way. It would be a lot more work, but it's definitely possible.
It can't be done the way you want it. Instead you can draw the image based on the data you have.
You say it's bar code... so I guess this would be a good start.

html, css, execute javascript from a gif file

I would like to know if it's possible to execute javascript from embedded .gif file . I can make rewrite to a php file to seem as a gif file and execute the code from there but I'm not faimilar with css/html therefore I was wondering if there is any trick to make seem there is embedded a picture but to execute javascript . I tried img src attribute but is not working as the browser seems to consider it just a simple picture...
GIF compression is lossy and is not suitable for code. However, the javascript hacker Jacob Seidelin implemented something very similar using pngs: http://blog.nihilogic.dk/2008/05/compression-using-canvas-and-png.html
Not directly answering your question, but you can embed JavaScript into SVG. Although not most browsers support SVG, but most SVG viewers support JavaScript.

How to save webpage as a image file using PHP?

I want to save webpage as image file or i want to take snapshot of an webpage using PHP. Just like firefox extension Fireshot doing.
Check out http://browsershots.org/ and Webkit2Png
You'll need to render the page with something. If you really want to only use php, i suggest you HTMLTOPS, wich render the page and output it in a ps file (ghostscript), then, convert it in a .jpg, .png, .pdf.. can be little slower with complex pages (and dont support all the CSS).
Else, you can use wkhtmltopdf to output a html page in pdf, jpg, whaterver.. Accept CSS2.0, use the webkit (safari's wrapper) to render the page.. so should be fine. You have to install it on your server, as well..
Source: Website screenshots using PHP
Like others have said, it would be difficult to do in PHP alone. However, there is an easy-to-use cross-platform tool called CutyCapt that does exactly what you need. The best part about this tool is that it's stand-alone and doesn't depend on anything else.
You cannot do this with pure PHP, try forking a browser like Mozilla with headless mode and let it handles taking snapshot of the page.

Categories