Skip to main content
pixxxelschubser
Community Expert
Community Expert
Jun 11, 2016 11:14 AM UTC
Answered

metadata width and height does not for png and gif

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 is closed to new replies. Start a new post to keep the conversation going.
SuperMerlin
SuperMerlinCorrect answer
Inspiring
Jun 11, 2016 4:00 PM UTC

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());

}

10 replies

SuperMerlin
Inspiring
Jun 11, 2016 12:34 PM UTC

Try adding this line before accessing the metadata.

app.synchronousMode = true;

Translate
pixxxelschubser
Community Expert
Community Expert
Jun 11, 2016 12:41 PM UTC

Hi SuperMerlin,

nice to see you here too.

But this change unfortunately makes no difference.

Translate
SuperMerlin
Inspiring
Jun 11, 2016 12:50 PM UTC

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());

}