5

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;
 }
Ormoz
  • 2,975
  • 10
  • 35
  • 50
  • 1
    How do you make/edit the images in php? What does your code look like? – DocRattie Jun 12 '15 at 10:32
  • 1
    @DocRattie, I edited my question and added some of my codes that creates image file – Ormoz Jun 12 '15 at 10:43
  • 2
    have you tried this? http://php.net/manual/en/function.iptcembed.php – Nikko Jun 12 '15 at 10:53
  • @Nikko Tnanks it was very helpful. But apparently it only serves for `jpeg` files. Are there any method available for other image formats like `gif` and `png`? – Ormoz Jun 12 '15 at 19:04
  • Without knowing the circumstances, I find it a bit tasteless setting the author and copyright on images uploaded by users. Just because they’re uploading images to your site doesn’t assign ownership/copyright to you. – Martin Bean Jun 12 '15 at 19:18
  • @MartinBean Actually I want to read the `copyright` data from the uploaded file and append it to the file created by `php` – Ormoz Jun 12 '15 at 19:21

1 Answers1

4

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);
Eborbob
  • 1,905
  • 1
  • 15
  • 30
  • thanks, But I think it is not common to modify `EXIF` data. Can you create and modify `Copyright` field with that? – Ormoz Jun 12 '15 at 19:10