Page 12 of 37
Re: Image Resizing Script required - a better one...
Posted: Wed Mar 08, 2017 12:07 pm
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?
Re: Image Resizing Script required - a better one...
Posted: Wed Mar 08, 2017 12:15 pm
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?
Re: Image Resizing Script required - a better one...
Posted: Wed Mar 08, 2017 12:34 pm
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?
Re: Image Resizing Script required - a better one...
Posted: Wed Mar 08, 2017 12:51 pm
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.
Re: Image Resizing Script required - a better one...
Posted: Wed Mar 08, 2017 12:53 pm
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?
Re: Image Resizing Script required - a better one...
Posted: Wed Mar 08, 2017 12:57 pm
by Celauran
Maybe. Why Consumer?
Re: Image Resizing Script required - a better one...
Posted: Wed Mar 08, 2017 1:06 pm
by simonmlewis
will this be used for admin too?
Re: Image Resizing Script required - a better one...
Posted: Wed Mar 08, 2017 1:08 pm
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.
Re: Image Resizing Script required - a better one...
Posted: Wed Mar 08, 2017 1:11 pm
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!
Re: Image Resizing Script required - a better one...
Posted: Wed Mar 08, 2017 1:14 pm
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.
Re: Image Resizing Script required - a better one...
Posted: Wed Mar 08, 2017 1:24 pm
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.
Re: Image Resizing Script required - a better one...
Posted: Wed Mar 08, 2017 1:53 pm
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?
Re: Image Resizing Script required - a better one...
Posted: Wed Mar 08, 2017 2:09 pm
by Celauran
What does your getSrcSet() look like and what is it returning?
Re: Image Resizing Script required - a better one...
Posted: Wed Mar 08, 2017 2:12 pm
by simonmlewis
That srcset section is just rendering ''.
Re: Image Resizing Script required - a better one...
Posted: Wed Mar 08, 2017 2:16 pm
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?