PHP写入读取图片自定义信息_exif或者什么的?
PHP 图片
需求描述:使用PHP对图片文件进行优化处理,但此优化处理只能进行一次,二次进行会对图片造成不可恢复的损坏。期望在图片一次处理之后,在图片文件中写入一个自定义的字符串,什么都行,仅用于判断图片是否经过处理。
期待可以使用PHP解决。目前想到JPEG格式的话有 EXIF信息,可以尝试从这个作为突破,在EXIF信息中写入一个自定义的字符串。但GIF,PNG,BMP呢,怎么能解决常用的网络图片格式?
google到这货
Imagick::setImageProperty
根据文档http://www.php.net/manual/zh/imagick.setimageproperty.php
貌似刚好符合要求,但是使用这个函数保存图片,再次读取却为空,不晓得是不是我的用法有问题
$image = new Imagick($file);$image->setImageProperty('Exif:Make', 'Imagick');$image->writeImage($file2);$image2 = new Imagick($file2);echo $image->getImageProperty('Exif:Make');
这个是一个思路,但没能实现,真希望是我用法有问题,而不是这个函数不可实现。
又尝试使用 APP13
$size = getimagesize('1.jpg', $info);var_dump($size);if(isset($info['APP13'])){ $iptc = iptcparse($info['APP13']); var_dump($iptc);}
但也不行。
难道我一直在走弯路?有什么好的办法吗?要的不多,就是想在不破快图片的基础上在图片数据中写入一个字符串,供下次读取这个字符串。
大大们现身吧,给点指点,解决问题立刻散分。
回复讨论(解决方案)
对于 GD 支持的图片,均可以在图片数据结束后附加自定义数据,而不影响图片的内容
示例代码
$url = 'http://avatar.profile.csdn.net/0/E/F/1_jaylecn.jpg';$im = imagecreatefromjpeg($url);imagepng($im, 'test.png'); //产生一个 png 图片文件$s = 'abcefg'; //待附加的信息file_put_contents('test.png', sprintf('%sInfo%s', $s, pack('n', strlen($s))), FILE_APPEND); //按自定义格式附加在图片文件之后//回读$s = file_get_contents('test.png');$t = unpack('A4t/noffs', substr($s, -6)); //取回自定义信息的长度$v = substr($s, -6 - $t['offs'], -6); //取回自定义信息echo $v;//abcdef
请自行验证图片数据没有被破坏
其实大致有这样一个概念:可以边读取边展示的基本是流式数据,这样的数据,头文件将说明了mimetype和长度(或有结束标志),那么在长度(或结束标志)之后的“额外”数据并不影响主体数据的展示,依此概念,甚至rar、zip等也能做手脚,呵呵
谢谢两位回答,看样子思路是一样的,就是在文件的数据格式之外插入一段字符。
开始也想到过这个办法,手工编辑了一个几个文件,其中有一个GIF动态图插入之后,图片遭到损坏。当时认为这种方法不可行,就没继续深入。我又使用了那张手工修改被破坏的GIF图,用xuzuning的代码测试了一下,竟然是可以的,看来手工修改确实不行。
这个方案看来基本可行了,那么问题就是 我恐怕 没办法去测试所有的图片格式,这种方法是否具有确定性呢?(GD只要支持,就一定没问题?)
PS:我先去测试几种格式的图片试试。
用Imagick类可以实现,需要PHP安装imagemagick扩展模块
我在发帖的时候就已经说了,google到了一个 Imagick 的函数,但实际上是无法写入的,不知道楼上说的用Imagick实现是怎么实现的?
是的,GD 支持的图片都是可以的(gif、jpg、png)
因为此类图片都是压缩保存的,在文件头部都有一个数据区长度的表识。
图片显示时,软件通过该标识读取并解压数据
但对于 GD 不支持的,比如 bmp 文件,就没有那么幸运了。
bmp 的数据是从文件尾向文件头方向排列的,如果你在文件尾部添加内容,就将破坏图片数据
其实最简单的测试方法就是准备你的二进制数据和原始文件,然后用DOS命令
copy /b 原始文件 + 二进制数据 新文件
然后检查这个新文件有没问题
先用这种方案吧,有个PHP类或者什么的,可以兼容各种图片格式就好了。这个需求我觉得倒还挺常见的,比如批量给图片打水印,可以给图片加个属性来判断是否已经水印过了。
结贴,感谢两位

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
