Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Copyright and Authors info to Images created in PHP?

Is there any way to add Copyright info to the image file created by PHP?

To be clearer, you can add copyright info to a file with photoshop, so when you get its properties, you see something similar to:

File Properties of an Image File opened in Windows 7

I want to Add/Edit Details info of a file in php. Is is possible?

EDIT:

I get an image from user input, then resize it with this function:

 function image_resize($src, $w, $h, $dst, $width, $height, $extension )
 {
   switch($extension){
     case 'bmp': $img = imagecreatefromwbmp($src); break;
     case 'gif': $img = imagecreatefromgif($src); break;
     case 'jpg': $img = imagecreatefromjpeg($src); break;
     case 'png': $img = imagecreatefrompng($src); break;
     default : return "Unsupported picture type!";
  }
   $new = imagecreatetruecolor($width, $height);
  // preserve transparency
    if($extension == "gif" or $extension == "png"){
     imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
      imagealphablending($new, true);
     imagesavealpha($new, false);
   }
   imagecopyresampled($new, $img, 0, 0, 0, 0, $width, $height, $w, $h);
   imageinterlace($new,1);//for progressive jpeg image
   switch($extension){
     case 'bmp': imagewbmp($new, $dst); break;
     case 'gif': imagegif($new, $dst); break;
     case 'jpg': imagejpeg($new, $dst); break;
     case 'png': imagepng($new, $dst); break;
   }
   return true;
 }
like image 459
Ormoz Avatar asked Sep 28 '22 15:09

Ormoz


1 Answers

I don't believe that PHP natively contains a function to edit the EXIF data in a JPEG file, however there is a PEAR extension that can read and write EXIF data.

pear channel-discover pearhub.org
pear install pearhub/PEL 

Website for the module is at http://lsolesen.github.io/pel/ and an example for setting the description is at https://github.com/lsolesen/pel/blob/master/examples/edit-description.php

UPDATE:

It seems the pearhub.org site is down / gone forever, but you can download the files from GitHub (no installation / setup required, just include the autoload.php file).

Below is an example to set the copyright field in a JPEG file. Files downloaded from GitHub are placed in a subdirectory called pel though you can place them where-ever you like (just update the require_once line).

<?php

// Make the PEL functions available
require_once 'pel/autoload.php';  // Update path if your checked out copy of PEL is elsewhere

use lsolesen\pel\PelJpeg;
use lsolesen\pel\PelTag;
use lsolesen\pel\PelEntryCopyright;

/*
 * Values for you to set
 */

// Path and name of file you want to edit
$input_file = "/tmp/image.jpg";

// Name of file to write output to
$output_file = "/tmp/altered.jpg";

// Copyright info to add
$copyright = "Eborbob 2015";


/*
 * Do the work
 */

// Load the image into PEL
$pel = new PelJpeg($input_file);

// Get the EXIF data (See the PEL docs to understand this)
$ifd = $pel->getExif()->getTiff()->getIfd();

// Get the copyright field
$entry = $ifd->getEntry(PelTag::COPYRIGHT);

if ($entry == null)
{
        // No copyright field - make a new one
        $entry = new PelEntryCopyright($copyright);
        $ifd->addEntry($entry);
}
else
{
        // Overwrite existing field
        $entry->setValue($copyright);
}

// Save the updated file
$pel->saveFile($output_file);
like image 87
Eborbob Avatar answered Oct 03 '22 03:10

Eborbob