Page 14 of 37

Re: Image Resizing Script required - a better one...

Posted: Thu Mar 09, 2017 5:44 am
by Celauran
Alternately,

Code: Select all

$srcset = getSrcSet($image);
echo "<img src='/images/pages/$image' srcset='{$srcset}'><p>$row->freetext</p>";

Re: Image Resizing Script required - a better one...

Posted: Thu Mar 09, 2017 5:46 am
by simonmlewis
Still an empty result.
How do I add in the function to ensure it is finding the image ??

Re: Image Resizing Script required - a better one...

Posted: Thu Mar 09, 2017 5:49 am
by Celauran
Time to start debugging the function itself. Easiest first step is to probably put

Code: Select all

var_dump('Found'); exit;
right before the $basename = line. Run the function, see if it dies where we're expecting it to. If it returns, the file isn't being found. If it dies with 'Found' then we know it's finding the file and we move on from there.

Re: Image Resizing Script required - a better one...

Posted: Thu Mar 09, 2017 5:50 am
by Celauran
It's probably also worth dumping out $source_directory to ensure that looks right. Keeping in mind that you're on a Windows machine, you may need to replace forward slashes with the DIRECTORY_SEPARATOR constant.

Re: Image Resizing Script required - a better one...

Posted: Thu Mar 09, 2017 5:53 am
by simonmlewis
It shows no error on screen at all.
How do I do that dumpin gout of $source_directory.

I did wonder about the forward slashes. When I echoed the directory, there are \ back slashes and / forwards.
Maybe that's the key here?!

Re: Image Resizing Script required - a better one...

Posted: Thu Mar 09, 2017 5:55 am
by Celauran

Code: Select all

var_dump(whatever you want here); exit;
That will spit out the string, variable, whatever and then halt execution. Bit of a blunt instrument but it'll do for now.

Re: Image Resizing Script required - a better one...

Posted: Thu Mar 09, 2017 5:56 am
by simonmlewis

Code: Select all

include dirname(__DIR__) . '/vendor/autoload.php';
function getSrcSet($filename, $desired_sizes = [475, 768, 1920])
{
    $root_directory = __DIR__;
    $images_directory = '/images/pages';
    $source_directory = $root_directory . $images_directory;
    $srcset = [];

    if (file_exists($source_directory . '/' . $filename)) {
    var_dump('Found'); exit;
    var_dump($source_directory); exit;
        $basename = pathinfo($filename, PATHINFO_FILENAME);
        $extension = pathinfo($filename, PATHINFO_EXTENSION);

        foreach ($desired_sizes as $size) {
            $fullname = $basename . '_' . $size . '.' . $extension;
            if (file_exists($source_directory . '/' . $fullname)) {
                $srcset[] = "{$images_directory}/{$fullname} {$size}w";
            }
        }
    }

    return implode(', ', $srcset);
}  
This echoes nothing additional on screen.

Re: Image Resizing Script required - a better one...

Posted: Thu Mar 09, 2017 5:59 am
by Celauran
The second one would never execute because the first one ends with an exit statement. That does, however, confirm that the file isn't being found. Move the var_dump($source_directory) up above the if statement and run it again. Looks like $source_directory is the problem.

Re: Image Resizing Script required - a better one...

Posted: Thu Mar 09, 2017 6:00 am
by simonmlewis
string(57) "C:\xampp\phpMyAdmin\site-wide\includes/images/pages"

I think we have found our problem.....

Re: Image Resizing Script required - a better one...

Posted: Thu Mar 09, 2017 6:01 am
by Celauran
Looks like. Aside from the mix of forward and backslashes, does the path itself look correct?

Re: Image Resizing Script required - a better one...

Posted: Thu Mar 09, 2017 6:02 am
by simonmlewis
The path is correct, yes. As you say, apart from the forwards and backs.
So I guess I need to use Directory separator in there somewhere...??

Re: Image Resizing Script required - a better one...

Posted: Thu Mar 09, 2017 6:06 am
by Celauran
You will. You also need to keep '/images/pages' for use in the srcset output.

Re: Image Resizing Script required - a better one...

Posted: Thu Mar 09, 2017 6:08 am
by simonmlewis
Sorry how do I keep it to use there, if I need to make those backslashes?
Or do I need TWO of them? One with, one without?

Re: Image Resizing Script required - a better one...

Posted: Thu Mar 09, 2017 6:10 am
by Celauran
I'd use two, maybe $images_directory and $images_path? Use DIRECTORY_SEPARATOR for one for the purposes of locating the files, and use '/' for the other as that's what the web server expects regardless of OS.

Re: Image Resizing Script required - a better one...

Posted: Thu Mar 09, 2017 6:14 am
by simonmlewis

Code: Select all

include dirname(__DIR__) . '/vendor/autoload.php';
function getSrcSet($filename, $desired_sizes = [475, 768, 1920])
{
    $root_directory = __DIR__;
    $images_directory = DIRECTORY_SEPARATOR."images".DIRECTORY_SEPARATOR."pages";
    $images_path = '/images/pages';
    
    $source_directory = $root_directory . $images_directory;
    $srcset = [];

    if (file_exists($source_directory .DIRECTORY_SEPARATOR. $filename)) {
    
        $basename = pathinfo($filename, PATHINFO_FILENAME);
        $extension = pathinfo($filename, PATHINFO_EXTENSION);

        foreach ($desired_sizes as $size) {
            $fullname = $basename . '_' . $size . '.' . $extension;
            if (file_exists($source_directory .DIRECTORY_SEPARATOR. $fullname)) {
                $srcset[] = "{$images_directory}/{$fullname} {$size}w";
            }
        }
    }

    return implode(', ', $srcset);
}  
I'm getting there, but I know this isn't right.
The source_directory now echos on screen correctly, but srcset not showing anything... hencing knowing I haven't got it quite right.