Page 15 of 37

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

Posted: Thu Mar 09, 2017 6:16 am
by Celauran
That's progress. Now that $source_directory is correct, let's put the var_dump back inside the if statement to see if we're finding the original file.

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

Posted: Thu Mar 09, 2017 6:20 am
by simonmlewis
string(57) "C:\xampp\phpMyAdmin\site-wide\includes\images\pages"
This is correct, but nothing showing in srcset just yet..

Also, I altered the last but as it wasn't using image_path.

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 = [];
    var_dump($source_directory); exit;
    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_path}/{$fullname} {$size}w";
            }
        }
    }

    return implode(', ', $srcset);
}  

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

Posted: Thu Mar 09, 2017 6:23 am
by Celauran
Right, move that var_dump down a line so it's inside the if statement and just above the $basename line. We want to see if the file is being found or not.

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

Posted: Thu Mar 09, 2017 6:25 am
by simonmlewis
Nothing errors on screen, nothing in the srcset html.

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

Posted: Thu Mar 09, 2017 6:27 am
by simonmlewis
Iv'e dumped the $filename too to ensure that is working, and it is.

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

Posted: Thu Mar 09, 2017 6:28 am
by Celauran
Hmm. Sounds like it's not finding the file. If $source_directory and $filename are correct, and you've got the var_dump just inside the if statement, it should stop on that line.

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

Posted: Thu Mar 09, 2017 6:30 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)) {
    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 .DIRECTORY_SEPARATOR. $fullname)) {
                $srcset[] = "{$images_path}/{$fullname} {$size}w";
            }
        }
    }

    return implode(', ', $srcset);
}  

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

Posted: Thu Mar 09, 2017 6:31 am
by Celauran
Looks good. It should die and spit out the value of $source_directory. If it's not, that means the if statement is returning false, meaning the file isn't found.

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

Posted: Thu Mar 09, 2017 6:34 am
by simonmlewis
But it's echoing the file on screen...... the value of src. So it's definitely there.
Any one way of providing it's there, via the directory separator method?

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

Posted: Thu Mar 09, 2017 6:35 am
by simonmlewis
sorry bad grammar. Any other way of proving it is there?

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

Posted: Thu Mar 09, 2017 6:37 am
by Celauran
You've got this function separated out into its own file, yeah? Why not access that file directly and give it an image name you know exists. Ensure the function itself is working properly and returning the expected results, then move on to integrating this into the application as a whole. Fewer moving parts.

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

Posted: Thu Mar 09, 2017 6:38 am
by simonmlewis
No as you instructed for now, it is part of this file.
Could I still do as you suggest somehow tho, with an forced file?

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

Posted: Thu Mar 09, 2017 6:38 am
by Celauran

Code: Select all

<?php

require_once dirname(__DIR__) . '/vendor/autoload.php';

// Hard-coding an image name just for the sake of demonstration. Use one you know exists. We'll remove it later.
$image = "tulsa.jpg";

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)) {
        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 .DIRECTORY_SEPARATOR. $fullname)) {
                $srcset[] = "{$images_path}/{$fullname} {$size}w";
            }
        }
    }

    return implode(', ', $srcset);
}

// Again, just to get the function working. To be removed.
$srcset = getSrcSet($image);
var_dump($srcset);
exit;

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

Posted: Thu Mar 09, 2017 6:40 am
by Celauran
I'm trying to minimize the number of moving parts here. Makes it easier to identify where the failure is occurring. We can start by focusing on getting the function working as expected. Right now it's not clear to me if the function isn't working as intended or if there's a failure elsewhere that is causing the srcset value to not display correctly. The more context you can give me, the better.

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

Posted: Thu Mar 09, 2017 6:42 am
by simonmlewis
Wasn't expecting this:
string(0) ""