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 »

Why not use file_exists to check if the file exists before trying to open it?
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 »

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...

Post 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.
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: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.
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: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?
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 »

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?
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: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?
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 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 ?
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 »

$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...

Post 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'/>
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 »

http://php.net/manual/en/function.pathinfo.php

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...

Post 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.
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: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...
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 »

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' ??
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 »

Determine the base path (ie. up to /images) and store that as a variable. Makes it easy to prepend where needed.
Post Reply