Page 35 of 37

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

Posted: Wed Mar 15, 2017 7:36 am
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!

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

Posted: Wed Mar 15, 2017 7:38 am
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.

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

Posted: Wed Mar 15, 2017 7:39 am
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.

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

Posted: Wed Mar 15, 2017 7:54 am
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.

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

Posted: Wed Mar 15, 2017 7:56 am
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

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

Posted: Wed Mar 15, 2017 7:57 am
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";
}
}

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

Posted: Wed Mar 15, 2017 8:09 am
by Celauran
You're still trying to do this inside that loop. Why? This is separate from trying to get the resized images.

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

Posted: Wed Mar 15, 2017 8:11 am
by simonmlewis
Because it has to be in a loop as that is where it outputs all the srcset code. Isn't it?

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

Posted: Wed Mar 15, 2017 8:14 am
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.

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

Posted: Wed Mar 15, 2017 8:18 am
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?

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

Posted: Wed Mar 15, 2017 8:25 am
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.

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

Posted: Wed Mar 15, 2017 8:27 am
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?

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

Posted: Wed Mar 15, 2017 8:31 am
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?

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

Posted: Wed Mar 15, 2017 8:32 am
by Celauran
That sounds closer to what you want, yes. Do you see how to implement that in code?

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

Posted: Wed Mar 15, 2017 8:33 am
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?