Page 27 of 37
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 1:23 pm
by simonmlewis
So how do I set a default?
I guess this function is pretty small. Just a way of saying if this is "thumbnails" use these directories... else... use this one.
I know how to do it in PHP normally:
if ($target == "thumbnails")
{
$path == "/thisone/";
}
else
{
$path == "differentone";
}
But not in a function I don't know.
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 1:26 pm
by Celauran
simonmlewis wrote:So how do I set a default?
I guess this function is pretty small. Just a way of saying if this is "thumbnails" use these directories... else... use this one.
I know how to do it in PHP normally:
if ($target == "thumbnails")
{
$path == "/thisone/";
}
else
{
$path == "differentone";
}
But not in a function I don't know.
It's nearly the same.
Code: Select all
function getPath($target)
{
if ($target == "thumbnails") {
$path = "/thisone/";
} else {
$path = "differentone";
}
return $path;
}
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 1:28 pm
by Celauran
Alternately, without the else:
Code: Select all
function getPath($target)
{
if ($target == "thumbnails") {
return "/thisone/";
}
return "differentone";
}
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 1:29 pm
by simonmlewis
Code: Select all
function getPath($target)
{
if ($target == "thumbnails")
{
$paths = array(
'fullsize' => '/images/productphotos',
'thumbnails' => '/images/productphotos/small',
);
}
else
{
$paths = '/images/pages';
}
return $path;
}
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 1:30 pm
by Celauran
That could work, but it could also be problematic. How are you planning on using this?
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 1:32 pm
by simonmlewis
I guess the $paths would be an array, and would need to be 'opened' if the upload is a thumbnail.
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 1:36 pm
by Celauran
simonmlewis wrote:I guess the $paths would be an array, and would need to be 'opened' if the upload is a thumbnail.
Could you not return a full path and a thumbnails path in all instances?
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 1:37 pm
by simonmlewis
Sorry..? /images/pages doesn't have a thumbnails path.
Or do you mean to do it, even if the pages/ thumbnails path goes nowhere..?
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 1:41 pm
by Celauran
But it does go somewhere. The thumbnails get saved somewhere, don't they?
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 1:43 pm
by simonmlewis
I said this earlier: product photos...:
originals go in "images/productphotos"
Resized ones go in "images/productphotos/small".
Banners like on the homepage, ALL go in /images/pages. No additional "thumbnails" for those. they are resized as you know, but they all go to the same place.
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 1:47 pm
by Celauran
simonmlewis wrote:I said this earlier: product photos...:
originals go in "images/productphotos"
Resized ones go in "images/productphotos/small".
Banners like on the homepage, ALL go in /images/pages. No additional "thumbnails" for those. they are resized as you know, but they all go to the same place.
Ah, but who said anything about additional?
Code: Select all
function getPath($target)
{
$paths = array(
'fullsize' => '/images/pages',
'thumbnail' => '/images/pages',
);
if ($target === 'thumbnails') {
$paths['fullsize'] = '/images/productphotos';
$paths['thumbnail'] = '/images/productphotos/small';
}
return $paths;
}
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 1:49 pm
by simonmlewis
Yes that is what I was hinting at myself! $thumbnail to be erroneous almost, but I suppose since it is in an array, it forces them all the to same place.
So you have there the default for $paths. And if the target is thumbnails, it set the arrange for paths.
Ok got that.
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 1:52 pm
by Celauran
simonmlewis wrote:Yes that is what I was hinting at myself! $thumbnail to be erroneous almost, but I suppose since it is in an array, it forces them all the to same place.
So you have there the default for $paths. And if the target is thumbnails, it set the arrange for paths.
Ok got that.
The advantage to this approach is you don't need to check later if it's an array or just a single path. You know you're getting an array back so you can set up functions that rely on this accordingly. It also gives you the flexibility to make changes down the road in only a single spot.
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 1:54 pm
by simonmlewis
Gotcha. I was trying to lead toward that, but not in the right wy, as my version didn't have the second bit in an array.
So now it knows it's "thumbnails". How am I telling it that? Should the 'resize' not be in the function, to relay it from the form?
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 1:58 pm
by Celauran
simonmlewis wrote:So now it knows it's "thumbnails". How am I telling it that? Should the 'resize' not be in the function, to relay it from the form?
You're passing $resize into your resizeImage function, and you're calling this function from inside resizeImage. Just pass the parameter along.
Code: Select all
// Takes input from a form post and saves original image plus all required resizes
function resizeImage($path_to_file, $file_name, $resize_type)
{
$imagine = new Imagine\Gd\Imagine();
$target_directory = dirname(__DIR__) . getPath($resize_type);
$pathinfo = pathinfo($file_name);
// etc...
}