Is there a api to create a picture in php? I want to create a chart in gif or jpg format.
Thanks.
There are several.
Yes, there are several described in the manual.
Here's an example from the manual describing how to use imagejpeg to create a JPEG:
<?php
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
// Set the content type header - in this case image/jpeg
header('Content-type: image/jpeg');
// Skip the filename parameter using NULL, then set the quality to 75%
imagejpeg($im, NULL, 75);
// Free up memory
imagedestroy($im);
?>
If you just want to create charts, maybe the Google Chart's Api or something similar , while not strictly php, maybe useful for you. Note that The second link doesn't create images, it creates flash charts, but they can be saved as an image.
Related
I have a website I am building that takes data (the person's name) from a form and posts it into a customized "note" in a table on the next page. I want the user to then be able to save their "note" as an image with their customized name on it. Is there a way to save a SPECIFIC HTML TABLE as a PNG? Maybe take a screenshot of a specific coordinate on a web page?
If not is there a way to render posted PHP content from the form in an HTML canvas?
Any help would be great. I'm in a little over my head right now.
While you can render HTML using a variety of tools, you can't be sure that those tools will render the HTML the same way your browser renders it.
If your end goal is to generate an image with text on it, then let's solve that problem instead of trying to make the solution you suggested work.
Have a look at PHP's imagettftext() function. It contains an Example #1 which creates a small, simple image from text that can be stored in any variable ... including a form variable.
By using this example, and adding a few other of PHP's GD functions, you can make a decent replica of a table, and make sure it looks exactly the way you want it, rather than the way html2ps or some other tool renders it.
<?php
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = isset($_POST['name']) ? $_POST['name'] : "name";
// Replace path by your own font path
$font = 'arial.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
Here's sample output generated by the above script:
Note that you'll need to provide arial.ttf per the instructions at the link above.
If things don't work, look for errors both on-screen and in your web server's error log FIRST, before following up here. It's possible that PHP's GD module is not installed on your web server. If this is the case, you should check with your server administrator to make sure what you need is available to you.
You can try this JS library on client side.
workable solution
use fpdf to create pdf from html (table)
use imagemagick to convert pdf to png
You can also use Xvfb. It's a linux package. It stands for "X-window Virtual Frame Buffer" (if you were wondering why on earth it had such an esoteric name).
What that will do, is load a page, execute any JavaScript and apply the different stylings from CSS, all in the frame-buffer of the server. Then you can save the image.
Xvfb will probably not be available on most shared-hosting services, however, if that doesn't matter, then it would be a viable solution.
You can use something like html2ps
i want to create an image from html, i couldn't use painty since it is outdated and no longer working and i want something similar.
i have already tried to create image using GD library like this
<?php
$html_code = "<b> this is the body </b> ";
// Create the image
$img = imagecreate("300", "600");
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,300,600,$c2);
imageline($img,300,0,0,600,$c2);
$white = imagecolorallocate($img, 255, 255, 255);
imagettftext($img, 9, 0, 1, 1, $white, "arial.ttf", $html_code);
// Display the image
header("Content-type: image/jpeg");
imagejpeg($img);
but it doesn't "compile" the html tags and prints them in the image as text.
Best Regards
the GD lib does not have the ablilty to render html-code and draw it on the image. if you want to draw bold text you need to use bold font, e.g. arialbd.ttf
Short answer is that you can't render HTML without a browser.
A longer answer is that you can render HTML in a browser but you can't easily automate capture of a screenshot (there are activeX widgets to do this - but activeX is such a bad idea). A better approach is to use a HTML client which can render to a suitable format - e.g. xhtml2pdf, there are also online services available for this
I'm trying to create a produkt configurator similar to: http://winmap.active-online.de/kler/pol/index.php3?room_name=200D
I'd like to put texture or color on diffrent image elements and put it together into one result image.
I'm trying to usa like this:
header('Content-Type: image/jpeg');
$poszycie = imagecreatefrompng('poszycie.png');
$tekstura = imagecreatefrompng('tekstura.png');
//imagefilter($poszycie,IMG_FILTER_EMBOSS);
//imagepng($tekstura);
//$bcg = imagecreatefromjpeg('las.jpg');
//$img = imagecreatefromjpeg('zdjecie.jpg');
//imagecopymerge($poszycie, $tekstura, 0, 0, 0, 0, imagesx($poszycie), imagesy($poszycie), 75);
//imagepng($poszycie, null, 100);
imagecopymerge($poszycie, $tekstura, 0, 0, 0, 0, imagesx($poszycie), imagesy($poszycie), 50);
imagepng($poszycie);
but result is :http://saveur.pl/testgd/configurator.png
Thanks for any help.
I think you'll be better off with ImageMagick. For example you could use these tricks http://www.imagemagick.org/discourse-server/viewtopic.php?f=2&t=14513&start=15 and I'm sure a web search for imagemagick texture image area would reveal more ideas.
Anyway you'll have to take apart the image and use the alpha channel.
I think my answer to this question can help. The problem deals with alpha channel. Besides, you should send this header (png instead of jpeg):
header('Content-Type: image/png');
since you are calling
imagepng($poszycie);
which may be another cause of trouble.
Using PHP: How can I create an image to display an email address (to help reduce spam)?
Meaning, if I want to display "joe#example.com" on my web page, since crawlers can easily find that text - I want to display the email address as an image that says "joe#example.com".
How do I do that? I want the font to be:
color: #20c
font: 11px normal tahoma
A simple search that you could easily do....
However: (color, font and string not the ones you specified)
header("Content-type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
Relevant function definitions:
php.net/imagecreate
php.net/imagestring
Use these:
header, to tell the browser to expect an image instead of HTML (PHP's default). The image function doc pages have more information about this.
imagettfbbox, to find out the required size for the image
imagecreatetruecolor, to create the image resource
imagecolorallocate, to allocate a color for the text
imagettftext, to draw the text (read the example, it's almost all you need)
imagepng, to output the image to the browser
You should use gd image processing library.
If you are only doing this for one address you should not be doing this dynamically everytime the page loads for performance reasons. If this is the case you can find amny email obfuscator online such as this one:
http://digitalcolony.com/lab/maskemail/maskemail.aspx
Is there a PHP function that would allow me to superimpose an image over another one? If not, how can I accomplish this (not asking for code, just a list of steps)?
I suppose there are functions provided by GD (generally enabled on PHP installations) that might do just that.
For instance, maybe one of imagecopy or imagecopymerge functions, I'd say.
See Example #1 Merging two copies of the PHP.net logo with 75% transparency on the manual page of the second one (quoting) :
<?php
// Create image instances
$dest = imagecreatefromgif('php.gif');
$src = imagecreatefromgif('php.gif');
// Copy and merge
imagecopymerge($dest, $src, 10, 10, 0, 0, 100, 47, 75);
// Output and free from memory
header('Content-Type: image/gif');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src);
?>
There are also these two examples that might prove useful :
Adding watermarks to images using alpha channels
and Using imagecopymerge() to create a translucent watermark
Use the GD graphics library. There is an example of creating a watermark, which is basically the same thing using the imagecopymerge() function.