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 »

simonmlewis wrote:It might even be better if the function did not run on the resize, but ran 'anyway'. So that if the resizing is done, but for some reason the old ones remains, the new function would delete the old ones. For example, on our demo server. I now have loads of redundant files in /small, that can be removed. A nice function to locate those, if there is a _475 version there, and delete, would be ideal.

I know it means I MUST ensure all thumbnail pages have this new tool though. So perhaps it's an addon function to be run, after a while.
Doesn't have to be either/or. Can be both. Write a function that will check for the existence of a given file name and delete it if found. When you're resizing, you can pass through a file name to get rid of the old file. You could also write a script that would grab the names of all files in productphotos, iterate over them, and pass each one through to the same function, deleting old thumbnails if found.
simonmlewis wrote:Thing is, while i can see some of this save image code, I don't know functions for deleting.
http://php.net/manual/en/function.unlink.php
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 »

Code: Select all

// Finds old Small images, and deletes
function deleteOldSmallThumbnail($original, $suffix, $save_path)
{
// not certain if I need imagine, but since I am getting hold of a file, I assume I do
    $imagine = new Imagine\Gd\Imagine();
// path taken from the getPaths function, via "save_path"
    $paths = getPaths($save_path);
    // where is the file located
    $target_directory = dirname(__DIR__) . $paths['thumbnail'];
    $pathinfo = pathinfo($original);

    // Open the uploaded image with the Imagine library
    $image = $imagine->open($target_directory . DIRECTORY_SEPARATOR . $original);

//... now, how do I DELETE it... I don't need glob as there is no pattern.  Just want to delete it maybe with unlink??

}
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 »

Is it this??

Code: Select all

// Finds old Small images, and deletes
function deleteOldSmallThumbnail($original, $suffix, $save_path)
{
    $imagine = new Imagine\Gd\Imagine();

    $paths = getPaths($save_path);
    // where is the file located
    $target_directory = dirname(__DIR__) . $paths['thumbnail'];
    $pathinfo = pathinfo($original);

    // Open the uploaded image with the Imagine library
    $image = $imagine->open($target_directory . DIRECTORY_SEPARATOR . $original);
    unlink($image);
}
It only want to do it if it finds an image though, otherwise I may get a ton of errors.
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 »

Imagine is a library to manipulate images, so you really don't need to worry about it here. What you're trying to do, if I'm understanding correctly, is this: given a file name, check if a file with the same name exists in the /small/ subdirectory. If it does, delete it. If that's the case, you need the path to the directory and the name of the file. Nothing else.
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 »

So here I am telling it to use thumbnail locations via getPaths.
I'm finding the "target_directory".
I'm checking to see if it exists in that director.
If it does, I'm opening the file, and then deleting it.

Do I need to open it and then delete? I assume I need to use something to ascertain the entire location of what I want to delete.

Code: Select all

// Finds old Small images, and deletes
function deleteOldSmallThumbnail($original, $suffix, $save_path)
{
    $paths = getPaths($save_path);
    // where is the file located
    $target_directory = dirname(__DIR__) . $paths['thumbnail'];
    $pathinfo = pathinfo($original);
    
    if (file_exists($target_directory . DIRECTORY_SEPARATOR . $filename))
    {
    // Open the uploaded image with the Imagine library
    $image = $imagine->open($target_directory . DIRECTORY_SEPARATOR . $original);
    unlink($image);
    }
}
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 »

One section, $filename is wrong. Trying to think what that should be. $original?
I think I am close tho.

Also not sure that I need $suffix in there.
Last edited by simonmlewis on Tue Mar 14, 2017 10:42 am, edited 1 time in total.
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 Imagine here at all. Also, try to give the function the information it needs rather than having the function try to figure it out on its own.
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 »

Code: Select all

// Finds old Small images, and deletes
function deleteOldSmallThumbnail($original, $save_path)
{
    $paths = getPaths($save_path);
    // where is the file located
    $target_directory = dirname(__DIR__) . $paths['thumbnail'];
    $filename = $target_directory . DIRECTORY_SEPARATOR . $original;
    
    if (file_exists($filename))
    {
    // Open the uploaded image with the Imagine library
    unlink($filename);
    }
}
Ok I've got this now.
So it's told the path of the file from getPaths, being told 'thumbnails' in the code.
I tell it the target_directory with the paths 'thumbnails' version.
Add that into $filename to get the full path to the file.
Then check with that variable if it exists. If it does, "unlink" it.

Am I right? OR just close.. or way off? lol
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 »

ps I know I also then need to make this function "run". Not sure why how I trigger it. But one thing at a time.
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:

Code: Select all

// Finds old Small images, and deletes
function deleteOldSmallThumbnail($original, $save_path)
{
    $paths = getPaths($save_path);
    // where is the file located
    $target_directory = dirname(__DIR__) . $paths['thumbnail'];
    $filename = $target_directory . DIRECTORY_SEPARATOR . $original;
    
    if (file_exists($filename))
    {
    // Open the uploaded image with the Imagine library
    unlink($filename);
    }
}
Ok I've got this now.
So it's told the path of the file from getPaths, being told 'thumbnails' in the code.
I tell it the target_directory with the paths 'thumbnails' version.
Add that into $filename to get the full path to the file.
Then check with that variable if it exists. If it does, "unlink" it.

Am I right? OR just close.. or way off? lol
I expect this will work just fine. However,
simonmlewis wrote:ps I know I also then need to make this function "run". Not sure why how I trigger it. But one thing at a time.
If you're going to run it when you're resizing an image, then you've already got the name of the file and the directory path. You could actually just call unlink in 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...

Post by simonmlewis »

... I could... but what if the resizing is already done. Just say that the resizing is done, and we want to "double check" that there are not loose ends for those products.

1) would I pass $original, $directory_path into the new function from the resize function
2) how would this run if no resizing is taking place? ie. it's just clearing up loose ends.
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 »

Am I making too much of out this?

Code: Select all

// Resizes a single image to a specific size and with a specific suffix
function resizeSingleImage($original, $suffix, $width, $save_path)
{
    $imagine = new Imagine\Gd\Imagine();

    $paths = getPaths($save_path);
    // get the source file ready for renaming
    $source_directory = dirname(__DIR__) . $paths['fullsize'];
    // where is the file going to be saved?
    $target_directory = dirname(__DIR__) . $paths['thumbnail'];
    $pathinfo = pathinfo($original);

    // Open the uploaded image with the Imagine library
    $image = $imagine->open($source_directory . DIRECTORY_SEPARATOR . $original);

    // Get the size of the original image
    $box = $image->getSize();

    $ratio = $width / $box->getWidth();
    $scaled_box = $box->scale($ratio);
    $new_filename = "{$pathinfo['filename']}_{$suffix}.{$pathinfo['extension']}";
    $original_filename = "{pathinfo['filename']}.{$pathinfo['extension']}";

    $options = getImageOptions($pathinfo['extension']);
    $image->resize($scaled_box)->save($target_directory . DIRECTORY_SEPARATOR . $new_filename, $options);
}



// Finds old Small images, and deletes
function deleteOldSmallThumbnail($original_filename, $save_path)
{
    $paths = getPaths($save_path);
    // where is the file located
    $target_directory = dirname(__DIR__) . $paths['thumbnail'];
    $filefordeletion = resizeSingleImage($original_filename);
    $target_file = $target_directory . DIRECTORY_SEPARATOR . $filefordeletion;
    
    if (file_exists($target_file))
    {
    // Open the uploaded image with the Imagine library
    unlink($target_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 »

simonmlewis wrote:... I could... but what if the resizing is already done. Just say that the resizing is done, and we want to "double check" that there are not loose ends for those products.

2) how would this run if no resizing is taking place? ie. it's just clearing up loose ends.
Two options. You could have the delete check in your getSrcSet call and/or you could have a dedicated script that runs off cron.
simonmlewis wrote:.1) would I pass $original, $directory_path into the new function from the resize function
Your function is basically just wrapping file_exists and unlink. You could pass it the full path to check. You don't even necessarily need a separate function here given how little it's doing.
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 »

So I can add something in getSrcSet to see if it is doing the whole process to create the new images.
If it is, then what? make it run the deletion from in there? Considering it has all the filename and directory there.

But if it is NOT using that, because the newly _475... images have been made, I could put it where..?
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 »

It couldn't run off cron, as I need it to delete them ONLY if the new ones are in place.
Best to do that in an "on the fly" scenario. (or if I do a small cron that done the on the fly at night).

But whatever I do, I need to add something in here.
I suppose I can get the information from resizeSingleImage, but that only runs if I need to resize, doesn't it?
So having unlink in there might not be right. Or might be duplication if it is needed elsewhere (if the resized ones are already done).
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply