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 »

At the top of this page:

Code: Select all

   <?php
include dirname(__DIR__) . '/vendor/autoload.php';
include '/functions/functionConsumerResize.php';
?>
In the area of the page where it all happens:

Code: Select all

echo "><a href='$row->url'>";
$image = $row->image;
$srcset = getSrcSet($image, 'categories');
echo "<img src='/images/pages/$image' srcset='{$srcset}'><p>$row->freetext</p>";
Have I done the include wrong?
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 »

That looks right. What's inside functionConsumerResize?
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 »

This is a straight copy paste of the file:

Code: Select all

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

$source_directory = $root_directory . $images_directory;
$file_string = $source_directory . DIRECTORY_SEPARATOR . $filename;
$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_path}/{$fullname} {$size}w";
}
}
}

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

// Returns an array of widths, keyed by target screen size
function getWidths($resize_option)
{
    switch($resize_option) {
        case 'categories':
        case 'products':
            $widths = [
                '475' => 237,
                '768' => 384,
                '1920' => 451,
            ];
            break;
        case 'wide':
            $widths = [
                '475' => 237,
                '768' => 384,
                '1920' => 950,
            ];
            break;
        case 'desktopslide':
            $widths = [
                '475' => 475,
                '768' => 768,
                '1920' => 1920,
            ];
            break;
        default:
            $widths = [];
            break;
    }

    return $widths;
}

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

So it's the old file before the recent changes were made. OK. Look at the inputs that getSrcSet accepts and look at what you're passing it. Do you see why it's not working?
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 »

Hang on yeah I have messed that up.
Just looked at your code, where is "resize.php" coming from??
That's not a file I know of.
IS this all going into ONE .php file? to then include on pages like this.

Sorry have i gotten lost a bit??
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 called it resize.php, you called it functionConsumerResize.php. They're meant to be the same file. I pasted the entire contents of my resize.php earlier. It contains five functions, including the modified getSrcSet that accepts the resize type string rather than an array of sizes.
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 I suspect would ultimately be best is to wrap the whole thing in a class and have it autoloaded, but let's focus on getting things working before we progress to that stage.
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 »

OMG! So I just saw the (__DIR__) areas I don't need, so I removed the braces.
I then loaded the page.
Saw another one. Fixed that.
Reloaded. And the page came up. Nothing unusual.
Checked the code, all srcsets are in there.
Checked the images/pages folder, and low and behold, a load of images have been generated!

My next job then is to add this brief code into the Products section, and the 'wide' section, and see what happens.

I need a closer inspection of those functions as well, to really get my head around it.
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 »

Awesome! Glad it's working out so well. Essentially, getting it working in other areas should entail including the file, calling the same function, and changing the second parameter as needed. That's it.
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 »

This one isn't working:

Code: Select all

echo "<a href='/product/$categreplace/$subcategreplace/$rowprod->id/$titlereplace'>";
$image = $row->image;
$srcset = getSrcSet($image, 'products');
echo "<img src='/images/pages/$image' srcset='{$srcset}' alt='$row->freetext'/>";
The original is in there, if I right click and view image, it tries to show the _1920 image which has not been generated.
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 included the file with the functions? It's not complaining about functions not existing or anything? Have you tried var_dump and exit inside the getSrcSet function to make sure it's being called?
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 »

Gaaaarrrr... I was being an idiot. I forget I had two sections of products One that isn't being used!!!
That's why.

Here's a step into this challenge then.
We also use a thing called Stock Banners. So the admin chap when he creates products and uploads their photos, he also create a 'stock banner'.
When he wants to promote a product but doesnt' have time to create a banner at the time, he might have one "in stock". A library if you like.

So he types in the stock code, up comes the product banner (if one was set), checks the box and that gets used.
This resize on the fly tool would be good to do those too. Same format as 'products'.

But - it needs to be stored in a different folder.
products goes in /images/page.
stock banners go in /images/stockbanners.

So how could I go above saying:
$srcset = getSrcSet($image, 'stockbanners');
Using the same size as products, but storing in different place??
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 »

A couple of things. First, you'd need to define some sizes for 'stockbanners' in your getWidths function, otherwise it won't know what to resize to. Next is we've got images/pages hard-coded into these functions. They'd need to be modified to accept the path as a parameter so the functions can know where to look for images and where to save the ones they create. Think that's enough info for you to get started making these modifications?
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 can see how to add it to the list for sizing, but not how to tell it which folders to use for stockbanners.
In theory, it's /images/pages, else, /images/stockbanners.
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 see how images are passed into the function as an argument and are then used within the function, yes? Try taking the same approach for the path.
Post Reply