Skip to main content
pixxxelschubser
Community Expert
Community Expert
June 11, 2016
Answered

metadata width and height does not for png and gif

  • June 11, 2016
  • 1 reply
  • 1343 views

Hi

the following code works as expected for the most file types

var file = app.document.getSelection("psd, jpg, jpeg, png, gif");

alert(file[0].core.quickMetadata.height);

alert(file[0].core.quickMetadata.width);

But it returns always 0 for all png and gif files.

How can I get the width and height value for png and gif files with bridge scripting (without to open the files in Photoshop)

Regards

This topic has been closed for replies.
Correct answer SuperMerlin

Yes, this works for the first part of my question as well.

Thanx for this.

Do you have also an answer for the second part --> width and height of gif files?


This seems to work.

File.prototype.readByte = function() {

   return this.read(1).charCodeAt(0);

};

File.prototype.readShort = function(ET) {

   var self = this;

   var b1 = this.readByte();

   var b2 = this.readByte();

   if(ET == "LE"){

      return (b2 << 8) + b1;

   }else{

      return (b1 << 8) + b2;

   }

};

File.prototype.readLong = function(ET) {

   var self = this;

   var s1 = self.readShort(ET);

   var s2 = self.readShort(ET);

   if(ET == "LE"){

      return (s2 << 16) + s1;

   }else{

      return (s1 << 16) + s2;

   }

};

var folder = Folder.selectDialog( "Please select input folder");

var GIFS = folder.getFiles("*.gif");

var Info = [];

for (var a in GIFS){

var file = File(GIFS);

file.encoding = 'BINARY';

file.open('r');

file.seek(6,0);

var Width = file.readShort("LE");

var Height = file.readShort("LE");

file.close();

Info.push([[decodeURI(file.name)],[Width],[Height]]);

}

for(var z in Info){

$.writeln(Info.toString());

}

SuperMerlin
Inspiring
June 11, 2016

Try adding this line before accessing the metadata.

app.synchronousMode = true;

pixxxelschubser
Community Expert
Community Expert
June 11, 2016

Hi SuperMerlin,

nice to see you here too.

But this change unfortunately makes no difference.

SuperMerlin
Inspiring
June 11, 2016

Here is another way of getting png details.

File.prototype.readByte = function() {

   return this.read(1).charCodeAt(0);

};

File.prototype.readShort = function(ET) {

   var self = this;

   var b1 = this.readByte();

   var b2 = this.readByte();

   if(ET == "LE"){

      return (b2 << 8) + b1;

   }else{

      return (b1 << 8) + b2;

   }

};

File.prototype.readLong = function(ET) {

   var self = this;

   var s1 = self.readShort(ET);

   var s2 = self.readShort(ET);

   if(ET == "LE"){

      return (s2 << 16) + s1;

   }else{

      return (s1 << 16) + s2;

   }

};

var folder = Folder.selectDialog( "Please select input folder");

var PNGS = folder.getFiles("*.png");

var Info = [];

for (var a in PNGS){

var file = File(PNGS);

file.encoding = 'BINARY';

file.open('r');

file.seek(16,0);

var Width = file.readLong();

var Height = file.readLong();

var BitDepth = file.readByte();

var ColourType = file.readByte();

var CompressionType = file.readByte();

var FilterType = file.readByte();

var InterlaceType = file.readByte();

file.close();

Info.push([[decodeURI(file.name)],[Width],[Height],[BitDepth],[ColourType],[CompressionType],[FilterType],[InterlaceType]]);

}

for(var z in Info){

$.writeln(Info.toString());

}