Image Resizing Script required - a better one...
Moderator: General Moderators
Re: Image Resizing Script required - a better one...
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...
What's the context here? Function separated out and accessed directly?simonmlewis wrote:Wasn't expecting this:
string(0) ""
-
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...
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;Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Image Resizing Script required - a better one...
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?
-
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...
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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Image Resizing Script required - a better one...
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;-
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...
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]
Why is 'includes' in there??
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;Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
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...
Sorry - includes is where my *.inc files are stored. But why is it going in there?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Image Resizing Script required - a better one...
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]Why is 'includes' in there??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;
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.simonmlewis wrote:string(57) "C:\xampp\phpMyAdmin\site-wide\includes\images\pages"
This is correct, but nothing showing in srcset just yet..
-
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...
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?
There are 8 of them. Do I need to put that Function somewhere else or am I missing something?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Image Resizing Script required - a better one...
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?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?
Re: Image Resizing Script required - a better one...
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);
}
-
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...
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" ??
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" ??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Image Resizing Script required - a better one...
You could account for that. Check for _ first and if that's not found, check for -. Just a thought.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.
Awesome!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.
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.simonmlewis wrote:Next then.. is that the "resize on the fly" ??
-
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...
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.
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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.