Android Question image height and width while still on server?

Todd Carlton

Member
Licensed User
Longtime User
Hello Community!

This one has me stumped. I admit up front that I don't even have any trial code to post. Apologies.

How would I get the height and width (in px) of a .jpg from a server without downloading it first?

The .jpg is on a server that can be accessed via browser or FTP. Access is easy. The answer eludes me.

Thank you for any direction; especially links to study.

-OCS
 

DonManfred

Expert
Licensed User
Longtime User
If you have running php on that server you can call a php-script which return the size of one or more images...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
@Todd Carlton btw: WELCOME to basic4android community!

b4arulez.gif


PHP:
<?php
header("content-type: text/plain");
$result = array();

foreach (glob('/path/to/images/*.png') as $filename) {
    $thisfile = array();

    $path_parts = pathinfo($filename);

    $thisfile["filename"] = $path_parts['basename'];
    $thisfile["ext"] = $path_parts['extension'];
    #echo $filename." - Größe: " . filesize($filename) . "\n";
    $size = getimagesize( $filename);
  $thisfile["width"] = $size[0];
  $thisfile["height"] = $size[1];
  # echo "Bildbreite: " . $size[0];
  # echo "Bildhöhe: " . $size[1];
  $result[] = $thisfile;
}
echo json_encode($result);
?>


sample output
[{"filename":"Aufnahme1.png","ext":"png","width":527,"height":281},{"filename":"aufgemotzterschlitten0001.png","ext":"png","width":479,"height":437},{"filename":"aufgemotzterschlitten0002.png","ext":"png","width":1360,"height":768},{"filename":"b4aupdates0001.png","ext":"png","width":527,"height":281},{"filename":"card-deck-02.png","ext":"png","width":779,"height":321},{"filename":"klassisch0002.png","ext":"png","width":305,"height":371},{"filename":"klassisch001.png","ext":"png","width":415,"height":263},{"filename":"label0001.png","ext":"png","width":258,"height":56},{"filename":"label0004.png","ext":"png","width":609,"height":428},{"filename":"label0005.png","ext":"png","width":997,"height":302},{"filename":"like.png","ext":"png","width":129,"height":130},{"filename":"like0001.png","ext":"png","width":693,"height":680},{"filename":"like0002.png","ext":"png","width":866,"height":75},{"filename":"like0003.png","ext":"png","width":199,"height":198},{"filename":"snap001.png","ext":"png","width":1920,"height":1080},{"filename":"snap0010.png","ext":"png","width":1042,"height":410},{"filename":"snap0011.png","ext":"png","width":402,"height":309},{"filename":"snap0012.png","ext":"png","width":177,"height":108},{"filename":"snap0013.png","ext":"png","width":160,"height":94},{"filename":"snap0014.png","ext":"png","width":530,"height":348},{"filename":"snap0017.png","ext":"png","width":675,"height":884},{"filename":"snap0018.png","ext":"png","width":726,"height":509},{"filename":"snap002.png","ext":"png","width":590,"height":179},{"filename":"snap0020.png","ext":"png","width":1094,"height":559},{"filename":"snap0021.png","ext":"png","width":693,"height":245},{"filename":"snap0022.png","ext":"png","width":1,"height":1},{"filename":"snap0023.png","ext":"png","width":38,"height":28},{"filename":"snap0024.png","ext":"png","width":656,"height":113},{"filename":"snap0025.png","ext":"png","width":1,"height":13},{"filename":"snap0026.png","ext":"png","width":550,"height":39},{"filename":"snap0027.png","ext":"png","width":1019,"height":537},{"filename":"snap0028.png","ext":"png","width":331,"height":490},{"filename":"snap0029.png","ext":"png","width":443,"height":60},{"filename":"snap003.png","ext":"png","width":600,"height":169},{"filename":"snap0030.png","ext":"png","width":269,"height":132},{"filename":"snap0031.png","ext":"png","width":1490,"height":713},{"filename":"snap0032.png","ext":"png","width":921,"height":625},{"filename":"snap0033.png","ext":"png","width":1094,"height":630},{"filename":"snap0034.png","ext":"png","width":1084,"height":633},{"filename":"snap0035.png","ext":"png","width":380,"height":187},{"filename":"snap004.png","ext":"png","width":611,"height":215},{"filename":"snap0041.png","ext":"png","width":814,"height":993},{"filename":"snap0042.png","ext":"png","width":389,"height":70},{"filename":"snap0043.png","ext":"png","width":197,"height":245},{"filename":"snap0044.png","ext":"png","width":683,"height":469},{"filename":"snap0045.png","ext":"png","width":287,"height":165},{"filename":"snap0046.png","ext":"png","width":389,"height":172},{"filename":"snap0050.png","ext":"png","width":1860,"height":960},{"filename":"snap0051.png","ext":"png","width":119,"height":124},{"filename":"snap0052.png","ext":"png","width":770,"height":97},{"filename":"snap0053.png","ext":"png","width":753,"height":460},{"filename":"snap0054.png","ext":"png","width":335,"height":120},{"filename":"snap0055.png","ext":"png","width":636,"height":224},{"filename":"snap0056.png","ext":"png","width":808,"height":58},{"filename":"snap0057.png","ext":"png","width":839,"height":83},{"filename":"snap0058.png","ext":"png","width":432,"height":210},{"filename":"snap0059.png","ext":"png","width":448,"height":210},{"filename":"snap0060.png","ext":"png","width":456,"height":220},{"filename":"snap0061.png","ext":"png","width":200,"height":71},{"filename":"snap0062.png","ext":"png","width":191,"height":34},{"filename":"snap0063.png","ext":"png","width":610,"height":98},{"filename":"snap0064.png","ext":"png","width":910,"height":84},{"filename":"snap0065.png","ext":"png","width":927,"height":140},{"filename":"swtor0007.png","ext":"png","width":443,"height":255},{"filename":"tooleap0002.png","ext":"png","width":878,"height":414},{"filename":"tooleap0003.png","ext":"png","width":830,"height":696},{"filename":"tooleap0005.png","ext":"png","width":885,"height":745},{"filename":"tooleap0006.png","ext":"png","width":886,"height":392},{"filename":"tooleap001.png","ext":"png","width":748,"height":64},{"filename":"updatestill.png","ext":"png","width":527,"height":281}]

Code to parse this jsonstring (for ex in a httpjobs Jobdone-sub)
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As List = parser.NextArray
For Each colroot As Map In root
  Dim height As Int = colroot.Get("height")
  Dim width As Int = colroot.Get("width")
  Dim filename As String = colroot.Get("filename")
  Dim ext As String = colroot.Get("ext")
Next

As you are new to B4A i´ll show you a complete example on how to do call the above php-script on a server and getting the result from it back to your app.
Please find the example attached. :)

See basic4android log for results (the app don´t have a layout)

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
JobName: getimagesizes
Aufnahme1.png width=527, height=281
aufgemotzterschlitten0001.png width=479, height=437
aufgemotzterschlitten0002.png width=1360, height=768
b4aupdates0001.png width=527, height=281
card-deck-02.png width=779, height=321
klassisch0002.png width=305, height=371
klassisch001.png width=415, height=263
label0001.png width=258, height=56
label0004.png width=609, height=428
label0005.png width=997, height=302
like.png width=129, height=130
like0001.png width=693, height=680
like0002.png width=866, height=75
like0003.png width=199, height=198
snap001.png width=1920, height=1080
snap0010.png width=1042, height=410
snap0011.png width=402, height=309
snap0012.png width=177, height=108
snap0013.png width=160, height=94
snap0014.png width=530, height=348
snap0017.png width=675, height=884
snap0018.png width=726, height=509
snap002.png width=590, height=179
snap0020.png width=1094, height=559
snap0021.png width=693, height=245
snap0022.png width=1, height=1
snap0023.png width=38, height=28
snap0024.png width=656, height=113
snap0025.png width=1, height=13
snap0026.png width=550, height=39
snap0027.png width=1019, height=537
snap0028.png width=331, height=490
snap0029.png width=443, height=60
snap003.png width=600, height=169
snap0030.png width=269, height=132
snap0031.png width=1490, height=713
snap0032.png width=921, height=625
snap0033.png width=1094, height=630
snap0034.png width=1084, height=633
snap0035.png width=380, height=187
snap004.png width=611, height=215
snap0041.png width=814, height=993
snap0042.png width=389, height=70
snap0043.png width=197, height=245
snap0044.png width=683, height=469
snap0045.png width=287, height=165
snap0046.png width=389, height=172
snap0050.png width=1860, height=960
snap0051.png width=119, height=124
snap0052.png width=770, height=97
snap0053.png width=753, height=460
snap0054.png width=335, height=120
snap0055.png width=636, height=224
snap0056.png width=808, height=58
snap0057.png width=839, height=83
snap0058.png width=432, height=210
snap0059.png width=448, height=210
snap0060.png width=456, height=220
snap0061.png width=200, height=71
snap0062.png width=191, height=34
snap0063.png width=610, height=98
snap0064.png width=910, height=84
snap0065.png width=927, height=140
swtor0007.png width=443, height=255
tooleap0002.png width=878, height=414
tooleap0003.png width=830, height=696
tooleap0005.png width=885, height=745
tooleap0006.png width=886, height=392
tooleap001.png width=748, height=64
updatestill.png width=527, height=281
 

Attachments

  • getimagesizes.zip
    6.5 KB · Views: 138
Last edited:
Upvote 0
Top