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 »

Mmmm but you can see what I am trying to do. As I said a bit lost.

Code: Select all

if ($resize_type == "thumbnails" || $resize_type == "stockbanner")
{
  if (file_exists($thumbnail_directory . DIRECTORY_SEPARATOR . $fullname)) 
  {
    $srcset[] = "{$images_path}/{$fullname} {$size}w";
  } 
  else 
  {
    resizeSingleImage($filename, $size, $width, $resize_type);
    $srcset[] = "{$images_path}/{$fullname} {$size}w";
  }
}
else
  {
    if (file_exists($thumbnail_directory . DIRECTORY_SEPARATOR . $fullname)) 
  {
    $srcset[] = "{$images_path}/{$filename}";
  } 
  else 
  {
    resizeSingleImage($filename, $size, $width, $resize_type);
    $srcset[] = "{$images_path}/{$filename}";
  }
  }
Because it looked right. I guess I am very wrong!
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 »

Let's walk through the getSrcSet function adding in some comments that describe the functionality for each segment of code. Should help clarify what's happening when and give you an idea of where you want to be adding in this new logic.
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 »

But yes, I can see I am using resizeSingleImage in there... definitely wrong. I don't want want to play with the image. I just want to push it out as is, for 2560.
Love PHP. Love CSS. Love learning new tricks too.
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...

Post by simonmlewis »

Does it somehow go in here, NOT within the resize?

Code: Select all

if (file_exists($thumbnail_directory . DIRECTORY_SEPARATOR . $fullname)) {
// creates each one if the file exists
$srcset[] = "{$images_path}/{$fullname} {$size}w";
} 
Just not sure how to do this in a loop, and state that for the 2560, it uses $filename and not $fullname.
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 don't need to worry about doing it in a loop. In fact, you don't want to. I suggested in a previous post where you could put it: viewtopic.php?f=1&t=143432&start=495#p707897
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 »

Getting there?

Code: Select all

if (file_exists($thumbnail_directory . DIRECTORY_SEPARATOR . $fullname)) {
// creates each one if the file exists
if (($resize_type == "categories" || $resize_type == "products" || $resize_type == "wide") && $size == "2560")
  {
    $srcset[] = "{$images_path}/{$filename} {$size}w";
  }
  else
  {
  $srcset[] = "{$images_path}/{$fullname} {$size}w";
  }
} 
else 
{
resizeSingleImage($filename, $size, $width, $resize_type);
$srcset[] = "{$images_path}/{$fullname} {$size}w";
}
}
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're still trying to do this inside that loop. Why? This is separate from trying to get the resized images.
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 »

Because it has to be in a loop as that is where it outputs all the srcset code. Isn't it?
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:Because it has to be in a loop as that is where it outputs all the srcset code. Isn't it?
Not really, no. The srcset array is generated in the getSrcSet function. We add the smaller sizes to it inside that loop because we have an array of resized images to iterate over. That's not what we're looking to do here, though. We have the file name for the original image and we just want to add that (and its width) to the srcset array before returning it. Look at the code I commented. That's where it should go. Step through the function and see what it's doing at each phase. You should understand why it belongs there. Where you're not clear, ask.
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 »

In normal language, I can see the srcset[] is set above. So I just want to add onto that srcset[] if the image is a category, products or wide. And then add onto it:

the srcset[] array, and add on the $filename and $size w.

Is that what I am doing here?
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:In normal language, I can see the srcset[] is set above. So I just want to add onto that srcset[] if the image is a category, products or wide. And then add onto it:

the srcset[] array, and add on the $filename and $size w.
Right.
simonmlewis wrote:Is that what I am doing here?
No. What you are doing is best described thus:
Here's a file name and a resize type. Check if the file actually exists. If it does, get me a list of which smaller sizes should exist. For each size in that list, check if an image at that size exists. If it doesn't exist, create it. If it exists, or has just been created, add it to the srcset array. Also for each size in that list, add the full sized image to the srcset array (here's the problem: you're adding it multiple times by having it in that loop). Once you're done all that, split the array into a long string and return it to the caller.
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 »

Then there's also the bit about $size == 2560. Not sure what that's about. Are you creating a new resized image at that size?
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 »

No. I'm asking it.... no wait. I don't need that in there. If it is one of those three, I'm just adding this line onto the array. I'm not asking what size it is, I'm just saying "add this line as well, for those resize_types, with this number on the end for 2560.

Right?
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 sounds closer to what you want, yes. Do you see how to implement that in code?
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 See. So outside of that loop, I then add this into the srcset, if the resize_type is one of the three.

Code: Select all

$basename = pathinfo($filename, PATHINFO_FILENAME);
$extension = pathinfo($filename, PATHINFO_EXTENSION);

foreach ($widths as $size => $width) {
$fullname = $basename . '_' . $size . '.' . $extension;
if (file_exists($thumbnail_directory . DIRECTORY_SEPARATOR . $fullname)) {
$srcset[] = "{$images_path}/{$fullname} {$size}w";
} else {
resizeSingleImage($filename, $size, $width, $resize_type);
$srcset[] = "{$images_path}/{$fullname} {$size}w";
}
}

if ($resize_type == "categories" || $resize_type == "products" || $resize_type == "wide")
  {
    $srcset[] = "{$images_path}/{$filename} {$size}w";
  }
}

return implode(', ', $srcset);
}
But how do I tell it this is for 2560 ?
Do I add that in at the very top in the "case" section?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply