Remove an image that was not put in uploads folder via wp_handle_upload

前端 未结 3 1083
长情又很酷
长情又很酷 2021-02-20 05:48

I\'m saving an image to uploads folder, but I\'m using file_put_contents instead of wp_handle_upload - because I get an image in base64 and not as a file in $_FILES.

Im

3条回答
  •  我寻月下人不归
    2021-02-20 05:57

    I actually accomplished this before I saw an answer here, so I'm providing a solution here if someone runs into the same problem. I used the function below (first one) and also using wp_delete_attachment after just to be sure all stuff got removed.

    /**
     * Attempt at removing all images in uploads folder by providing an image url 
     * Example: 
     *   - Provide http://mypage.com/wp-content/themes/mytheme/uploads/2015/12/testImage.jpg
     *   - this should remove all its images created when uploading:
     *   - testImage-150x150.jpg, testImage-300x300.jpg etc and also the provided original image 
     *
     * We'r doing this because wp_delete_attachment() will not remove an image that was uploaded via a 
     * custom mytheme_upload_image() function (which is providing base64 image instead of file in $_FILES)
     *
     * TODO TODO mytheme_get_image_sizes() does not return ALL IMAGES THAT WERE CREATED (all sizes)
     */
    function mytheme_remove_all_image_sizes_from_uploads($primary_image_url) {
    
        $pi = pathinfo($primary_image_url);
    
        $img_dirname = $pi['dirname'];
        $img_dirname_exploded = explode('/',$img_dirname);   
        $last_dir = array_pop($img_dirname_exploded); // month usually (two digits)
        $second_last_dir = array_pop($img_dirname_exploded); // year usually (4 digits)
    
        $basename = $pi['basename']; // without trailing /
        $img_name = $pi['filename'];
        $img_extension = $pi['extension'];
    
        $uploads = wp_upload_dir();
        $base_uploads_dir = $uploads['basedir']; // without trailing /
    
        $path_to_appropriate_uploads_dir =      $base_uploads_dir.'/'.$second_last_dir.'/'.$last_dir.'/';
    
        $img_name_to_remove = $img_name.'.'.$img_extension; // UNLINK
    
        if(!@unlink($path_to_appropriate_uploads_dir.$img_name_to_remove)) {
           // this image was not removed
        }
    
        $image_sizes = mytheme_get_image_sizes();
    
        foreach($image_sizes as $size) {
           $img_name_to_remove = $img_name.'-'.$size.'.'.$img_extension; // UNLINK
           $img_path = $path_to_appropriate_uploads_dir.$img_name_to_remove;
    
           if(mytheme_image_on_url_exists($img_path) && !@unlink($img_path)) {
              // this image was not removed
           }
        }
    
     }
    
    
        /**
     * Get size information for all currently-registered image sizes.
     * Found an example of this on one of the wordpress' example pages ..
     *
     * @global $_wp_additional_image_sizes
     * @uses   get_intermediate_image_sizes()
     * @return array $sizes Data for all currently-registered image sizes.
     * TODO TODO mytheme_get_image_sizes() does not return ALL IMAGES THAT WERE CREATED (all sizes)
     */
    function mytheme_get_image_sizes() {
        global $_wp_additional_image_sizes;
    
        $sizes = array();
    
        foreach ( get_intermediate_image_sizes() as $_size ) {
    
            if ( in_array( $_size, array( 'thumbnail', 'medium', 'large' ) ) ) {
    
                $width = get_option( "{$_size}_size_w" );
                $height = get_option( "{$_size}_size_h" );
    
            } elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
    
                $width = $_wp_additional_image_sizes[ $_size ]['width'];
                $height = $_wp_additional_image_sizes[ $_size ]['height'];
    
            }
    
            $img_name_end = $width."x".$height;
    
            if(!in_array($img_name_end,$sizes)) {
                $sizes[] = $img_name_end;
            }
    
        }
    
        // ADD CUSTOM SIZES (this one is not returned otherwise?!)
        $sizes[] = '300x200';
    
        return $sizes;
    }
    

提交回复
热议问题