0

I have some PHP script fetching a PNG file from some remote location and saving it to some location on my server.

Several other servers check if a new PNG is available and download it if necessary. The problem is, that an image is considered "new" if the filesize has changed (this is weird, I know, but this can not be changed.)

Is there is a possibility to change the filesize with PHP without corrupting/changing/damaging the image itself?

yivi
  • 42,438
  • 18
  • 116
  • 138
Jakob
  • 778
  • 3
  • 9
  • 25
  • Better check the file date. – GolezTrol Dec 20 '17 at 10:00
  • Please note: "The problem is, that an image is considered "new" if the filesize has changed (this is wierd, I know, but this can not be changed.)" – Jakob Dec 20 '17 at 10:01
  • So you want to ensure the file size *has* changed so the other servers will download it? – deceze Dec 20 '17 at 10:05
  • In that case no. The normal functions for checking file size just read the file size. That's no behavior you can change from within a PHP script. If it's wrapped in a class, maybe there is some magic, but there is no way for us to know that without closely inspecting your code and have clear insight in which parts can change in which parts can't. Maybe you can get away with adding an extra byte to the end of the file. If that corrupts the file depends on how it's read. But it's a very ugly workaround for something that could have a simple solution. – GolezTrol Dec 20 '17 at 10:06
  • @GolezTrol I agree this would ba a ugly workaround but unfortunately there is no way to change the download functionality.... But adding some bytes to the end of the file could work, do you know some examples how to do it in PHP? – Jakob Dec 20 '17 at 10:10
  • 3
    `file_put_contents($file, "\x00", FILE_APPEND)`… The safer option might be to edit the PNG's meta data section though. – deceze Dec 20 '17 at 10:14
  • 3
    I wouldn't just add it to the end. That seems to invalidate the file. I'd rather investigate the [PNG file format](https://www.w3.org/TR/PNG-Structure.html) and see if there is a way to insert one of the optional chunks there with a little piece of harmless meta information. Of course you need to make sure that new image+chunk is not exactly as large as the old one, so you need to check whether or not you must insert the chunk. But really, talk to the boss or whatever, because this is typically the solution that will end up on https://thedailywtf.com/ – GolezTrol Dec 20 '17 at 10:15
  • @GolezTrol Thanks a lot for the hints, I'll give it a try. The reason why I cannot change the download funktionality is that the sources of the related piece of software are not available any longer - I have only some compiled Java programm. – Jakob Dec 20 '17 at 10:21
  • @Jakob I see. Been there, and it sucks when that happens. But it's also also a moment to consider rewriting or replacing it. Offering downloads shouldn't be *that* hard, and otherwise you are spending quite some effort in finding a workaround (which is also harder to maintain in the future). Not saying you must, of course, but at least consider replacing that piece of software with something that actually works the way you want, either your own or another 3rd party tool. – GolezTrol Dec 20 '17 at 10:35

1 Answers1

-1

Basically u can alter the metadata. https://www.w3.org/TR/2003/REC-PNG-20031110/#11textinfo

PNG provides the tEXt, iTXt, and zTXt chunks for storing text strings associated with the image, such as an image description or copyright notice. Keywords are used to indicate what each text string represents. Any number of such text chunks may appear, and more than one with the same keyword is permitted.

I would do it with imagemagick How to add extra meta data to PNG?

user5542121
  • 1,051
  • 12
  • 28