PHP (>=5.4), 199 197 bytes
(-2 bytes by more golfing)
<?$s=strlen(file_get_contents($argv[1])).'';echo strtr([waning_crescent,waning_gibbous,new_moon,0,waxing_crescent,waxing_gibbous,full_moon,first_quarter,third_quarter][($s[0]+$s[3])%11-2],'_',' ');
To run it:
php -d error_reporting=0 -d short_open_tag=1 <filename> <image_path>
Example:
php -d error_reporting=0 -d short_open_tag=1 lunar_phase.php https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Moon_phase_6.svg/240px-Moon_phase_6.svg.png
Notes:
- The
-d error_reporting=0
option is used to not output notices/warnings.
- The
-d short_open_tag=1
is required to allow short tags.
- If you are using a
https
URL like the example above, OpenSSL should also be enabled.
How?
Gets file size (bytes) and creates a unique number for it by this formula:
((<first_bytes_digit> + <fourth_bytes_digit>) % 11) - 2
This formula generates numbers from 0 to 8 with only a 3 missing.
┌─────────────────┬───────┬─────────┬─────┬────────────────────────┐
│ Phase │ Bytes │ 1st+4th │ %11 │ -2 (position in array) │
├─────────────────┼───────┼─────────┼─────┼────────────────────────┤
│ new moon │ 3451 │ 3+1=4 │ 4 │ 2 │
│ waxing crescent │ 6430 │ 6+0=6 │ 6 │ 4 │
│ first quarter │ 5144 │ 5+4=9 │ 9 │ 7 │
│ waxing gibbous │ 7070 │ 7+0=7 │ 7 │ 5 │
│ full moon │ 5283 │ 5+3=8 │ 8 │ 6 │
│ waning gibbous │ 7067 │ 7+7=14 │ 3 │ 1 │
│ third quarter │ 4976 │ 4+6=10 │ 10 │ 8 │
│ waning crescent │ 6337 │ 6+7=13 │ 2 │ 0 │
└─────────────────┴───────┴─────────┴─────┴────────────────────────┘
Previous approaches:
PHP (>=5.4), 251 bytes
<?foreach([4,8,16,20]as$w){$a+=imagecolorat(imagecreatefrompng($argv[1]),$w*10,120)>1e7;$a&&$w<5?$b=-2:0;}$x=explode('_','full moon_waning gibbous_third quarter_waning crescent_new moon_waxing crescent_first quarter_waxing gibbous');echo$x[$a*++$b+4];
To run it:
php -d error_reporting=0 -d short_open_tag=1 <filename> <image_path>
Example:
php -d error_reporting=0 -d short_open_tag=1 lunar_phase.php https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Moon_phase_6.svg/240px-Moon_phase_6.svg.png
Notes:
- The
-d error_reporting=0
option is used to not output notices/warnings.
- The
-d short_open_tag=1
is required to allow short tags.
- PHP must have GD and it should be enabled.
- If you are using a
https
URL like the example above, OpenSSL should also be enabled.
How?
Checks for the color of 4 pixels in the image at 40,120
, 80,120
, 160,120
and 200,120
and decides on the moon phase from those colors.
1
As a bonus, check out this cool gif: https://upload.wikimedia.org/wikipedia/commons/b/ba/Lunar_libration_with_phase_Oct_2007_450px.gif
– Beta Decay – 2018-07-20T15:45:24.867