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 »

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.
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"
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);
}  
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 »

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.
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 »

Nothing errors on screen, nothing in the srcset html.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
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 »

Iv'e dumped the $filename too to ensure that is working, and it is.
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 »

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.
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)) {
    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);
}  
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 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.
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 »

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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
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 bad grammar. Any other way of proving it is there?
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'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.
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 »

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?
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

<?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;
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'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.
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 »

Wasn't expecting this:
string(0) ""
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply