Bit of a weird issue.
I'm generating some HTML reports and then, due to Outlook 2010's limitations of understanding CSS/HTML/Images, I'm sending the whole damn report out as an image.
The generation works fine. Previewing the generated HTML in a browser looks lovely at a range of resolutions. However, WKHTMLTOPDF (or rather, wkhtmltoimage) is giving me some odd behaviour with it's --width flag.
I have the following PHP code:
# Converts a .html file to a .jpg file - requires wkhtmltopdf installed:
# http://wkhtmltopdf.org/downloads.html
function html_to_jpg($fileIn, $fileOut) {
echo "\nRendering HTML to image\n";
$command = 'wkhtmltoimage --width 1130 ' . $fileIn . ' ' . $fileOut;
$remove = 'rm -f ' . filter_var($fileOut, FILTER_SANITIZE_STRING);
try {
exec($remove);
exec($command);
return true;
} catch (Exception $e) {
return false;
}
}
This works. It generates an image of "1130" width as specified. However, the report looks a little dodgy at that width, so I increase the --width...
$command = 'wkhtmltoimage --width 1140 ' . $fileIn . ' ' . $fileOut;
Now this still generates an image, however, the image is now 36480 pixels wide, with the content centered, and a stack of black space on the right.
Through experimentation I've confirmed that widths < 1140 do not generate this problem, but every width above does. I've confirmed this on Ubuntu 15.10 Wily, and CentOS 6.7, both installed from different RPMs.
Has anyone else experienced this? Am I doing something stupid (other than using exec)?
I have experienced the same behavior, and I fixed it by changing the width of the html element to match the width that I pass in the command and it works fine
so basically in your $fileIn add this in a style tag
html, body {
width:1140px !important;
}
of course you can ommit the !important if nothing else affects the width of these elements in your file.
Related
I'm converting PDF files to jpg. PDFs are pretty complicated and consists of lot of images. When I convert pdf, some layers respectively parts of pages are missing. For example:
Here is screenshot from desktop PDF reader application
http://mariusrak.sk/original.png
And here is same part of result of conversion:
http://mariusrak.sk/result.png
As you can see, background and that thing in grey field are missing.
I've tried merging layers and flattenimages, also ($i = $i->flattenimages()) and result is still the same, some parts of images/layers are missing.
//Edit:
So here's the code:
ini_set('max_execution_time', 600);
$f = './f_big.pdf';
$I = new Imagick;
$I->setresolution(144, 144);
$p = 0;
while(true){
try {
$I->readimage($f.'[' . $p . ']');
$attrs = $I->identifyimage();
$dpi = 1600 / ($attrs['geometry']['height'] / $attrs['resolution']['y']);
$I->setresolution($dpi, $dpi);
$I->readimage($f.'[' . $p . ']');
} catch (Exception $E) {
break;
}
$I->writeimage('./big-'.$p.'.jpg');
$I->clear();
++$p;
}
The issue is happening with large files e.g. 8Mb and 46 pages. But when I use same files but only with first two pagesEverything works just fine.
Files are created with InDesign and missing layers are PSD files inside of InDesign project with many layers.
// Edit:
So I found out the problem is with file themselves. Files that are stored in PDF version 1.5 or higher are encoded/stored different than older version. If I save same file to version PDF 1.4 everything works just fine. So it's not matter of size but of version.
I think, the best solution would be to convert file on server. Can anybody recomnd some library or solution to do that? Can be paid.
Suppose a user uploads a .txt or .php file, and I want to generate a .png thumbnail for it. Is there a simple way of doing it, that doesn't require me to open the file and write its contents into a new .png? I have ImageMagick and FFmpeg available, there must be a way to take advantage of that, but I've been looking a lot and no luck yet.
Thanks in advance.
You can use ffmpeg:
ffmpeg -video_size 640x480 -chars_per_frame 60000 -i in.txt -frames:v 1 out.png
However, it has some caveats:
By default it renders 6000 characters per frame, so it may not draw all of your text. You can change this with the -chars_per_frame and/or -framerate input options. Default frame rate is 25.
The text will not be automatically word wrapped so you will have to add line breaks for the text to fit your output video size.
You could always use php's imagettftext function.
It would give you a representation of what is in the text file.
http://php.net/manual/en/function.imagettftext.php
You can use PHP's class imagick to convert the file to image.
It will work for a txt file nicely.
try
{
$im = new imagick("inputfile.txt[0]");
if ($im)
{
$im->setImageAlphaChannel(imagick::ALPHACHANNEL_DEACTIVATE);
$im->setImageFormat('jpg');
$im->setImageCompressionQuality(85);
file_put_contents("outfile.jpg",$im->getImageBlob());
}
} catch(Exception $e)
{
echo "cannot process";
}
// When imagick is unable to read the file, it may wrongly
// set internal server error 500 status code.
// I do not understand why that happens, because the script
// continues normally. Anyway, lets overwrite it here for all cases.
header("HTTP/1.1 200 OK");
$image = new Imagick();
$image->readImage("text:" . $filename . "[0]");
$image->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 1);
$image->setImageFormat('png');
$image->writeImage($linkImage);
I wrote a quick PHP script for loading an image in a directory, but I've been finding that it regularly gives me "Image truncated or corrupt" errors in the Error Console, despite the images not being corrupt. I'm able to view the image normally if I browse to its full path, and I'm also able to download it and view it without issue. I've tried using different browsers but have the same problem.
Oddly enough, some images work fine and some do not. The issue doesn't seem to be related to file size.
My code is pretty simple, and I've been using it for ages:
if (isset($_GET['i']) && is_numeric($_GET['i'])) {
$path = 'D:/Images/';
if (is_file($path . $i . '.jpg')) {
header('content-type: image/jpg');
require($path . $i . '.jpg');
}
You shouldn't be using require, that includes the image as if it were PHP code. You should be using readfile(), which will just open the image and send it straight to the browser:
readfile($path . $i . '.jpg');
exit;
Also note that you'll want to call exit() after readfile().
here I have to cache about 2000 favicon.ico files for performance enhancements. I grab the files and try to shrink them via IMagick (v.6.6.0) and PHP 5.3.5
The PHP code for this is
try {
$image = new Imagick($im_hint . ':' . BASE . '/upload/favicon.ico');
$image->cropThumbnailImage(16, 16);
$image->setImageFormat('ico');
$image->writeImage(BASE . '/favicons/' . $id[0] . '/' . $id[1] . '/' . $id[2] . '/' . $id . '.ico');
} catch (Exception $e) { die($e->getMessage()); }
where $im_hint could be ico, png, jpg and so on.
For 99% of the files all is fine and I get a working ICO file. But for one percent of files, I get only a blank ICO file and I don't know why? An example for an ICO file where this code fails is http://www.augensound.de/favicon.ico
I tried to comment out the cropThumbnailImage call and try to use setFormat instead of setImageFormat and tried to save it as PNG...but nothing works. There is also no exception.
Regards
Not an answer to your question, but I get a blank image too when I open the example file in IrfanView or PhotoImpact. It's not a multi-page/multi-resolution file so there is nothing to switch. The canvas just is blank.
Windows 7's built-in preview renders it fine, though.
It could be that IM can't deal with these files because they have the wrong format or sub-format.
Im trying to create vector graphics in PHP. Ive tried Cairo and I havn't been able to get it to work. I understand that imageMagick has vector functionality but the documentation on php.net is very poor can some one lead me in the right direction? The ideas is to be able to save the graphic to EPS. I also need to be able to use different fonts to output text.
Although you're looking to create eps I would still aim to create a PDF. PDF's are fully editable in any major package: Adobe Illustrator, Corel Draw, Xara Pro etc
TCPDF works well and there is a bunch of code samples including fonts and support for vector images eps and ai output to PDF
eps/ai example http://www.tcpdf.org/examples/example_032.pdf
All the examples and php code http://www.tcpdf.org/examples.php
I know what this is quite old question, but I had some problem few weeks ago and solve it for myself, hope this answer helps someone.
Cairo library have PHP bindings, but it also have few bugs which break convertation between formats - forget about it. We need something native here on start. Look at SVG format - open your vector image in editor (I use Inkscape) and save it as SVG file. After that you can change it via php just like xml file.
Adding custom fonts in SVG:
$text_path = 'm 100,200'
$font_name = 'Some_font.ttf';
$font_size = '20px';
$font = base64_encode('font_file_content');
$text = 'Bla bla bla';
$font_svg = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<path d="' . $text_path . '" id="font_id_123"/>
<style type="text/css">
<![CDATA[
#font-face {
font-family: ' . $font_name . ';
src: url("data:font/ttf;charset=utf-8;base64,' . $font . '");
]]>
</style>
</defs>
<text style="font-family: ' . $font_name . '; font-size: ' . $font_size . ';">
<textPath xlink:href="#font_id_123">' . $text . '</textPath>
</text>
</svg>';
$content = file_get_contents($svg_file); // $svg_file - your vector image
$content = substr($content, 0, -6); // cut last '</svg>' tag from file
$newContent = $content . $font_svg . '</svg>'; // add font to the end
file_put_contents($svg_file, $newContent); // save changes
Ok, we have SVG with needed fonts, but we need EPS. For converting SVG to EPS I used Inkscape with simple bash script svg2eps.sh:
#!/bin/bash
inkscape -f $1 -z -T -E $2
You can call it from php:
exec('/path/to/svg2eps.sh /path/to/in.svg path/to/out.eps');
Other tips:
1)Install latest version of Inkscape. I tested it on openSuse 12.3 - works great.
2)Install all custom fonts to system fonts.
Try these links:
http://www.imagemagick.org/script/magick-vector-graphics.php
and
http://www.imagemagick.org/discourse-server/viewtopic.php?f=10&t=10144
I can't tell you how to create vector images in PHP but perhaps you would like a bit different approach- create raster images in PHP and convert them to vectors? It works okay for black & white images not sure about color ones.
<?php
$im = imagecreatetruecolor(500,500);
//draw something on $im
imagepng($im, 'image.png');
$url = 'http://server.com/image.png'; //change to your server's domain
$data = json_decode(file_get_contents('http://api.rest7.com/v1/raster_to_vector.php?url=' . $url . '&format=svg'));
if (#$data->success !== 1)
{
die('Failed');
}
$vec = file_get_contents($data->file);
file_put_contents('vectors.svg', $vec);