Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbrichardson/10ff12d4823aa493fee523e277a1fcf5 to your computer and use it in GitHub Desktop.
Save sbrichardson/10ff12d4823aa493fee523e277a1fcf5 to your computer and use it in GitHub Desktop.
Convert daily New York Times frontpage to jpg
<?php
// set the output-file
$outputfile = "/var/www/nyt/nyt.jpg";
// set path to todays NYT frontpage
$pathToPdf="https://static01.nyt.com/images/".date('Y')."/".date('m')."/".date('d')."/nytfrontpage/scan.pdf";
// check if there is any today
$file_headers = @get_headers($pathToPdf);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
}
else {
    $exists = true;
}
// if there's none today, get yesterdays
if(!$exists) {
$yesterday = date('Y/m/d',strtotime("-1 days"));
$pathToPdf="https://static01.nyt.com/images/".$yesterday."/nytfrontpage/scan.pdf";
}
// you needed to have Imagick compiled in PHP otherwise this won't work
$im = new Imagick();
// So this panel is 1440x2550, 600dpi, it doesn't really like PNG so we are converting a PDF to JPG (sue me)
// get it here, compression to nothing, output it
$im->setResolution(600,600);
$im->readimage($pathToPdf);
$im->setImageCompressionQuality(100);
$im->setImageFormat('jpg');
$im->stripImage();
$im->scaleImage(1440,2550);
$im->writeImage($outputfile);
$im->clear();
$im->destroy();
// and display it with nice margins
?>
<meta http-equiv="refresh" content="21600">
<body style="margin-top:-100px;margin-left:-27px">
<img width=102% height=108% src='<?=$outputfile?>?v=<?=date('Ymdhmi')?>' />
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment