Programmatically create image from web-page or a single DIV - php

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.

Related

'Add image' option to image gallery?

I have am planning to use Colorbox the jquery lightbox plugin. I am just wondering as to what the best method would be to implement a 'add/upload image' option so it would be easy for a user to upload new images without having to go into the HTML markup and add an individual <img> line of code for each new image.
I am only currently knowledgeable with HTML and CSS at this current time. So if no one already has a snippet they could possibly provide me with, which language would be 'simpler' to learn for just for this specific task. Would it be PHP or Js or something different?
OR am I going about this completely wrong?
Of course they have to upload images to database. You need a suitable back end design. That means you need php to interact with database(or python will also do). If you need dummy upload procedures then you can easily do it with appendTo() method using jquery.
As you are using a plugin, then you will have a method to add an image to the webpage. It will be something like $("user-uploaded-image").plugin-name();
Where you want to upload the image?
Uploading an image means transferring the image to another location (server). For that you need a server side script that
Receives the image.
Saves the image to server hard disk.
php is a server side scripting language.
JS is client side language that is used to sending requests (in this case image ) to server.
Just google it for getting sample image upload scripts.
From taking a look around google for upload scripts suggested by #kiren Siva I came across quite a few CMS type plugins that give me the ability to do exactly what I need.
Some examples can be found here.
Thank you all for your help and suggestions.

Create a custom print ready image

I am looking to create a small app that allows a user to create a custom cover for a book. The client doesn't want to just be supplied with what the customer selected but a print ready 300dpi JPEG or PDF.
I know PHP can handle most of this. But the customisation seems to be a bit difficult to get right. I have tried using canvas to create the client side preview, which work ok enough.
Just to see if I'm wasting time trying to recreate the wheel, does anyone know if something like what I need exists already?
Basically it would be something kind of similiar to this but for creating print ready images.
Thanks!
If you're using Canvas for building the editor, for the best results, I'd recommend you re-render the customization on the server using whatever technology fits your need in PHP and not try to take the image they're looking at and make it work somehow by sending it to the server. The DPI will be wrong if you use Canvas in a standard way (it will be at the screen DPI).
For example, if they have the ability to place an image, then just note the coordinates and place the image in a web server created image at 300 DPI. Text, same thing, etc. Yes, it will be extra work, but it should be of higher quality and better consistency.
This would imply that there's a simple serialization format for the representation of the custom cover that is sent to the web server.
You might be able to use SVG, but you'll have to introduce a reliable SVG to PDF conversion and handle fonts. Maybe something like this? (I'm not sure that building an SVG editor would be easier than Canvas based).
Starting from your "work ok enough preview", jsPDF will be able to generate a PDF right from the browser in a breez.
convert the canvas to a image/jpeg (toDataURL will be your best friend)
instanciate a jsPDF object, add the image
output the jsPDF object
Tricky details:
beware your screen displays stuff with 72dpi but print expect 300dpi. So you can up scale your canvas then draw the image within the pdf PDF
beware to Cross Origin Resources: if you are using images outside your domain, you are doomed (or you will need a proxy)

Export div content to image

Currently I'm working on a project in which user of website can design a giftwall by drag and drop of gifts. Drag, drop and sorting works perfect and I'm able to store generated giftwall into database. On recipient side system lists all the gift images in the same sequence sender sent so it visually looks like a giftwall. I want to allow users to store this giftwall into a single image so they can store their giftwalls into image album. In current system it lists all individual gifts into a individual div resided in main wrapper div. How can I export this wrapper div to image so it looks same as HTML. Any help will really be appreciated. Waiting for reply.
Thanks!
I don't know if there is a way by using pure javascript, but you can generate the image on your server and send it back.
I have never heard of saving browser's viewport to an image file via JS. I think it's only possible vie creating SVG or using HTML5 canvas.
I asked exactly the same thing a while back.
HTML div to screenshot?
The conclusion is... it is not possible with JS. Questionably possible from PHP also.
Use can use an online service for screen capture, like browsershots, but its not in real-time, and doesn't render everything well.
I used a workaround for my situation.
Recreated all the DOM/HTML elements which create the image (load parameters form database, and generate the DOM). Wrap everything in one container div, and zoom it out to the size you need.
I know, not the prettiest solution, but the only solution I could get to work.
Using <canvas> you can create image that will looks similar to how it looks on the page. Try it this way:
Get the size of wrapper <div> and create the same canvas element. Then get the position of each image in the div wrapper and draw that image on canvas at the same position with the same size.
Things may be harder if you need to draw additional controls, like button or textbox.
All modern desktop browsers suport <canvas> now.
You can either use html2canvas, which isn't perfect yet. Or store browser offsets of elements in div and then with GD or ImageMagick combine them into an image
Maybe this could be of use: http://html2canvas.hertzen.com/

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.

Problem in picture overlapping when I convert HTML page to PDF

I want to overlap pictures, but it is not working and I need some help.
Here's the link to the page I'd like to convert:
http://9m9.com/innovative/sample/two.html
I want to convert this page to a PDF. You can see the small image overlapping the bigger one.
This is the page where you can click on a link that will convert the page to PDF.
http://citysoftsolutions.com/eclients/virtualtour/view_property_images.php?pid=9&uid=67
As you can see the image is placed behind the big image.
I'm using this converter script: http://mpdf.bpm1.com/
When I printed it using PrimoPDF driver it came out just fine. Last image was easily laid over. So there must be a bug with the script you're using.
What do I suggest?
If you'd like to convert your pages to PDFs "on the fly" I suggest you either
contact script creator and inform them of a bug in the script
use a different script (I'd check out this question that can help you)
If you'd like to just provide PDFs of your page I suggest you install a PDF printer driver (like PrimoPDF that I'm using) and print those pages yourself and use those.
I'm not working for Nitro PDF Software company nor am I related to them in any way. So this is not me advertising their products/services.
On a sidenote
Something's telling me that what you'd actually like to do is to create a PDF flyer/promo material or something. If that's actually what you're after I suggest you do that using some software that's meant for such a job. Microsoft Office Word will do, but you'll better off using some other. If it's a one page leaflet you could use Adobe Illustrator or CorelDraw. But if it's going to be an actual multipage document use something like Word or Adobe InDesign.
Word is probably something you can easily master. So go with that one.

Categories