Page 31 of 37
Re: Image Resizing Script required - a better one...
Posted: Tue Mar 14, 2017 8:56 am
by Celauran
Very simple example:
Code: Select all
<?php
function sayHello($name)
{
return "Hello, {$name}";
}
echo sayHello('Simon');
The string 'Simon' gets passed to the sayHello function and is referred to internally as $name. Let's assume you wanted to run this function for a collection of users. You retrieve the users from your database and want to pass each of their names to this function. You're just going to call something like
Code: Select all
foreach ($users as $user) {
echo sayHello($user->first_name);
}
or something along those lines. Make sense?
Re: Image Resizing Script required - a better one...
Posted: Tue Mar 14, 2017 8:58 am
by simonmlewis
I see it takes the variables "in order". You have named them differently for whatever, reason, but the values are essentially the same. So I am guessing the usage of these variables within the functions, are mismatched somewhere........
Re: Image Resizing Script required - a better one...
Posted: Tue Mar 14, 2017 8:59 am
by Celauran
simonmlewis wrote:So that is the key to this. Where is it wrong?? I'm guessing $type and $save_path are the key. What have I messed up?
They are key. Essentially, in your function signature, you've said you're going to have a variable called $save_path, but inside the function you're referencing $type, which is what it was called elsewhere. The crucial thing is that those names match.
Re: Image Resizing Script required - a better one...
Posted: Tue Mar 14, 2017 8:59 am
by simonmlewis
Celauran wrote:Very simple example:
Code: Select all
<?php
function sayHello($name)
{
return "Hello, {$name}";
}
echo sayHello('Simon');
The string 'Simon' gets passed to the sayHello function and is referred to internally as $name. Let's assume you wanted to run this function for a collection of users. You retrieve the users from your database and want to pass each of their names to this function. You're just going to call something like
Code: Select all
foreach ($users as $user) {
echo sayHello($user->first_name);
}
or something along those lines. Make sense?
Yes I think so. As I say, I think one of these functions is using one variable at the top, and then the wrong one at the bottom (based on a variable way up the page).
Am I right?
Re: Image Resizing Script required - a better one...
Posted: Tue Mar 14, 2017 9:04 am
by Celauran
simonmlewis wrote:I see it takes the variables "in order".
Yes! The order of the parameters absolutely matters. Look at the
mail() function, which I know you're familiar with. The recipient must be the first argument. If you try to pass the subject first, you're going to have a bad time. Functions spell out which parameters they're expecting and the order in which they're expecting them so they can make some assumptions about the types of data they receive and how those can be used.
simonmlewis wrote:You have named them differently for whatever, reason, but the values are essentially the same.
Again, yes! This is key. We're concerned with the values, after all, not what variable those values are attached to. I try to name my variables and my function parameters descriptively so that it's easy to understand at a glance what they're doing and/or what they represent. I could have called $type $resize_type instead, but it's not terribly important.
simonmlewis wrote:So I am guessing the usage of these variables within the functions, are mismatched somewhere........
Right. You've got $save_path in the signature (the bit between the parentheses) and $type in the function body. They need to match.
Re: Image Resizing Script required - a better one...
Posted: Tue Mar 14, 2017 9:09 am
by Celauran
As I mentioned earlier in the thread, I encourage you to check out the free Laracasts series covering this sort of thing. I think it will be very beneficial.
viewtopic.php?f=1&t=143432&p=707648#p707648
Re: Image Resizing Script required - a better one...
Posted: Tue Mar 14, 2017 9:22 am
by simonmlewis
That's it. That's done it. It's working locally and live (demo) in generating the thumbnails on the fly.
So in that function where it has the variables one after the other, how do I know which one is which? As you say, they could be named so differently.
What's they key to that?
Also, you keep using the term "function signature". What do you mean?
And thanks - will do.
It now works on all the page we have worked hard on lately.
The category pages takes some churning, as it is generating the images. Whether that's ok SEO wise for now, or not, I'm not sure. Maybe we need to run one BIG page, maybe split into a few pages, and initiate that before we go live. Just to generate all the images.
Re: Image Resizing Script required - a better one...
Posted: Tue Mar 14, 2017 9:29 am
by Celauran
simonmlewis wrote:That's it. That's done it. It's working locally and live (demo) in generating the thumbnails on the fly.
Great!
simonmlewis wrote:So in that function where it has the variables one after the other, how do I know which one is which? As you say, they could be named so differently.
What's they key to that?
That's partly why descriptive naming is important. For functions you have written, you're going to know because you wrote it. For most functions, you'll have documentation explaining what each parameter is, which are required vs optional, and so on. It's also a good habit to get into to document your own functions so that when you come back to them in six months, or when someone else on the team needs to work with them, it's pretty clear what does what.
simonmlewis wrote:Also, you keep using the term "function signature". What do you mean?
The function signature is the name of the function and the list of parameters it accepts. Everything up to the opening brace, essentially. The function body is everything between the braces.
simonmlewis wrote:It now works on all the page we have worked hard on lately.
The category pages takes some churning, as it is generating the images. Whether that's ok SEO wise for now, or not, I'm not sure. Maybe we need to run one BIG page, maybe split into a few pages, and initiate that before we go live. Just to generate all the images.
Once they're generated, you needn't worry about it. Maybe write a crawler or something that will hit all your product pages, category pages, etc and generate the images for you. Run it overnight or something, when you know traffic is going to be low. Alternately, run it locally and upload the generated images.
Re: Image Resizing Script required - a better one...
Posted: Tue Mar 14, 2017 9:32 am
by simonmlewis
Yeah a simple overnight cron could do it. Wow what a journey.
Re: Image Resizing Script required - a better one...
Posted: Tue Mar 14, 2017 9:42 am
by simonmlewis
Ok here's one final quickie.
When the are resized on the fly there will be old stored versions (save as original filename in /images/productphotos) in .../small. ie. the original thumbnails.
How can I delete those on the fly, as the new ones are made? Else we are going to get duplicates forever more.
Re: Image Resizing Script required - a better one...
Posted: Tue Mar 14, 2017 10:06 am
by Celauran
Hmm. Can you give me some examples of this? Thumbnails are currently named the same as the original image, only with a suffix on the end. What sort of case are you anticipating? We can work on dealing with that.
Re: Image Resizing Script required - a better one...
Posted: Tue Mar 14, 2017 10:18 am
by simonmlewis
OK.
Let's say right now the core file is: /images/productphotos/1234_fred.jpg.
And the old thumbnail is: /images/productphotos/small/1234_fred.jpg.
Yet the new ones written on the fly or on upload are for example: /images/productphotos/small/1234_fred_475.jpg.
I Want the "old thumbnail" to be deleted when the new ones are made. So as it starts to compile the new ones, the old ones are removed.
Re: Image Resizing Script required - a better one...
Posted: Tue Mar 14, 2017 10:23 am
by simonmlewis
I wonder if it can be a small function, that is run from within here:
function resizeSingleImage($original, $suffix, $width, $save_path)
This resizes one image. So as it resizes that one image, it can delete the original image (without the suffix), from the productphotos/images folder. But obviously, only if the $save_path is "thumbnails".
(trying to think 'function' here now.... )...
Re: Image Resizing Script required - a better one...
Posted: Tue Mar 14, 2017 10:26 am
by Celauran
OK. I see what you're saying now. Some old thumbnails not following the current naming scheme may already exist and you would like those to be removed. Gotcha. I think you're on the right track with your approach to this. Let me know how it turns out of if you get stuck.
Re: Image Resizing Script required - a better one...
Posted: Tue Mar 14, 2017 10:29 am
by simonmlewis
It might even be better if the function did not run on the resize, but ran 'anyway'. So that if the resizing is done, but for some reason the old ones remains, the new function would delete the old ones. For example, on our demo server. I now have loads of redundant files in /small, that can be removed. A nice function to locate those, if there is a _475 version there, and delete, would be ideal.
I know it means I MUST ensure all thumbnail pages have this new tool though. So perhaps it's an addon function to be run, after a while.
Thing is, while i can see some of this save image code, I don't know functions for deleting.