About as off topic as I can think of

I’m trying to do a quick PHP task that will eventually convert into a script that takes a bunch of specific types of pictures and creates a bunch of HTML files from them. I’m starting by reading a list of files into an array using the scandir() function, then I want to look at those filenames and move them different arrays based on the first letter of the filename. Here’s the code:
<?php
$f = array();
$p = array();
$t = array();
$i = array();
$dir = ‘./’;
$files = scandir($dir);
print_r($files);
$thumbs = array();
$thumbs = array_filter($files, “thumb”);
print_r($thumbs);

    function thumb($var)
    {
        // returns all entries where the first character is 't'
    	$i = 0;
    	foreach ($var as $key => $value) {
    		if (strpos($value, 't', function ($strpos) use ($t)) == 0)  {
    			$t[$i] = $var[$key];
    			$i++;
    		}
    	}
        return $t;
    }
    ?>

I’m seeing an error:
“Warning: Invalid argument supplied for foreach() in C:\Users\billsey\Documents\My Webs\www.seapexshow.org\exhibits2021test\1920ASFB\build.php on line 21”
“Notice: Undefined variable: t in C:\Users\billsey\Documents\My Webs\www.seapexshow.org\exhibits2021test\1920ASFB\build.php on line 27”
The first line is the foreach statement, the second the return statement.
Anyone familiar enough with PHP to give me a hint? I’m going to end up with four arrays, the last of which will be further parsed into a two dimensional array. Once the arrays are correct I’ll start on the real output. :slight_smile:

I haven’t used php in a long time. And I was never very into it. Are you sold on php? I think a lot of new development is all done in javascript. Even on the back end. Php and html are just not used AFAICT in many new developments.

Just looking at this anyway. I can’t see where you are calling the function, but it looks like var needs to be an array. If it is, then the next thing I would do is change all these variable names. The single letters and thumbs, thumb, var, key, value all looks like it could be causing collisions with something. I would also get rid of the one letter variable names, but that might just be my pet peeve :slight_smile:

1 Like

Oh man. I might not be much use here either.

from what I can tell of the script, it looks correct.

Where is ‘thumb’ being called? is the value being passed in to $var an array ?

This is where the thumb() function is called… $files is passed.

I was under the assumption javascript was more a client side thing. I’m running this on my machine to generate the files which will be published to a web server. The actual site isn’t using any scripting for this portion.

Aha. The array filter looks like it is already iterating over the array and giving just one value to the thumb function. You should either just call $thumbs = thumb($files) or remove the foreach in the thumb function.

Node.js has made javascript live on the server side as well. There are also a ton of good python server solutions (if it was just for my hobby use, I would use the flask library).

I may be wrong. I bet it matters if you are making this for a few (<50) users or for millions.

Heck, I’m making for one user… I did the whole thing manually for the last virtual show I setup and it took close to six weeks to handle a quarter of the normal size. Once I get this working I can get rid of 90% of my web building time and start looking at automating the image generation.

I’m not sure what image generation you are doing, but if it is simple stuff like cropping and resizing, you can automate a bunch of that with image magick. I use it in Linux. I’m not sure if there is a windows version. It can certainly automate making thumbnails.

I start with a full size image of the scanned page. It is typically 8.5x11 at 300 or 600 DPI, so pretty big. I then resize to 800 pixels wide and save it under a different name, then resize again to 200 pixels wide and save a third copy. I then go back to the original and crop and save each item on the page separately. The first two could be automated, the last pretty much has to be manual.
Once the html for the pages has been created by the script I’ll be manually setting the mapped areas to match those last cropped images, so when someone clicks on an item on the page they see it full sized. If the click on the background of the page they see the original full sized scan. The smallest images are used as thumbnails in index pages…

Here’s an example that I did manually:
http://seapexshow.org/exhibits2020/SPE/SPE1.html

Cool! Reminds me of using the Ruby Sinatra app back in the day. Never could get my head around rails.

I did the same thing in php a long time ago. Jeff is right in that ImageMagick is perfect to automate the thumbnail creation too. Php even has a library for calling ImageMagick from inside it.

These days I use python for everything.

With flask, you can make an html template. In the template, you can have placeholders for things like imageName and then in the python you can configure it to return any request to "pages/<pagename>" to return the template with imageName=something. In that way, it would be able to do this. You can also do for loops in the templates and stuff like that.

Honestly, I don’t know if that would help you. But it would be a lot easier for me, since I know Python :smiley:. The result wouldn’t be a static webpage you could just copy though, the python would have to serve it forever.

Yeah, the key I missed was the behavior of array_filter. Fixing that gives me the expected results:
<?php

$f = array();
$p = array();
$t = array();
$i = array();

$dir    = './';
$files = scandir($dir);

$t = array_filter($files, "thumb");
$p = array_filter($files, "pic");
$f = array_filter($files, "full");
$i = array_filter($files, "item");

print_r($t);
print_r($p);
print_r($f);
print_r($i);

function thumb($var)
{
// returns all entries where the first character is ‘t’
$first = $var[0];
if ($first === ‘t’) {
return $var;
}
return false;
}

function pic($var)
{
// returns all entries where the first character is ‘p’
$first = $var[0];
if ($first === ‘p’) {
return $var;
}
return false;
}

function full($var)
{
// returns all entries where the first character is ‘f’
$first = $var[0];
if ($first === ‘f’) {
return $var;
}
return false;
}

function item($var)
{
// returns all entries where the first character is ‘i’
$first = $var[0];
if ($first === ‘i’) {
return $var;
}
return false;
}

?>
Now I just have to put the meat on the bones. :slight_smile: Though I’ll look closely at ImageMagick, I’ve had scripts running it in the past, but they were script someone else wrote…