Been doing a fair bit of digging this morning, and not seeing an obvious answer - is it possible to save an image to pdf format using PHP (or one of it's many libraries)?
I am fairly familiar with GD, although it doesn't seem to have a built in PDF format exporter/save function from my reading so far.
If anyone has any suggestions, it would be much appreciated!!
I tried to add this to the accepted answer. Here is an example of how to convert an image to a different format (including pdf) with the Imagick module:
$img = new Imagick('path/to/image.jpg');
$img->setImageFormat('pdf');
$success = $img->writeImage('path/to/image.pdf');
OR
$img = new Imagick();
$img->readImageBlob($imageBytes);
$img->setImageFormat('pdf');
$success = $img->writeImage('path/to/image.pdf');
I see 2 other options :
the pdflib extension, but the opensource edition is quite limited (I don't know if you can use image functions without a paid license)
Zend_Pdf, which is a plain-PHP lib, part of the Zend Framework.
Related
Or other image file. Can this be done in a (fairly) simple way using PHP?
I'm coding a website that will allow users to upload photos, but I know JPEGs are notorious for their metadata and ideally I'd like to strip all images uploaded of metadata, either by removal or replacement with junk text.
If you're just looking to strip most of the exif data quickly and easily without using a library to specifically write it, you can 'resave' the image using gd:
$file = 'myjpg.jpg';
$im = imagecreatefromjpeg($file);
imagejpeg($im, 'myjpg2.jpg');
Maybe not the best/prettiest solution, but it accomplishes what you want without adding additional libraries.
Take a look at this extension for PHP: http://lsolesen.github.io/pel/
i use getID3 library for this. it makes it quite easy
http://getid3.sourceforge.net/
it works with al lot of file extentions
for a demo click here:
demo
I want to have a system in my Joomla website where user can upload their image and create and design a product image by having the product template. For example the keychain needs to be design.
Can any one recommend is this possible with PhP? or I need some other platform to work on.
Thanks
This can be done with GD or Imagemagick libraries for PHP.
Here is a sample script I wrote a while back to composite an image to a blank coffee mug using PHP and imagemagick. This should be very similar to what you're trying to do.
<?php
$im = new Imagick('image.jpg');
$mug = new Imagick('images/mug.png');
$im->scaleImage(300, 0);
$im->setImageFormat('png');
$im->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$im->setImageMatte(true);
$height = $im->getImageHeight();
$mug->compositeImage($im, $im->getImageCompose(), 80, 130);
header("Content-Type: image/png");
echo $mug;
?>
If you want to provide a kind of graphic editor to users most likely you need a JavaScript implementation with PHP only handling the final result of it.
A common way to edit images with PHP is to use Imagick, but your case doesn't really sound like that. This is more applicable to, say, cases when you need to resize all the uploaded image or to save them in a different format.
For a couple of days I'm trying to write (or update) EXIF information (geotag, latitude and longitude) in a JPG image using PHP.
After consulting many sites without success I think the best option is to use Imagick but although it seems I can set the latitude and longitude with setImageProperty(), but when I write the picture the EXIF is not saved.
Here I give a code fragment:
//Loading existing image
$edited = new Imagick(dirname(__FILE__)."/mini.jpg");
//Stripping the curren EXIF info. I think is not mandatory and I try to comment but nothing...
$edited->stripImage();
//Setting the new properties
$edited->setImageProperty('exif:GPSLatitude', '30/1, 46/1, 58605/1000');
$edited->setImageProperty('exif:GPSLongitude', '63/1, 57/1, 35550/1000');
$propiedades = $edited->getImageProperties();
var_dump($propiedades);
var_dump($edited->writeImage('mini_edited.jpg'));
//reading the new image EXIF Info
$readedited = new Imagick(dirname(__FILE__)."/mini_edited.jpg");
$propiedades_edited = $readedited->getImageProperties();
The image is saved successfully but no the exif information updates.
Anyone have an idea how I can solve this problem with this or any other tool?
The only requirement is to use PHP
Thank you very much in advance!
The only way I've found is to install PEL - the PHP Exif Library
The gd or ImageMagick libraries will help you do this sort of thing.
If you are using shared hosting one (or both of them) may have been installed for you.
How can I convert 2 tiff images to PDF, I already knows how to get the image out of the DB, and I print it using echo and setting up the MIME type.
But, right know I need to use a duplex printer option, so I need a way to generate a PDF from inside my PHP page, that PDF must containt both TIFF images (one per page) How can I do that? What do I need for php to work with that library.
Thank you very much.
EDIT:
Is a self hosted app, I own the server (actually I'm using WAMP 2).
I extract the images from the MySQL DB (stored using LONGBLOBS).
There is a very simple PHP script that interfaces with ImageMagick:
How to convert multipage TIFF to PDF in PHP
I haven't used it myself but it looks all right.
For this you will need
ImageMagick installed
Ghostscript installed
the linked article describes how to install those in a Ubuntu Linux environment.
Another road to take would be inserting the images directly into a auto-generated PDF file without ImageMagick. The best-known PDF generation library, FPDF, can do this, but for JPEG, PNG and GIF only.
Maybe one of these works for you.
What you really need is a library that brings you a PDF composition engine. And of course you need that engine to support image insertions (specifically TIFF).
The best option is iText.
public void createPdf(String filename) throws DocumentException, IOException
{
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
document.add(new Paragraph("PDF Title"));
// step 5
document.add(new Image("Tiff image path..."));
// step 6
document.close();
}
Hope it helps!
Using imagick library, below solution worked for me -
$document = new Imagick($path."/".$fileName.tiff);
$data = $document->getImageBlob();
$document->setImageFormat("pdf");
$document->writeImages($path."/".$fileName.pdf, true);
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.