Page 18 of 37

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

Posted: Thu Mar 09, 2017 9:10 am
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?

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

Posted: Thu Mar 09, 2017 9:11 am
by Celauran
That looks right. What's inside functionConsumerResize?

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

Posted: Thu Mar 09, 2017 9:12 am
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;
}

?>

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

Posted: Thu Mar 09, 2017 9:15 am
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?

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

Posted: Thu Mar 09, 2017 9:20 am
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??

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

Posted: Thu Mar 09, 2017 9:22 am
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.

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

Posted: Thu Mar 09, 2017 9:23 am
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.

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

Posted: Thu Mar 09, 2017 9:29 am
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.

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

Posted: Thu Mar 09, 2017 9:32 am
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.

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

Posted: Thu Mar 09, 2017 9:43 am
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.

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

Posted: Thu Mar 09, 2017 9:46 am
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?

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

Posted: Thu Mar 09, 2017 9:56 am
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??

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

Posted: Thu Mar 09, 2017 10:03 am
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?

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

Posted: Thu Mar 09, 2017 10:05 am
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.

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

Posted: Thu Mar 09, 2017 10:10 am
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.