【问题标题】:How to add Copyright and Authors info to Images created in PHP?如何将版权和作者信息添加到用 PHP 创建的图像?
【发布时间】:2015-06-12 10:31:24
【问题描述】:

有什么方法可以将版权信息添加到PHP 创建的图像文件中?

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

我想在 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;
 }

【问题讨论】:

  • 如何在 php 中制作/编辑图像?你的代码是什么样的?
  • @DocRattie,我编辑了我的问题并添加了一些创建图像文件的代码
  • @Nikko Tnanks 非常有帮助。但显然它只适用于jpeg 文件。 gifpng 等其他图像格式是否有可用的方法?
  • 在不了解情况的情况下,对用户上传的图片设置作者和版权,感觉有点鸡肋。仅仅因为他们将图像上传到您的网站并不会将所有权/版权分配给您。

标签: php image file properties file-attributes


【解决方案1】:

我不相信 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);

【讨论】:

  • 谢谢,但我认为修改EXIF数据并不常见。你能用它创建和修改Copyright 字段吗?
猜你喜欢
  • 1970-01-01
  • 2012-06-27
  • 2016-07-30
  • 2019-08-22
  • 2015-04-24
  • 1970-01-01
  • 1970-01-01
  • 2013-08-22
  • 2012-03-01
相关资源
最近更新 更多