Creating PDF thumbnail in PHP and caching it - php

I'm using a generic PHP based CMS, i wanted to create a script which read the pdf created a thumbnail and cached it. There were lots of different answers, and i did have a fair few problems with different versions of imagick, but this is script which worked for me.
some people might find it useful and maybe someone could advice me if it is optimised?
<?php
$loc = *the file location*;
$pdf = *the file name*;
$format = "jpg";
$dest = "$loc$pdf.$format";
if (file_exists($dest))
{
$im = new imagick();
$im->readImage($dest);
header( "Content-Type: image/jpg" );
echo $im;
exit;
}
else
{
$im = new imagick($loc.$pdf.'[0]');
$im->setImageFormat($format);
$width = $im->getImageheight();
$im->cropImage($width, $width, 0, 0);
$im->scaleImage(110, 167, true);
$im->writeImage($dest);
header( "Content-Type: image/jpg" );
echo $im;
exit;
}
?>

Leverage PHP and ImageMagick to create PDF thumbnails
http://stormwarestudios.com/articles/leverage-php-imagemagick-create-pdf-thumbnails/
In this article, we discuss using PHP and ImageMagick to generate thumbnails from a given PDF, storing them in a temporary (or “cache”) directory, and serving them up to the web.
One of our more recent clients made a request to display PDF thumbnails published through the Joomla CMS that we’d deployed for them.
The requirement was fairly simple, but the execution was a little more involved. After installing ImageMagick, ImageMagick PHP bindings (which incidentally aren’t working, and a workaround was devised), and sleuthing some code, the following solution was determined:
<?php
function thumbPdf($pdf, $width)
{
try
{
$tmp = 'tmp';
$format = "png";
$source = $pdf.'[0]';
$dest = "$tmp/$pdf.$format";
if (!file_exists($dest))
{
$exec = "convert -scale $width $source $dest";
exec($exec);
}
$im = new Imagick($dest);
header("Content-Type:".$im->getFormat());
echo $im;
}
catch(Exception $e)
{
echo $e->getMessage();
}
}
$file = $_GET['pdf'];
$size = $_GET['size'];
if ($file && $size)
{
thumbPdf($file, $size);
}
?>
The above code assumes that you’ve provided appropriate permissions to the temporary directory (usually chmod 755 or chmod 777, depending on your level of courage), that you’ve saved the above code snippet in a file called thumbPdf.php, and placed this somewhere visible on your web server.
After obtaining parameters from GET, the code checks the destination temporary directory, and if the desired image is not present, it uses ImageMagick’s convert program to generate the PDF thumbnail, sized down to the appropriate proportion, and saves the image in the temporary directory. Finally, it reloads the thumbnail into an ImageMagick PHP object, and outputs the content to the browser.
Invoking the above code is done fairly easily; simply call the PHP script from inside an image tag, like so:
<img src="/path/to/thumbPdf.php?pdf=your.pdf&size=200" />
The above code would generate a thumbnail from the first page of “your.pdf”, sized 200 pixels wide by an appropriately-proportioned height.
Good luck, and happy webmastering!

I know it's been discussed here:
Should I use a PHP extension for ImageMagick or just use PHP's Exec() function to run the terminal commands?
And to quote drew101:
You would benefit a lot using the PHP extensions instead of using exec
or similar functions. Built in extensions will be faster and use less
memory as you will not have to spawn new processes and read the output
back. The image objects will be directly available in PHP instead of
having to read file output, which should make the images easier to
work with.
If you have a busy site, creating lots of processes to edit images may
start to slow things down and consume additional memory.

If you have not installed the Imagick php library for some reason you may use the ghost script and generate thumbnail of an pdf using the below example :
exec('gs -dSAFER -dBATCH -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r300 -sOutputFile=xyz.jpg xyz.pdf');

Related

Generate thumbnail for text file

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);

PHP & ImageMagick - image code into image?

I want to be able to generate image thumbnails without saving them to the server. So far, I've come up with this code, but I'm not sure what to do with $code.
$code = system("convert galleries/13_0.jpg -resize 400x270 /dev/stdout");
How would I go about plugging $code into the PHP/HTML to get the raw image code to display as a jpg?
I would advise you not to do this from the system as you have described.
PHP has libraries for doing this sort of thing.
http://www.php.net/manual/en/ref.image.php.
http://php.net/manual/en/book.imagick.php.
And there are libraries the wrap these native functions for manipulating images. https://imagine.readthedocs.org/en/latest/
That way all your code is in PHP and you are not relying on the system to do anything (providing PHP has been compiled with the libraries as described. They are standard libraries available in most PHP builds and you can enable them if they are not included).
Edit: I'm advocating that you.
Open the source image.
Convert the source image
Return the source image
with imagine you would do it thus:
$imagine = new Imagine\Gd\Imagine();
$size = new Imagine\Image\Box(400, 270);
$imagine->open('/path/to/large_image.jpg')
->resize($size)
->show('jpg');
If the $code is the raw data, could you have another file getimage.php which would write the data out but set the header content type to image/JPEG? So your img src would be getimage.php?image=13_0.jpg for example?
I would save it to memory, then read it, then delete it. /dev/shm is a ramdrive on most Linux systems.
$tmp = '/dev/shm/'.uniqid('',true).'.jpg';
system("convert galleries/13_0.jpg -resize 400x270 $tmp");
header("Content-Type: image/jpeg");
header('Content-Length: '.filesize($tmp));
readfile($tmp);
unlink($tmp);

Thumbnail image of PDF using ImageMagick and PHP

After searching Google and SO, I found this little bit of code for creating thumbnails of PDF documents using ImageMagick.
The trouble for me is in implementing it into my WordPress theme. I think that I'm getting stuck on the path to cache that the script needs for temporary files.
I'm using it as described in the article:
<img src="http://localhost/multi/wp-content/themes/WPalchemy-theme/thumbPdf.php?pdf=http://localhost/multi/wp-content/uploads/2012/03/sample.pdf&size=200 />
which must be right (maybe... but I assume i am correct to use full URL to the actual file), because when I click on that URL I am taken to a page that reads the following error:
Unable to read the file: tmp/http://localhost/multi/wp-content/uploads/2012/03/sample.pdf.png
Now tmp is defined in the thumbPdf.php script, but I am confused as to what it's value should be. Is it a url or a path? Like timthumb.php, can i make it be relative to the thumbPdf.php script? (I tried ./cache which is the setting in timthumb -and was sure to have a /cache folder in my theme root, to no avail). also, fyi I put a /tmp folder in my root and still get the same error.
So how do I configure tmp to make this work?
http://stormwarestudios.com/articles/leverage-php-imagemagick-create-pdf-thumbnails/
function thumbPdf($pdf, $width)
{
try
{
$tmp = 'tmp';
$format = "png";
$source = $pdf.'[0]';
$dest = "$tmp/$pdf.$format";
if (!file_exists($dest))
{
$exec = "convert -scale $width $source $dest";
exec($exec);
}
$im = new Imagick($dest);
header("Content-Type:".$im->getFormat());
echo $im;
}
catch(Exception $e)
{
echo $e->getMessage();
}
}
$file = $_GET['pdf'];
$size = $_GET['size'];
if ($file && $size)
{
thumbPdf($file, $size);
}
I have seen this answer:
How do I convert a PDF document to a preview image in PHP?
and am about to go try it next
The error tells everything you need.
Unable to read the file: tmp/http://localhost/multi/wp-content/uploads/2012/03/sample.pdf.png
Script currently tries to read file from servers tmp/ folder.
$tmp = 'tmp';
$format = "png";
$source = $pdf.'[0]';
//$dest = "$tmp/$pdf.$format";
$dest = "$pdf.$format";
Remember securitywise this doesn't really look so good, someone could exploit ImageMagic bug to achieve very nasty things by giving your script malformed external source pdf. You should at least check if the image is from allowed source like request originates from the same host.
Best way to work with ImageMagic is to always save the generated image and only generate a new image if generated image doesn't exist. Some ImageMagic operations are quite heavy on large files so you don't want to burden your server.

Need Help Understanding this PHP Code

i am aware of the basics like what is a function, a class, a method etc. however i am totally confused on what exactly the below code does to read the image, i read it somewhere that we have to read the image in binary format first. i am confused on the process how the php reads the image and loads it for reading. i would like to know the function of each and every code in this class and what is actually happening with the code.
Code :
class Image {
function __construct($filename) {
//read the image file to a binary buffer
$fp = fopen($filename, 'rb') or die("Image $filename doesn't exist");
$buf = '';
while(!feof($fp))
$buf .= fgets($fp, 4096);
//create image
imagecreatefromstring($buf);
}
}
and when i instantiate the object image with the syntax $image = new Image("pic.jpg"); it does not print the image in html, what does the variable $image actually hold, if i want to print that image in html what should i be doing.
Update :
FYI : I understand PHP and HTML, as i was trying to learn OOP in PHP and i came across the article as this particular code was not understood clearly by me so i thought of asking you guys, i highly appreciate your response, i would be thankful if you could try and explain the code instead of asking me to try different code.
My concern is purely meant for learning purpose, i am not implementing it anywhere.
In HTML, all you need to do is refer to the file in an <img> tag.
<img src="/path/to/image/image.jpg" width="600" height="400" alt="Image Name" />
The source needs to be the URL of the image, relative to your webserver root directory.
As for the code, you put up. That would be completely unnecessary for HTML usage, and is also unnecessary for standard image use within PHP, as there are direct methods to load an image from a file, imagecreatefromjpeg() for instance for JPEG files.
As it stands, the constructor of your Image class takes a filename, opens that file and reads the entire contents as binary data in to the string $buf, 4096 bytes at a time. Then it calls imagecreatefromstring($buf) to convert the file data in to an image resource, which can then be used further within PHP with the PHP GD image handling functions.
As I say, none of this is particularly relevant if all you wish to do is display an existing image within HTML. Those commands are designed for image manipulation, inspection and creation.
Your $image would contain an instance of the Image Class.
Your constructor will try to open $filename. If that's not possible, the script will die/terminate with an error message. If $filename can be opened, the file will be read into the $buf variable and a GD image resource will be created.
The code is suboptimal for a number of reasons:
the GD resource created by imagecreatefromstring is not assigned to a property of the Image class. This means, the entire process is pointless, because the resource will be lost after it was created. The work done in the constructor is lost.
calling die will terminate the script. There is no way to get around this. It would be better to throw an Exception to let the developer decide whether s/he wants the script to terminate or catch and handle this situation.
reading a file with fopen and fread works, but file_get_contents is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.
You should not do work in the constructor. It is harmful to testability.
A better approach would be to use
class Image
{
protected $_gdResource;
public function loadFromFile($fileName)
{
$this->_gdResource = imagecreatefromstring(
file_get_contents($fileName)
);
if(FALSE === $this->_gdResource) {
throw new InvalidArgumentException(
'Could not create GD Resource from given filename. ' .
'Make sure filename is readable and contains an image type ' .
'supported by GD'
);
}
}
// other methods working with $_gdResource …
}
Then you can do
$image = new Image; // to create an empty Image instance
$image->loadFromFile('pic.jpg') // to load a GD resource
PHP's imagecreate* function return a resource. If you want to send it to the client, you'll have to set the appropriate headers and then send the raw image:
header('Content-Type: image/jpeg');
imagejpeg($img);
See the GD and Image Functions documentation for details.
class Image
{
public $source = '';
function __construct($filename)
{
$fp = fopen($filename, 'rb') or die("Image $filename doesn't exist");
$buf = '';
while(!feof($fp))
{
$buf .= fgets($fp, 4096);
}
$this->source = imagecreatefromstring($buf);
}
}
$image = new Image('image.jpg');
/* use $image->source for image processing */
header('Content-Type: image/jpeg');
imagejpeg($image->source);
If you just want to display the image, all of the above is irrelevant. You just need to write out an HTML image tag, e.g.
echo '<img src="pic.jpg" />';
That's it.
The code that you have given takes a very long and inconvenient way to load an image for manipulation using the GD library; that's almost certainly not what you wanted to do, but if you did, then you could use imagecreatefromjpeg instead.

Creating two pdf pages with Imagick

Currently i can create PDF files from images in Imagick with this function
$im->setImageFormat("pdf");
$im->writeImage("file.pdf");
And it's possible to fetch multiple pages with imagick like this
$im = new imagick("file.pdf[0]");
$im2 = new imagick("file.pdf[1]");
But is it possible to save two image objects to two pages?
(example of what i am thinking, its not possible like this)
$im->setImageFormat("pdf");
$im->writeImage("file.pdf[0]");
$im2->setImageFormat("pdf");
$im2->writeImage("file.pdf[1]");
I know this is long past due, but this result came up when I was trying to do the same thing. Here is how you create a multi-page PDF file in PHP and Imagick.
$images = array(
'page_1.png',
'page_2.png'
);
$pdf = new Imagick($images);
$pdf->setImageFormat('pdf');
if (!$pdf->writeImages('combined.pdf', true)) {
die('Could not write!');
}
Accepted answer wasn't working for me, as a result, it always generated one page pdf (last image from constructor), to make this work I had to get file descriptor first, like this:
$images = array(
'img1.png', 'img2.png'
);
$fp = fopen('combined.pdf', 'w');
$pdf = new Imagick($images);
$pdf->resetiterator();
$pdf->setimageformat('pdf');
$pdf->writeimagesfile($fp);
fclose($fp);
Is this working?
$im->setImageFormat("pdf");
$im->writeImage("file1.pdf");
$im2->setImageFormat("pdf");
$im2->writeImage("file2.pdf");
exec("convert file*.pdf all.pdf");
CAM::PDF is a pure-Perl solution for low-level PDF manipulation like this. You can either use the appendpdf.pl command-line tool, or do it programmatically like this:
use CAM::PDF;
my $doc1 = CAM::PDF->new('file1.pdf');
my $doc2 = CAM::PDF->new('file2.pdf');
$doc1->appendPDF($doc2);
$doc1->cleanoutput('out.pdf');
If you can figure out how to make ImageMagick write to a string instead of to a file (I'm not an ImageMagick expert...) then you save some performance overhead by keeping it all in Perl.
(I'm the author of CAM::PDF. It's free software: GPL+Artistic dual-licensed).
I do not know php or this Imagick library, but if calling an external program is acceptable I can recommend the program pdfjoin to merge pdf files.
It does have an dependency to pdflatex, so if this is something you intend to run on a server you might have to install extra latex stuff, but the end result from pdfjoin is very good, so I think it will be worth it.

Categories