Image Resizing Script required - a better one...
Moderator: General Moderators
Re: Image Resizing Script required - a better one...
Why not use file_exists to check if the file exists before trying to open it?
Re: Image Resizing Script required - a better one...
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?
-
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...
That's what I am doing. Isn't it?
Maybe I am running before stepping here though.
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.
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";
}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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Image Resizing Script required - a better one...
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.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.
Re: Image Resizing Script required - a better one...
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?simonmlewis wrote:That's what I am doing. Isn't it?
Maybe I am running before stepping here though.
This is correctly showing "no" for a load of images.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"; }
-
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...
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?
How is that wrong?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Image Resizing Script required - a better one...
You're not passing the absolute path. Remember we had a $target_directory variable when resizing?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?
-
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...
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 ?
That's done in place of $file ?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Image Resizing Script required - a better one...
$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.
-
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...
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'/>Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Image Resizing Script required - a better one...
http://php.net/manual/en/function.pathinfo.php
PATHINFO_FILENAME is going to give you just the file name, not the full path.
PATHINFO_FILENAME is going to give you just the file name, not the full path.
-
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...
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.
I'm not interested in whether the original image is there.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Image Resizing Script required - a better one...
Sure, but you still want to pass it an absolute path.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.
No? I thought you wanted to resize on the fly...simonmlewis wrote:I'm not interested in whether the original image is there.
-
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...
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' ??
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' ??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Image Resizing Script required - a better one...
Determine the base path (ie. up to /images) and store that as a variable. Makes it easy to prepend where needed.