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

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 is that different from doing the full one... twice?
Can you show me what you mean?

The way I see it happening, is I check first to see if the file exists, by first breaking up the original (after putting that into a variable), and checking with {filename}_475 within a full target directory path.

Is that right?
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 »

simonmlewis wrote:Sorry how is that different from doing the full one... twice?
Mostly means you only need to update it once if something changes. Can also mean less typing, which means less likelihood of typos.
simonmlewis wrote:The way I see it happening, is I check first to see if the file exists, by first breaking up the original (after putting that into a variable), and checking with {filename}_475 within a full target directory path.

Is that right?
That sounds right, yes.
simonmlewis wrote:Can you show me what you mean?

Code: Select all

<?php

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

// Hard-coding an image name just for the sake of demonstration
$image = "tulsa.jpg";

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

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Show Image</title>
</head>
<body>
    <img src="/images/pages/<?= $image; ?>" srcset="<?= getSrcSet($image); ?>">
</body>
</html>
Does that make sense?
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 »

Regretfully, no.
I can see the directory stuff ok.
I don't see where $filename is getting it's information from.
I also don't see where the actual pixel size is being set. Assuming I should put these in an external function PHP file if it's own.
Am I missing a lot from this code, like a Save function?
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 »

simonmlewis wrote:I don't see where $filename is getting it's information from.
It's being passed in when the function is called. See how I'm calling getSrcSet($image)? That's where $filename comes from.
simonmlewis wrote:I also don't see where the actual pixel size is being set.
The second argument accepts an array of file suffixes. I have defaulted it to 475, 768, and 1920 but it can be overwritten with anything. That's not pixel sizes per se, but it allows you to check for the existence of specific files.
simonmlewis wrote:Am I missing a lot from this code, like a Save function?
This function simply creates the strset value, it doesn't create the missing files on the fly. One thing at a time.
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 »

So is it best to put that function in a separate file called "functionConsumerResize.php" for example, and "include" it in this page?
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 »

Maybe. Why Consumer?
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 »

will this be used for admin too?
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 »

simonmlewis wrote:will this be used for admin too?
I don't know. I'm not particularly familiar with the application, having only even seen bits and bobs you've shared in this thread. That's where I was going, though: Don't repeat yourself.
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 »

I'm thinking of naming it a file that means something to me.
I suspect this will be used only on consumer pages. As the admin ones... work!
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 »

Between this and the earlier resizing, I think an argument could easily be made to have a dedicated Image class. This is a step in the right direction, though.
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 »

Yeah. I"m prob going to do this function in the morning, as last night I had code running thru my head at night! OR later this evening if I fancy it.
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 »

The function is in an external file now.
The page looks like this:

Code: Select all

require_once '/functions/functionConsumerResize.php';
$image = $row->image;
echo "<img src='/images/pages/$image' srcset='"; getSrcSet($image); echo "'>
But it's now echoing only the standard images and not one that is resized. Though I can see them in the folder.
So how does it look for the _475.jpg type images?
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 »

What does your getSrcSet() look like and what is it returning?
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 »

That srcset section is just rendering ''.
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 »

What does the method look like? What arguments are you passing to it? Is the base directory correct? Is it finding any of the files?
Post Reply