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

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 »

So you're uploading an image, which goes in one directory, and then creating multiple resized images which go in another directory? And all the resized images are in the same directory?
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 »

Correct. So if you had shirt.jpg, and it was renamed on upload to 12345_shirt.jpg, that goes into /images/productphotos.
While 12345_shirt_475.jpg (and the other three) will go into /images/productphotos/small.

If I were to write this from scratch I'd put them in one, but I didn't. And won't. lol.
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 »

So what's the criteria that determines whether they go in images/productphotos or images/pages?
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 »

The $resize would be "thumbnails". Rather than Categories, Products or Wide.
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 »

So resizeImage and getSrcSet are going to need to be changed since they both currently rely on hardcoded paths. However, we've got a common parameter being passed to both, so we can pass that parameter along to some getTargetDirectory function we've yet to create. That function could return an array containing the directory in which to store the original image as well as the directory in which to store the thumbnails. Starting to see how this would work?
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 »

Sure. the both use actual_paths. So we need to tap into that, and feed it with getTargetDirectory, possibly with a 'default' one for the banners (images/pages).
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 »

I think that's a reasonable approach. First, notice that you've got the logic for $actualfolder in two different places. I'd start by extracting that to a function, then move on to having it return two directories instead of one. Take a crack at it and then we can look at fine tuning it together.
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 »

Blimey I'm lost now.
If I take the Resize_type function within resizeimage as a template, that takes the 'case', and creates a variable.

But how I create then two variables, and use both within the upload script, based on the type of image I am upload, I'm lost.

I did figure out how to add one into that section, but not how to then store into TWO locations via a function. I don't want the answer, I want a steering... firstly with the right sort of function to create. I can see now the 'return' method. So I may be able to figure that bit out.
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 »

Show me what your functions file currently looks like and what you've added, and I'll give you a little nudge.
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 getTargetDirectory($targetdirectory)
{  
  switch($targetdirectory) {
      case 'thumbnails':
      $folder_thumbnails= 'productphotos/small';
      $folder_original= 'productphotos/';
      break;
     }
I really don't know where to go from here. I know I need to 'return' something, so it knows the default location, or the one(s) I have set.
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 »

You do need to return something, and functions can only return one thing. But you need to return two values, and you need to know which is which. A single element containing key/value pairs....

Also, don't forget that you're going to be calling this function regardless of the value of $resize, so you need to be able to return something for all cases.
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 »

Am I right in any of this... or is not where you'd start at all?
ie. do I return the two values, assuming I must, since I have two file paths.
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 »

Code: Select all

$paths = array(
    'fullsize' => '/path/to/full/images',
    'thumbnails' => '/path/to/thumbnails',
);
Functions can only return one thing. $paths is one thing.
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 »

Ok. So before I return $paths(), do I need to add in a default there for the images/pages?
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 »

I would recommend it, yes. If you don't, what happens when $targetdirectory is not 'thumbnails'?
Post Reply