Image Resizing Script required - a better one...
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...
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?
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?
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...
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:Sorry how is that different from doing the full one... twice?
That sounds right, yes.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?
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>-
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...
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?
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?
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...
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 don't see where $filename is getting it's information from.
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:I also don't see where the actual pixel size is being set.
This function simply creates the strset value, it doesn't create the missing files on the fly. One thing at a time.simonmlewis wrote:Am I missing a lot from this code, like a Save function?
-
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...
So is it best to put that function in a separate file called "functionConsumerResize.php" for example, and "include" it in this page?
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...
will this be used for admin too?
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...
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.simonmlewis wrote:will this be used for admin too?
-
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...
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!
I suspect this will be used only on consumer pages. As the admin ones... work!
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...
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.
-
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...
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.
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...
The function is in an external file now.
The page looks like this:
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?
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 "'>So how does it look for the _475.jpg type images?
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 does your getSrcSet() look like and what is it returning?
-
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...
That srcset section is just rendering ''.
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 does the method look like? What arguments are you passing to it? Is the base directory correct? Is it finding any of the files?