Image Resizing Script required - a better one...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

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...

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post 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;
}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post by Celauran »

Alternately, without the else:

Code: Select all

function getPath($target)
{
    if ($target == "thumbnails") {
        return "/thisone/";
    }

    return "differentone";
}
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...

Post 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;
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post by Celauran »

That could work, but it could also be problematic. How are you planning on using this?
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...

Post by simonmlewis »

I guess the $paths would be an array, and would need to be 'opened' if the upload is a thumbnail.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post 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?
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...

Post 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..?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post by Celauran »

But it does go somewhere. The thumbnails get saved somewhere, don't they?
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...

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post 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;
}
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...

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post 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.
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...

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post 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...
}
Post Reply