Page 16 of 37
Re: Image Resizing Script required - a better one...
Posted: Thu Mar 09, 2017 6:43 am
by Celauran
I have copied the code in your function definition and it's working perfectly locally, suggesting that the function itself is OK. I am, however, on a mac, so the DIRECTORY_SEPARATOR business wouldn't be an issue for me in any event.
Re: Image Resizing Script required - a better one...
Posted: Thu Mar 09, 2017 6:43 am
by Celauran
simonmlewis wrote:Wasn't expecting this:
string(0) ""
What's the context here? Function separated out and accessed directly?
Re: Image Resizing Script required - a better one...
Posted: Thu Mar 09, 2017 6:45 am
by simonmlewis
Code: Select all
include dirname(__DIR__) . '/vendor/autoload.php';
$image = "25644881shirt.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;
Re: Image Resizing Script required - a better one...
Posted: Thu Mar 09, 2017 6:47 am
by Celauran
And that's returning an empty string? So we're back to the image not being found. You've checked in explorer or whatever that the file is indeed there?
Re: Image Resizing Script required - a better one...
Posted: Thu Mar 09, 2017 6:48 am
by simonmlewis
If I go into the source code, the file that is the first one from the database, is in fact the one I am hardcoding to test with. No question - it's there.
Re: Image Resizing Script required - a better one...
Posted: Thu Mar 09, 2017 6:50 am
by Celauran
What's the output here? Still look right?
Code: Select all
<?php
include dirname(__DIR__) . '/vendor/autoload.php';
$image = "25644881shirt.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 = [];
var_dump($source_directory . DIRECTORY_SEPARATOR . $filename); 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);
}
// Again, just to get the function working. To be removed.
$srcset = getSrcSet($image);
var_dump($srcset);
exit;
Re: Image Resizing Script required - a better one...
Posted: Thu Mar 09, 2017 6:54 am
by simonmlewis
Bingo. Not sure the answer but found the flaw.
The code below renders this:
[text]string(95) "C:\xampp\phpMyAdmin\site-wide\includes\images\pages\25644881shirt.jpg" [/text]
Code: Select all
include dirname(__DIR__) . '/vendor/autoload.php';
$image = "25644881shirt.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;
$file_string = $source_directory . DIRECTORY_SEPARATOR . $filename;
$srcset = [];
var_dump($file_string); 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);
}
// Again, just to get the function working. To be removed.
$srcset = getSrcSet($image);
var_dump($srcset);
exit;
Why is 'includes' in there??
Re: Image Resizing Script required - a better one...
Posted: Thu Mar 09, 2017 6:58 am
by simonmlewis
Sorry - includes is where my *.inc files are stored. But why is it going in there?
Re: Image Resizing Script required - a better one...
Posted: Thu Mar 09, 2017 6:58 am
by Celauran
simonmlewis wrote:Bingo. Not sure the answer but found the flaw.
The code below renders this:
[text]string(95) "C:\xampp\phpMyAdmin\site-wide\includes\images\pages\25644881shirt.jpg" [/text]
Code: Select all
include dirname(__DIR__) . '/vendor/autoload.php';
$image = "25644881shirt.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;
$file_string = $source_directory . DIRECTORY_SEPARATOR . $filename;
$srcset = [];
var_dump($file_string); 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);
}
// Again, just to get the function working. To be removed.
$srcset = getSrcSet($image);
var_dump($srcset);
exit;
Why is 'includes' in there??
simonmlewis wrote:string(57) "C:\xampp\phpMyAdmin\site-wide\includes\images\pages"
This is correct, but nothing showing in srcset just yet..
You had mentioned earlier that this was OK. Wrapping __DIR__ in dirname(), so dirname(__DIR__), will remove includes from the path if that's what you're after.
Re: Image Resizing Script required - a better one...
Posted: Thu Mar 09, 2017 7:03 am
by simonmlewis
And we have a winner! That's PERFECT, but it only seems to do it for the first loop.
There are 8 of them. Do I need to put that Function somewhere else or am I missing something?
Re: Image Resizing Script required - a better one...
Posted: Thu Mar 09, 2017 7:05 am
by Celauran
simonmlewis wrote:And we have a winner! That's PERFECT, but it only seems to do it for the first loop.
There are 8 of them. Do I need to put that Function somewhere else or am I missing something?
What is doing what for the first loop? Functions should only need to be defined once and can be reused anywhere. That's the whole point. Can you show some example code that will highlight the issue you're having now?
Re: Image Resizing Script required - a better one...
Posted: Thu Mar 09, 2017 7:06 am
by Celauran
Also, just for the sake of being thorough, you have removed the $image as well as the var_dump and exit statements from the function? All the stuff we put in there for debugging needs to be removed. The file in its entirety should look something like this:
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);
}
Re: Image Resizing Script required - a better one...
Posted: Thu Mar 09, 2017 7:12 am
by simonmlewis
What a total prized fool - it's not showing them in srcset as the others don't have the correct file format of _475, they are -475.
So it's working exactly as it should be!!!
Those first two images are now echoing the _1920 for me.
So it WORKS.
Next then.. is that the "resize on the fly" ??
Re: Image Resizing Script required - a better one...
Posted: Thu Mar 09, 2017 7:14 am
by Celauran
simonmlewis wrote:What a total prized fool - it's not showing them in srcset as the others don't have the correct file format of _475, they are -475.
You could account for that. Check for _ first and if that's not found, check for -. Just a thought.
simonmlewis wrote:So it's working exactly as it should be!!!
Those first two images are now echoing the _1920 for me.
So it WORKS.
Awesome!
simonmlewis wrote:Next then.. is that the "resize on the fly" ??
Now we'll want to integrate the resizing functionality into this function file. We'll also need a means by which to determine which $resize value to use.
Re: Image Resizing Script required - a better one...
Posted: Thu Mar 09, 2017 7:18 am
by simonmlewis
Yes. So in that function, if it finds the file is NOT there, it starts the resize. But before that it needs to be told what $resize to use.
That can likely be done directly in the <img> tag somewhere? On the category pages (which I will also need to put this), they are ALL the same.
It's just the landing pages - ie. /shirts or /jackets. That sort of page that has different sized banners.
There are four at the moment.
categories
products (those these two are actually the same, I want to keep their variables separate.. in case one day they choose to change them!)
wide
desktopslider
If I can get the top three working, we are good.