Skip to content

Instantly share code, notes, and snippets.

@CodeBrauer
Created July 31, 2014 10:39
Show Gist options
  • Save CodeBrauer/f7412fc578f41cb66a05 to your computer and use it in GitHub Desktop.
Save CodeBrauer/f7412fc578f41cb66a05 to your computer and use it in GitHub Desktop.
Converts px to cm (with DPI) and variable precision
<?php
function get_cm_by_px($px, $dpi = 300, $precision = 2) {
$comma = ','; // if US use '.'
return number_format(2.54 * $pic->exif->size->height / $dpi , $precision, $comma,'') ;
}
@CodeBrauer
Copy link
Author

Example:

1000px Photo = 8,47cm (if printed with 300DPI)

<?php
$px = '1000px';

function get_cm_by_px($px, $dpi = 300, $precision = 2) {
    $comma = ','; // if US use '.'
    return number_format(2.54 * (int)$px / $dpi , $precision, $comma,'') ;
}

var_dump(get_cm_by_px($px));

Result: (test with all PHP 4.3.0 - 5.6.0 versions) -> http://3v4l.org/Z1LmO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment