Page 11 of 37

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

Posted: Wed Mar 08, 2017 11:24 am
by Celauran
Why not use file_exists to check if the file exists before trying to open it?

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

Posted: Wed Mar 08, 2017 11:28 am
by Celauran
Something else occurs to me. You're using the same suffix for different sizes depending on the value of $resize when you're first resizing them. How will you be able to tell a 950px wide '-1920' image from a 451px wide one?

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

Posted: Wed Mar 08, 2017 11:31 am
by simonmlewis
That's what I am doing. Isn't it?
Maybe I am running before stepping here though.

Code: Select all

$file = "/images/pages/$row->image";
$ext = pathinfo($file, PATHINFO_EXTENSION);
$filename = pathinfo($file, PATHINFO_FILENAME);

// is the smaller file missing?
$filecheck = $filename . "_475" . $ext;
if (!file_exists($filecheck))
{
echo "no";
}
This is correctly showing "no" for a load of images. Mainly because I started doing this with a -475- on the end, rather than the -475.
But it does show the file_exists... works.
Secondly, for each of these sections, if _475 doesn't exist, none of those resized ones will. And I will add the $widths code in there, specific to that section manually. Unless I ought to be putting Categories, Products, Wide etc, into a separate function (don't know how) to use on the resize admin tool, and this tool as well.

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

Posted: Wed Mar 08, 2017 11:33 am
by Celauran
simonmlewis wrote:And I will add the $widths code in there, specific to that section manually. Unless I ought to be putting Categories, Products, Wide etc, into a separate function (don't know how) to use on the resize admin tool, and this tool as well.
A good rule of thumb is if you find yourself repeating the same code, you should extract it to a function and call the function wherever you need to.

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

Posted: Wed Mar 08, 2017 11:36 am
by Celauran
simonmlewis wrote:That's what I am doing. Isn't it?
Maybe I am running before stepping here though.

Code: Select all

$file = "/images/pages/$row->image";
$ext = pathinfo($file, PATHINFO_EXTENSION);
$filename = pathinfo($file, PATHINFO_FILENAME);

// is the smaller file missing?
$filecheck = $filename . "_475" . $ext;
if (!file_exists($filecheck))
{
echo "no";
}
This is correctly showing "no" for a load of images.
Right, but that's checking if the resized file exists. You're then trying to open the original with $row->image. Does that store the full path or just the file name?

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

Posted: Wed Mar 08, 2017 11:42 am
by simonmlewis
Yes I am trying to open that file (like we do on the resize script before), to then create the three new ones.
How is that wrong?

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

Posted: Wed Mar 08, 2017 11:42 am
by Celauran
simonmlewis wrote:Yes I am trying to open that file (like we do on the resize script before), to then create the three new ones.
How is that wrong?
You're not passing the absolute path. Remember we had a $target_directory variable when resizing?

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

Posted: Wed Mar 08, 2017 11:47 am
by simonmlewis
Ok. So I need to use that instead, to get the exact placement of it (yes I see why, to get the file).
That's done in place of $file ?

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

Posted: Wed Mar 08, 2017 11:49 am
by Celauran
$file is fine. It contains the relative path. You just need to prepend the full path to the /images/pages/ directory. Again, like we did when resizing the image initially. dirname() and __DIR__ are going to be handy here.

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

Posted: Wed Mar 08, 2017 11:52 am
by simonmlewis
I Think this is what I actually used:

Code: Select all

$file = $_SERVER['DOCUMENT_ROOT']."/images/pages/$row->image";
$ext = pathinfo($file, PATHINFO_EXTENSION);
$filename = pathinfo($file, PATHINFO_FILENAME);

// is the smaller file missing?
$filecheck = $filename . "_475" . $ext;
if (!file_exists($filecheck))
{
echo "no";
}

echo "<img srcset='/images/pages/$filename" . "_475" . ".$ext" . " 475w, 
/images/pages/$filename" . "_768" . ".$ext" . " 768w, 
/images/pages/$filename" . "_1920" . ".$ext" . " 1920w'
 sizes='(max-width: 475px) 475px,
            (max-width: 768px) 768px,
            (max-width: 1920px) 1920px'
src='/images/pages/$row->image' alt='$row->freetext'/>

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

Posted: Wed Mar 08, 2017 11:56 am
by Celauran
http://php.net/manual/en/function.pathinfo.php

PATHINFO_FILENAME is going to give you just the file name, not the full path.

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

Posted: Wed Mar 08, 2017 11:57 am
by simonmlewis
Sure - perfect though. As I want to find out if {filename}_475.jpg exists. The only way I know to do that, is to extract the filename from the path, add a _475 on the end, and check to see if it is there.

I'm not interested in whether the original image is there.

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

Posted: Wed Mar 08, 2017 12:00 pm
by Celauran
simonmlewis wrote:Sure - perfect though. As I want to find out if {filename}_475.jpg exists. The only way I know to do that, is to extract the filename from the path, add a _475 on the end, and check to see if it is there.
Sure, but you still want to pass it an absolute path.
simonmlewis wrote:I'm not interested in whether the original image is there.
No? I thought you wanted to resize on the fly...

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

Posted: Wed Mar 08, 2017 12:01 pm
by simonmlewis
Oh I see what you are saying.
So first I want to find out if _475 exists, by passing an absolute path to it.
Then if it doesn't, pass an absolute path to the original to get it, to do the resize.
So do I need to run two 'paths' ??

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

Posted: Wed Mar 08, 2017 12:03 pm
by Celauran
Determine the base path (ie. up to /images) and store that as a variable. Makes it easy to prepend where needed.