I am trying to draw Devanagari text on image using PHP ImageMagick.
ImageMagick renders text correctly if I use font available in the system, but not when loading a font from a file.
For exmaple if use
$imagick->setFont('Lohit Marathi');// installed font
works correctly but if I use
$imagick->setFont("fonts/lohit_mr.ttf");//load from file
Font does not loads and it renders using default font.
code I am trying is as follows
<?php
$imagick = new Imagick();
$imagick->setFont('ANY_SYSTEM_FONT') // works;
$imagick->setFont('somefont.ttf') // does not work;
?>
Can someone point out anything I am missing?
I'm using PHP Intervention Image Library 2.x to put some text on an image. Everything works fine except for the font colour.
Font size, font, font angle, and the position change according to the given values, the only issue is it not takes the colour I'm setting in the callback function.
Image::configure(array('driver' => 'imagick'));
$img = Image::make(IMG . 'gold-full.jpg');
$font = new Intervention\Image\Imagick\Font();
$img->text('Y. C. Perera', 1061, 1298.4, function($font) {
$font->file('./img/ERASBD.TTF');
$font->size(88);
//$font->color('#fff');
//$font->color('white');
//$font->color(array(255,255,255));
$font->color('#ffffff');
});
$img->save('./uploads/new-gold-full.jpg');
$img->destroy();
As I mentioned with the comment I tried all those types of settings for the color. but no luck.
I am trying to convert a web page into jpeg image file. i had used following codes.
<?php //put your html code here
$html_code = "
<html>
<head>
<title>My test title</title>
<style>
body {
font-family:verdana;
font-size:11px;
color:black
}
</style>
</head>
<body>
this is the body
</body>
</html>";
// Create the image
$img = imagecreate("300", "600");
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,300,600,$c);
imageline($img,300,0,0,600,$c);
$white = imagecolorallocate($img, 255, 255, 255);
imagettftext($img, 9, 0, 1, 1, $white, "VERDANA.TTF", $html_code);
// Display the image
header("Content-type: image/jpeg");
imagejpeg($img);
?>
Question: Is there any library to convert html page into image ?
You will need an HTML renderer for this. There exists a few such renderers, but most of them requires an X server on your web server, so check out Xvfb to run a framebuffer device without a screen.
GD does not do this natively, you can render a picture from your screen though but that is as far as it goes. I had painty in my links with a description of html to jpg php conversion.
You need to internally render the page. You can find possible solutions here.
When I needed to 'screen-capture' an HTML table as an image a few months back, I couldn't find anything suitable.
I eventually settled for writing my own code using PHP's GD library with GIF output.
It was for a very specialised purpose and I'm sure it wouldn't suit an application outputting jpegs.
Is it possible to print html div tag with style on image in PHP?. if not, then what is the alternative way?
Some hosts have ImageMagick for PHP. To add text to your image, take a look at the syntax of the commands here. The example given on that page should help some - it's pretty easy to get text on an image.
The benefits of using ImageMagick over a fixed image is that you can vary the content of the text, which is what you might want (you didn't mention needing a static text; for this, I'd use an image with a transparent background). For more comprehensive font commands, take a look here.
To put a transparent image on top of your base image, take a look at this very nicely designed site.
I'll also give the code presented on that site here:
$photo = imagecreatefromjpeg("original.jpg");
$watermark = imagecreatefrompng("watermark.png");
// This is the key. Without ImageAlphaBlending on, the PNG won't render correctly.
imagealphablending($photo, true);
// Copy the watermark onto the master, $offset px from the bottom right corner.
$offset = 10;
imagecopy($photo, $watermark, imagesx($photo) - imagesx($watermark) - $offset, imagesy($photo) - imagesy($watermark) - $offset, 0, 0, imagesx($watermark), imagesy($watermark));
// Output to the browser
header("Content-Type: image/jpeg");
imagejpeg($photo);
To output the image to a file, please Google that and replace the last two lines of the example given above.
For ImageMagick stuff, take a look here
I hope this helps :-)
James
You can set the image as the background graphic of any div using CSS. Then the text within that div will appear on top of the image.
(CSS)
.mydiv {
background:url(/path/to/image.gif);
width:100px; /* set to width of the image */
height:100px; /* set to height of the image */
}
(HTML)
<div class='mydiv'>Some text here</div>
There is no easy way to print text on images using html/css on server side, because php can't parse html, so you'd better find another solution like php GD.
Why is this code not working ?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<?php
header('Content-type: image/png');
$myImage = imagecreate(200, 100);
$myGray = imagecolorallocate($myImage, 204, 204, 204);
$myBlack = imagecolorallocate($myImage, 0, 0, 0);
imageline($myImage, 15, 35, 120, 60, $myBlack);
imagepng($myImage);
imagedestroy($myImage);
?>
</body>
</html>
I always get error The image cannot be displayed because it contains errors.. I've already enabled php_gd2.dll and memory_limit in php.ini is also 128M. If i remove header('Content-type: image/png'); i don't get the error but i don't see the image either. All i see is this :-
‰PNG ��� IHDR���È���d���ùHíH���PLTEÌÌÌ���Ó33d���MIDATH‰c£Àx�§” Nf*k²Ã)Ãø�§”•5}À)ÅS†ÚšpJUà”a§²¦œ2ÔŽw<špJ‚Q0 †;�� uTBúŸ����IEND®B‚ `
You must not output anything before header(). Just start your document with <?php (as the first file characters), followed by the code for displaying the image. Skip the HTML tags. Do not even write a single blankline before header().
If you want to display an image inside the html document of yours, you must do it in two files. One, call it for example image.php, containing only the PHP code including the header. The second file, call it show.php or show.html, includes the HTML code you like, including <img src="image.php" alt="Your generated image" />.
If you base64 encode the output, you could use the image directly with a Data URI scheme.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<?php
$myImage = imagecreate(200, 100);
$myGray = imagecolorallocate($myImage, 204, 204, 204);
$myBlack = imagecolorallocate($myImage, 0, 0, 0);
imageline($myImage, 15, 35, 120, 60, $myBlack);
ob_start();
imagepng($myImage);
printf('<img src="data:image/png;base64,%s"/>',
base64_encode(ob_get_clean()));
imagedestroy($myImage);
?>
</body>
</html>
Note that Data URIs are not supported by all browsers (guess which).
You should output only the image. You are outputting a bunch of tags. Specifically
<!DOCTYPE HTML PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN">
<title></title>
</head>
<body>
and then the image as a binary. If you want to see this, wget the page from your server and try opening it in an editor. Your code should start at the <?php.
Removing the header gets rid of the notification to the client that this is an image so it will try out print it out as text.
While trying to output a jpg of a Facebook profile-picture from their Graph API with PHP,
I noticed that if the PHP file is saved with UTF-8 encoding - this error was returned,
but if the file was saved with ANSI encoding -
then it worked OK.
If you have this problem, try to delete any space character between the begin of the script and the php tag
> <?php
I spent several hours before realize this. And now it works OK. This happens because any character on the file alter the png format. This worked great for me.
Even if you think you have removed all the text from before header() your PHP file may contain a Byte Order Marker
This will be invisible to you in your editor, but your browser will see it and think the image is corrupt. You need to take the steps appropriate to your editor to remove any BOM.
This is all you need. You can not print anything else because it needs to look like it's own file. You could call this image.php and pass it a variable to define which image to output.
<?php
header('Content-type: image/png');
$myImage = imagecreate(200, 100);
$myGray = imagecolorallocate($myImage, 204, 204, 204);
$myBlack = imagecolorallocate($myImage, 0, 0, 0);
imageline($myImage, 15, 35, 120, 60, $myBlack);
imagepng($myImage);
imagedestroy($myImage);
?>
I isolated the PHP script into its own file (image.php) and it worked fine: I got a grey rectangle with a black slanted line. Your issue is trying to embed this within a HTML file.
You need the PHP in its own, separate file (as I did, call it image.php or something more description to your needs) and then create a HTML file as follows:
<html>
<head>
<title>Document Title</title>
</head>
<body>
<img src="image.php" alt="" />
</body>
</html>
You should get your desired output then.
I had the same problem and the solution was to change the charset of the code from UTF-8 to ansi or viceversa. If the server is set to UTF-8 and your code is in ansi this don't work and if your code is in ansi and the server is configured to UTF-8 neither.
If you are using Filezilla to upload your webpages make sure you select transfer type as 'Auto/Binary'.
Transfer -> Transfer type -> Auto/ Binary.