search
HomeBackend DevelopmentPHP TutorialPHP写入读取图片自定义信息_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类或者什么的,可以兼容各种图片格式就好了。这个需求我觉得倒还挺常见的,比如批量给图片打水印,可以给图片加个属性来判断是否已经水印过了。

结贴,感谢两位

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

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.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.