CodeIgniter Forums
resize and unlink file question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: resize and unlink file question (/showthread.php?tid=64491)



resize and unlink file question - wolfgang1983 - 02-25-2016

On my resize function. I am trying to unlink with my width and height size add on to the new file.

When it creates a new file it stores it in my images > cache folder and then addes width and height on to it.

example_100x50.jpg but the original file would be example.jpg the original files are located in images > catalog

If the example.jpg file is not there in folder then I need to be able to unlink that image but because it has the width and height added on to it I do not know how to make sure it unlinks correct one.

Question how could I unlink the correct cache image even though it has width and height different to the original image


PHP Code:
public function resize($filename$width$height) { 
    $this->load->library('image_lib');

    $old_file FCPATH 'images/' $filename;

    $extension pathinfo($filenamePATHINFO_EXTENSION);

    $extended_image substr($filename0strrpos($filename'.')) . '_' $width 'x' $height '.' $extension;
        
    $new_file 
FCPATH 'images/cache/' $extended_image;

    // Checks if file does not exist in images folder but exist in cache will remove it.

    $check_if_file_is_set array_reverse(glob(FCPATH  'images/catalog/*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}'GLOB_BRACE));

    for ($i=0$i count($check_if_file_is_set); $i++) { 
        if ($i sizeof($check_if_file_is_set)) {
                
        $file 
str_replace(FCPATH 'images/'''$check_if_file_is_set[$i]);

            if (!file_exists(FCPATH 'images/' $file)) {
                // unlink(FCPATH . 'images/cache/' . $some_variable);
            }
        }
    }

    if (!is_file($new_file) || (filectime($old_file) > filectime($new_file))) {

        if (!is_dir($old_file)) {
            @mkdir(FCPATH 'images/cache/'dirname($filename), 0777);
        }
            
        $config
['image_library'] = 'gd2';
        $config['source_image'] = $old_file;
        $config['maintain_ratio'] = FALSE;
        $config['width'] = $width;
        $config['height'] = $height;
        $config['new_image'] = $new_file;

        $this->image_lib->clear();
        $this->image_lib->initialize($config);
        $this->image_lib->resize();

    }

    $ouput_image '';

    return base_url('images/cache/' $extended_image);




RE: resize and unlink file question - Diederik - 02-25-2016

You could use something like this below. First you verify if the filename has the width and hieght in its name and then check if a file exists without those values. If not you can delete the file. You could also write some regular expression for this job.

But you need to be aware that you cannot accept uploads of a file that already uses that same filenaming format. For example if you upload a file called "some-background-image_1920x1020.jpg" then the generated thumbnail would be "some-background-image_1920x1020_100x50.jpg" (or perhaps "some-background-image_1920x1020_100x53.jpg" if you preserve the aspect ratio). If you loop trough all your files in the directory then "some-background-image_1920x1020.jpg" would be removed because the file "some-background-image.jpg" does not exists.
You need to extend it somewhat to check this, or rename such a file during upload or place your thumbnails in a separate folder.




PHP Code:
$file 'example_100x50.jpg';

$position_dot        strrpos($file'.');
$position_underscore strrpos($file'_');

if (
$position_dot && $position_underscore && $position_dot $position_underscore) {

 
   $sizeString substr($file$position_underscore 1, ($position_dot $position_underscore) -);
 
   // $sizeString should contain something like 100x50
 
   
    $sizesArray 
explode('x'$sizeString);

 
   if (count($sizesArray) == && is_numeric($sizesArray[0]) && is_numeric($sizesArray[1])) {

 
       // The filename is a generated thumbnail

 
       // Remove the sizes string and underscore from the filename
 
       $originalFile str_replace('_' $sizeString''$file);

 
       if (!is_file(FCPATH 'images/' $originalFile)) {

 
           // Remove this thumbnail because the original file does not exists anymore.
 
           if (@unlink(FCPATH 'images/' $file)) {

 
               // File was removed
 
               
            
} else {

 
               // Some error handeling, unlink was unsuccessfull
 
           
            
}
 
       }

 
   }




RE: resize and unlink file question - wolfgang1983 - 02-25-2016

(02-25-2016, 03:13 AM)Diederik Wrote: You could use something like this below. First you verify if the filename has the width and hieght in its name and then check if a file exists without those values. If not you can delete the file. You could also write some regular expression for this job.

But you need to be aware that you cannot accept uploads of a file that already uses that same filenaming format. For example if you upload a file called "some-background-image_1920x1020.jpg" then the generated thumbnail would be "some-background-image_1920x1020_100x50.jpg" (or perhaps "some-background-image_1920x1020_100x53.jpg" if you preserve the aspect ratio). If you loop trough all your files in the directory then "some-background-image_1920x1020.jpg" would be removed because the file "some-background-image.jpg" does not exists.
You need to extend it somewhat to check this, or rename such a file during upload or place your thumbnails in a separate folder.




PHP Code:
$file 'example_100x50.jpg';

$position_dot        strrpos($file'.');
$position_underscore strrpos($file'_');

if (
$position_dot && $position_underscore && $position_dot $position_underscore) {

 
   $sizeString substr($file$position_underscore 1, ($position_dot $position_underscore) -);
 
   // $sizeString should contain something like 100x50
 
   
    $sizesArray 
explode('x'$sizeString);

 
   if (count($sizesArray) == && is_numeric($sizesArray[0]) && is_numeric($sizesArray[1])) {

 
       // The filename is a generated thumbnail

 
       // Remove the sizes string and underscore from the filename
 
       $originalFile str_replace('_' $sizeString''$file);

 
       if (!is_file(FCPATH 'images/' $originalFile)) {

 
           // Remove this thumbnail because the original file does not exists anymore.
 
           if (@unlink(FCPATH 'images/' $file)) {

 
               // File was removed
 
               
            
} else {

 
               // Some error handeling, unlink was unsuccessfull
 
           
            
}
 
       }

 
   }


Thank you for your help. I have now got it working so far with

PHP Code:
if (null !==($this->input->get('directory'))) {
            
$cached_images array_reverse(glob(FCPATH  'images/cache/' $this->input->get('directory'). '/*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}'GLOB_BRACE));
                    } else {
            
$cached_images array_reverse(glob(FCPATH  'images/cache/catalog/*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}'GLOB_BRACE));
        }

        foreach (
$cached_images as $cached_image) {

            
$replcement str_replace(FCPATH 'images/cache/'''$cached_image);

            
$cache_image_extension pathinfo($replcementPATHINFO_EXTENSION);

            
$originalFile str_replace('_thumb-' $width 'x' $height''$replcement);

            if (!
is_file(FCPATH 'images/' $originalFile)) {

                if (@
unlink(FCPATH 'images/cache/' $replcement)) {

                
                } else {


                }
            }

        } 



RE: resize and unlink file question - wolfgang1983 - 02-27-2016

Hello thanks again for your advice I found that this code below works more better using RecursiveDirectoryIterator and RecursiveIteratorIterator all new to me


PHP Code:
public function resize($filename$width$height) { 
        
    $this
->load->library('image_lib');

    $old_file FCPATH 'images/' $filename;

    $extension pathinfo($filenamePATHINFO_EXTENSION);

    $extended_image substr($filename0strrpos($filename'.')) . '_thumb-' $width 'x' $height '.' $extension;
        
    $new_file 
FCPATH 'images/cache/' $extended_image;

    $this->load->helper('directory');

    $get_directory $this->input->get('image_directory');

    $path FCPATH 'images/cache/catalog/';

    $dir = new RecursiveDirectoryIterator(FCPATH 'images/cache/'FilesystemIterator::SKIP_DOTS);
        
    
// Flatten the recursive iterator, folders come before their files
    $cached_images  = new RecursiveIteratorIterator($dir,
            RecursiveIteratorIterator::SELF_FIRST);
    
    foreach 
($cached_images as $image => $object) { 

        $original_image_file str_replace('_thumb-' $width 'x' $height''$image);
            $original_image_short_path str_replace(FCPATH 'images/cache\\'''$original_image_file);
            
        if 
(!file_exists(FCPATH 'images/' $original_image_short_path)) {
            @unlink($image);
        }
                    
    
}

    if (!is_file($new_file) || (filectime($old_file) > filectime($new_file))) {

        if (!is_dir($old_file)) {
            @mkdir(FCPATH 'images/cache/'dirname($filename), 0777);
        }
            
        $config
['image_library'] = 'gd2';
        $config['source_image'] = $old_file;
        $config['maintain_ratio'] = FALSE;
        $config['width'] = $width;
        $config['height'] = $height;
        $config['new_image'] = $new_file;

        $this->image_lib->clear();
        $this->image_lib->initialize($config);
        $this->image_lib->resize();

    }


    return base_url('images/cache/' $extended_image);