BlogProgrammingWordpress

How to copy EXIF information from one file to another using PEL?

Hey there, this is as small update to my other script that adds a watermark to your images, adding a way to copy EXIF information from one file to another using PEL. Since that article is in Portuguese, I will reproduce it here with the alterations needed to keep your EXIF information.

Why do you need this? Well, this is because the PHP library GD for image processing, strips all the EXIF information from your photographs. This is a bit annoying if you like me are sharing your photographic posts with other networks like Flikr that uses the EXIF information for more information in how the picture was taken, like camera settings, etc.

When I wrote the original post, I was looking for a WordPress plugin that somehow managed to make watermarks to my photos.

Of all available plugins found, few of them worked. And the ones that worked did not place the watermark as I would like, and these they all ended up putting the watermark on the photo permanently, preventing to make changes to the watermark, as the original was marked forever.

The immediate solution seemed that I had to get to work and make a watermark / signature to around +400 photos by hand, individually for each photo, and then replace one by one. Suddenly it looked like it would be easy, but quickly even using an automated system in Photoshop, I would spend a long time, and it didn’t solve my problem in a long term, because I would have to put the watermark by hand, and sincerely It is a process that does not please me much, it’s a boring way and I would end with the image with a permanent watermark, and to avoid this would have save more copies, among other problems.

So the ultimate solution, I have to go and create a script to create the watermark automatically (on the fly) each time the image is requested from the server, without ever damaging the original, because I want to at any time change my brand watermark / signature.

The requirements for that final pass to is to have an Apache server with mod_rewrite module and PHP5 with GD module.

Putting my hand to work, we need to edit (or create, if there is) the .htaccess file, in this case the root directory of the website. We put the following file:


<IfModule mod_rewrite.c>
RewriteRule ^(.*)wp-content/uploads/((.*)\.(jpe?g))$ //watermark/_watermark.php?src=wp-content/uploads/ [T=application/x-httpd-php,L,QSA]
</IfModule>

Generally, what this code does is :

  1. Check that the mod_reqrite Apache module is installed . If so
  2. Execute PHP file watermark.php for all images served from wp-content/uploads

Save and close the .htaccess file here do not need anything else.
Then create a folder in the root called watermark, which is where we will place a file named _watermark.php.

Yet, before we need an additional library. Remember GD doesn’t handle EXIF as it should.
Now goto this link, it will download a ZIP file with a library called PEL. PEL is a PHP Exif Library. A library for reading and writing Exif headers in JPEG and TIFF images using PHP.

  1. Inside your watermark folder, create a new folder called pel.
  2. Open the downloaded ZIP file, open the main folder (eg. lsolesen-pel-308f226)
  3. Copy all contents to the newly created pel folder

Now, the code for your _watermark.php

set_include_path('/your_server_path/watermark/pel/');
require_once('autoload.php');
use lsolesen\pel\PelJpeg;
use lsolesen\pel\PelTag;
use lsolesen\pel\PelIfd;
use lsolesen\pel\PelExif;

$original = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
waterMark($original);

function waterMark($original){

	$original = urldecode($original);

	$info = GetImageSize($original, $info_exif);
	$width = $info[0];
	$height = $info[1];

	$mime = $info['mime'];
	$type = substr(strrchr($mime, '/'), 1);

	$image_create_func = 'ImageCreateFromJPEG';
	$image_save_func = 'ImageJPEG';
	$new_image_ext = 'jpg';

	$new_image = ImageCreateFromJPEG($original);

	if ($width > 900){
		// bar at bottom with transparency
		$bg = ImageColorAllocateAlpha($new_image, 150, 150, 150, 65); // (PHP 4 >= 4.3.2, PHP 5)
		imagefilledrectangle($new_image, 0, $height-50, $width, $height, $bg); 

		// colors
		$white = imagecolorallocate($new_image, 255, 255, 255);
		$grey = imagecolorallocate($new_image, 128, 128, 128);
		$black = imagecolorallocate($new_image, 0, 0, 0);

		if ($width >= 900){
			// texto a escrever
			$text = 'http://fotografia.clerigo.pt';
			// caminho até à fonte ttf
			$font = '/your_server_path/watermark/KhmerUIb.ttf';

			// adicionar texto à imagem
			imagettftext($new_image, 20, 0, 10, $height-15, $white, $font, $text);

			// texto a escrever
			$text = date("Y") . ' © João Clérigo ';
			// caminho até à fonte ttf
			$font = '/your_server_path/watermark/segoepr.ttf';

			// adicionar sombra ao texto
			imagettftext($new_image, 20, 0, $width-299, $height-15, $black, $font, $text);

			// adicionar texto à imagem
			imagettftext($new_image, 20, 0, $width-300, $height-16, $white, $font, $text);
		}
	}

        // only copy EXIF information for images bigger than 2047px
	if ($width < 2048){ header("Content-Type: image/jpeg"); ImageJPEG($new_image); imagedestroy($new_image); }else{ $jpeg = new PelJpeg($original); $exif = $jpeg->getExif();

		$jpeg = new PelJpeg($new_image);
		$jpeg->setExif($exif);
		imagedestroy($new_image);
		header("Content-Type: image/jpeg");
		echo $jpeg->getBytes();
	}
}

And you’re done. From now on all images served from the specified folder always have a watermark.
Taking note two points:

  1. The mark will only be created for images with a width of more than 900 pixels
  2. True Type fonts are needed, which should be placed in a folder on the server

Related Articles

Deixe um comentário

O seu endereço de email não será publicado. Campos obrigatórios marcados com *

Este site utiliza o Akismet para reduzir spam. Fica a saber como são processados os dados dos comentários.

Back to top button
João Clérigo - Photography
Close

AdBlocker Detetado
AdBlocker Detected

Por favor ajude este website permitindo a visualização de alguns anúncios. Obrigado. Please help this website allowing the view of some advertising. Thank you!