Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
Some older versions of PHP may complain so I used more strict syntax on the date object, PHP tags and I also made it generic to call the convert function without a path.

This should make it more compatible.
  • Loading branch information
graiz committed Mar 30, 2021
1 parent 854ac6b commit 6672565
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@
function getCounter() {
global $maxPapers;
$fp = fopen("counter.txt", "r");
$x= intval(fread($fp,1024));
if ($fp) {
$x= intval(fread($fp,1024));
fclose($fp);
} else {
$x = 0;
}
if ($x > $maxPapers) {
$x = 0;
}
fclose($fp);
if (strlen($_REQUEST['index'])) { // Override the counter if there
if (!empty($_REQUEST)) { // Override the counter if there
$x = $_REQUEST['index']; // is a URL parameter for index
}
return($x);
Expand All @@ -46,7 +50,7 @@ function incrementCounter(int $counter){
// fetch a paper and cache in as a JPG. Return the path to the JPG if we found it.
// We can pass in an offset in days to get yesterday or two days ago
function fetchPaper($prefix, $offset=0){
$pathToPdf = "https://cdn.freedomforum.org/dfp/pdf" . date(d,strtotime("-" . $offset . " days")) . "/" . $prefix . ".pdf";
$pathToPdf = "https://cdn.freedomforum.org/dfp/pdf" . date('d',strtotime("-" . $offset . " days")) . "/" . $prefix . ".pdf";
$pdffile = "archive/" . $prefix . "_" . date('Ymd',strtotime("-" . $offset . " days")) . ".pdf";
$jpgfile = "archive/" . $prefix . "_" . date('Ymd',strtotime("-" . $offset . " days")) . ".jpg";
$rootpath = getcwd() . "/";
Expand All @@ -67,7 +71,7 @@ function fetchPaper($prefix, $offset=0){
}
if ($exists) { // convert a high-dpi image, force a white background and
// resize to 1600px wide at 95% jpg quality
$command = '/bin/convert -density 300 -background white -alpha remove ' . $rootpath . $pdffile .
$command = 'convert -density 300 -background white -alpha remove ' . $rootpath . $pdffile .
' -colorspace RGB -resize 1600 -quality 95 ' . $rootpath . $jpgfile;
exec($command, $output, $response);
}
Expand Down Expand Up @@ -104,7 +108,7 @@ function fetchPaper($prefix, $offset=0){
</style>
</head>
<body>
<?if (strlen($jpgfile)> 0) {
<?php if (empty($imageresult)) {
echo "Newspaper File Not Found. " . $imageresult. " Will keep looking. Checking again in another hour.";
} else {
echo "<img src='" . $imageresult . "' class='paper' >";
Expand All @@ -113,7 +117,7 @@ function fetchPaper($prefix, $offset=0){
</div>
</body>
</html>
<?
<?php
incrementCounter($currentIndex);
?>

0 comments on commit 6672565

Please sign in to comment.