Image Resizing Script required - a better one...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

Alternately,

Code: Select all

$srcset = getSrcSet($image);
echo "<img src='/images/pages/$image' srcset='{$srcset}'><p>$row->freetext</p>";
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

Still an empty result.
How do I add in the function to ensure it is finding the image ??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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?!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

string(57) "C:\xampp\phpMyAdmin\site-wide\includes/images/pages"

I think we have found our problem.....
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

Looks like. Aside from the mix of forward and backslashes, does the path itself look correct?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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...??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

You will. You also need to keep '/images/pages' for use in the srcset output.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply