Write EXIF into JPG with PHP - php

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.

Related

php How to reduce file size using gd and upload to folder [duplicate]

I have a site with about 1500 JPEG images, and I want to compress them all. Going through the directories is not a problem, but I cannot seem to find a function that compresses a JPEG that is already on the server (I don't want to upload a new one), and replaces the old one.
Does PHP have a built in function for this? If not, how do I read the JPEG from the folder into the script?
Thanks.
you're not telling if you're using GD, so i assume this.
$img = imagecreatefromjpeg("myimage.jpg"); // load the image-to-be-saved
// 50 is quality; change from 0 (worst quality,smaller file) - 100 (best quality)
imagejpeg($img,"myimage_new.jpg",50);
unlink("myimage.jpg"); // remove the old image
I prefer using the IMagick extension for working with images. GD uses too much memory, especially for larger files. Here's a code snippet by Charles Hall in the PHP manual:
$img = new Imagick();
$img->readImage($src);
$img->setImageCompression(Imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(90);
$img->stripImage();
$img->writeImage($dest);
$img->clean();
You will need to use the php gd library for that... Most servers have it installed by default. There are a lot of examples out there if you search for 'resize image php gd'.
For instance have a look at this page http://911-need-code-help.blogspot.nl/2008/10/resize-images-using-phpgd-library.html
The solution provided by vlzvl works well. However, using this solution, you can also overwrite an image by changing the order of the code.
$image = imagecreatefromjpeg("image.jpg");
unlink("image.jpg");
imagejpeg($image,"image.jpg",50);
This allows you to compress a pre-existing image and store it in the same location with the same filename.

Edit exif data of an image [duplicate]

On our site, we get a large amount of photos uploaded from various sources.
In order to keep the file sizes down, we strip all exif data from the source using mogrify:
mogrify -strip image.jpg
What we'd like to be able to do is to insert some basic exif data (Copyright Initrode, etc) back onto this new "clean" image, but I can't seem to find anything in the docs that would achieve this.
Has anybody any experience of doing this?
If it can't be done through imagemagick, a PHP-based solution would be the next best thing!
Thanks.
You can save a large amount of space, especially if you have a large number of images..
Add the following to text.txt (format of the IPTC tags taken from here):
2#110#Credit="My Company"
2#05#Object Name="THE_OBJECT_NAME"
2#55#Date Created="2011-02-03 12:45"
2#80#By-line="BY-LINE?"
2#110#Credit="The CREDIT"
2#115#Source="SOURCE"
2#116#Copyright Notice="THE COPYRIGHT"
2#118#Contact="THE CONTACT"
2#120#Caption="AKA Title"
Strip all existing exif data from the image
mogrify -strip image.jpg
Add the credit to your image
mogrify -profile 8BIMTEXT:text.txt image.jpg
Exiftool looks like it would be an exact match for you.
I haven't tried it but I'm now tempted to go and fix all my honeymoon photos which are marked 01/01/2074 because I forgot to reset the date after the batteries died.
Here's a PHP Exif Library that should do what you need.
The PHP Exif Library (PEL) lets you
fully manipulate Exif (Exchangeable
Image File Format) data. This is the
data that digital cameras place in
their images, such as the date and
time, shutter speed, ISO value and so
on.
Using PEL, one can fully modify the
Exif data, meaning that it can be both
read and written. Completely new Exif
data can also be added to images. PEL
is written completely in PHP and
depends on nothing except a standard
installation of PHP, version 5. PEL is
hosted on SourceForge.
on linux there is a program called jhead. It can add a minimal exif header with the command:
jhead -mkexif img.jpg
I doubt you will gain lot of space by removing Exif information...
Anyway, I can be wrong, but Exif metadata belongs more to store technical (and contextual) information. For stuff like copyright, you should use IPTC instead.
That's something you can do, apparently, with ImageMagick: Write IPTC Data to Jpeg with ImageMagick.
You can do this directly in PHP using the PEL library. You would do this by simply overwriting the existing EXIF-headers,
// Load image data
$data = new PelDataWindow(file_get_contents('IMAGE PATH'));
// Prepare image data
$jpeg = $file = new PelJpeg();
$jpeg->load($data);
// Create new EXIF-headers, overwriting any existing ones (when writing to disk)
$exif = new PelExif();
$jpeg->setExif($exif);
$tiff = new PelTiff();
$exif->setTiff($tiff);
// Create Ifd-data that will hold EXIF-tags
$ifd0 = new PelIfd(PelIfd::IFD0);
$tiff->setIfd($ifd0);
// Create EXIF-data for copyright
$make = new PelEntryAscii(PelTag::COPYRIGHT, '2008-2017 Conroy');
$ifd0->addEntry($make);
// Add more EXIF-data...
// Save to disk
$file->saveFile('IMAGE.jpg');
You can find a complete list of all supported EXIF-data (PelTag) in the PEL docs.

Upload jpg/png, convert to pdf and save with PHP?

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.

How to read Lightroom keywords from image file using PHP?

I have a photo community (www.jungledragon.com) that allows users to upload photos. My platform is PHP/CodeIgniter.
As part of the upload process I'm already reading EXIF info using PHP's exif_read_data function, which works fine. I read camera details and show these on an info tab.
On top of that, user's are expected to manually set the photo title, description and tags on the website after uploading the photo. However, some users manage these fields in their image management program, for example Lightroom. It would be great if I could read those as well, uploading would become a total joy.
I already improved my EXIF reading to read the "caption", this way users don't have to set the image title after uploading anymore. Now I'm looking to read keywords, which is where I am stuck. Here's a partial screenshot of an image in Lightroom:
I can read the Metadata, but how do I read the keywords? The fact that it is not inside metadata makes me wonder if it's at all possible? I've tried reading every value I can get (ANY_TAG, IFD0, EXIF, APP12) using exif_read_data, but the keywords are not to be found.
Any thoughts?
As suggested you may have to use another method of reading metadata.
http://www.foto-biz.com/Lightroom/Exif-vs-iptc-vs-xmp
Image keywords may be stored in IPTC and not in EXIF. I don't know if there is a standard platform method for reading iptc but a quick google shows this
http://php.net/manual/en/function.iptcparse.php
Try using PEL, a much more comprehensive library than exif_read_data() for exif data.
After a long research, i found the solution to get keywords exported by lightroom in a jpg file :
$image = getimagesize($imagepath, $info);
if(isset($info['APP13']))
{
$iptc = iptcparse($info['APP13']);
$keywordcount = count($iptc["2#025"]);
for ($i=0; $i<$keywordcount; $i++)
{
echo "keyword : " . $iptc["2#025"][$i] . "<br/>";
}
}

ImageMagick: Tiff to PDF from PHP

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

Categories