delete_generated_files() static public method

Deletes the auto-generated files for resize and letterboxing created by Timber
static public delete_generated_files ( string $local_file )
$local_file string ex: /var/www/wp-content/uploads/2015/my-pic.jpg or: http://example.org/wp-content/uploads/2015/my-pic.jpg
Beispiel #1
0
 /**
  * Deletes all resized versions of an image when the source is deleted
  */
 protected static function add_actions()
 {
     add_action('delete_attachment', function ($post_id) {
         $post = get_post($post_id);
         $image_types = array('image/jpeg', 'image/png', 'image/gif', 'image/jpg');
         if (in_array($post->post_mime_type, $image_types)) {
             $attachment = new Image($post_id);
             if ($attachment->file_loc) {
                 ImageHelper::delete_generated_files($attachment->file_loc);
             }
         }
     });
 }
Beispiel #2
0
 /**
  * Generate image sizes defined for Timmy with TimberImageHelper.
  *
  * @param  int	$attachment_id	The attachment ID for which all images should be resized
  * @return void
  */
 private function timber_generate_sizes($attachment_id)
 {
     $img_sizes = get_image_sizes();
     $attachment = get_post($attachment_id);
     // Timber needs the file src as an url
     $file_src = wp_get_attachment_url($attachment_id);
     /**
      * Delete all existing image sizes for that file.
      *
      * This way, when Regenerate Thumbnails will be used,
      * all non-registered image sizes will be deleted as well.
      * Because Timber creates image sizes when they’re needed,
      * we can safely do this.
      */
     Timber\ImageHelper::delete_generated_files($file_src);
     foreach ($img_sizes as $key => $img_size) {
         if (!$this->timber_should_resize($attachment->post_parent, $img_size)) {
             continue;
         }
         $resize = $img_size['resize'];
         // Get values for the default image
         $crop = isset($resize[2]) ? $resize[2] : 'default';
         $force = isset($resize[3]) ? $resize[3] : false;
         image_downsize($attachment_id, $key);
         if (isset($img_size['generate_srcset_sizes']) && false === $img_size['generate_srcset_sizes']) {
             continue;
         }
         // Generate additional image sizes used for srcset
         if (isset($img_size['srcset'])) {
             foreach ($img_size['srcset'] as $src) {
                 // Get width and height for the additional src
                 if (is_array($src)) {
                     $width = $src[0];
                     $height = isset($src[1]) ? $src[1] : 0;
                 } else {
                     $width = (int) round($resize[0] * $src);
                     $height = isset($resize[1]) ? (int) round($resize[1] * $src) : 0;
                 }
                 // For the new source, we use the same $crop and $force values as the default image
                 self::resize($img_size, $file_src, $width, $height, $crop, $force);
             }
         }
     }
 }
Beispiel #3
0
 /**
  * Checks if attachment is an image before deleting generated files
  *
  * @param  int  $post_id   an attachment post id
  *
  */
 public static function _delete_generated_if_image($post_id)
 {
     if (wp_attachment_is_image($post_id)) {
         $attachment = new Image($post_id);
         if ($attachment->file_loc) {
             ImageHelper::delete_generated_files($attachment->file_loc);
         }
     }
 }