5

有没有办法将版权信息添加到由创建的图像文件中PHP

为了更清楚,您可以copyright使用 Photoshop 将信息添加到文件中,因此当您获取它时properties,您会看到类似于以下内容的内容:

在 Windows 7 中打开的图像文件的文件属性

我想在 php.ini 中添加/编辑文件的详细信息。有可能吗?

编辑:

我从用户输入中获取图像,然后使用此函数调整其大小:

 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;
 }
4

1 回答 1

4

我不相信 PHP 本身包含编辑 JPEG 文件中的 EXIF 数据的功能,但是有一个可以读取和写入 EXIF 数据的 PEAR 扩展。

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

该模块的网站位于http://lsolesen.github.io/pel/,设置描述的示例位于https://github.com/lsolesen/pel/blob/master/examples/edit-description.php

更新:

pearhub.org 网站似乎已关闭/永远消失了,但您可以从 GitHub 下载文件(无需安装/设置,只需包含autoload.php文件)。

下面是在 JPEG 文件中设置版权字段的示例。从 GitHub 下载的文件被放置在一个名为的子目录中,pel尽管您可以将它们放置在您喜欢的任何位置(只需更新该require_once行)。

<?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);
于 2015-06-12T11:00:17.467 回答