• Hi, i’ve been writing some class and functions to modify the path of thumbnails. I extended the original WP_Image_Editor_GD class to achieve custom structure.

    What i want: to store the thumbnails in different folders based on their slugs, in the upload directory. such as : https://example.com/uploads/medium/image.jpg

    What i have already done:

    class WP_Image_Editor_Custom extends WP_Image_Editor_GD {
        public function generate_filename($prefix = NULL, $dest_path = NULL, $extension = NULL) {
    		global $current_size_slug;
            // If empty, generate a prefix with the parent method get_suffix().
            if(!$prefix)
                $prefix = $this->get_suffix();
    
            // Determine extension and directory based on file path.
            $info = pathinfo($this->file);
            $dir  = ABSPATH."/media/";
            $ext  = $info['extension'];
    
            // Determine image name.
            $name = wp_basename($this->file, ".$ext");
    
            // Allow extension to be changed via method argument.
            $new_ext = strtolower($extension ? $extension : $ext);
    
            // Default to $_dest_path if method argument is not set or invalid.
            if(!is_null($dest_path) && $_dest_path = realpath($dest_path))
                $dir = $_dest_path;
    
            // Return our new prefixed filename.
    		$slug = $current_size_slug;	
            return trailingslashit($dir)."{$slug}/{$name}.{$new_ext}";
        }
    	function multi_resize($sizes) {
    		$sizes = parent::multi_resize($sizes);
    		foreach($sizes as $slug => $data)
    			$sizes[$slug]['file'] = $slug."/".$data['file'];
    			$current_size_slug = $slug;
    		return $sizes;
    	}
    }

    When i upload the image, the thumbnails are created properly, however the filenames are not. The $slug value is not passed from multi_resize() to generate_filename.

    I tried to write the multi_resize() function as below:

    class WP_Image_Editor_Custom extends WP_Image_Editor_GD {
        public function generate_filename($prefix = NULL, $dest_path = NULL, $extension = NULL) {
    		global $current_size_slug;
            // If empty, generate a prefix with the parent method get_suffix().
            if(!$prefix)
                $prefix = $this->get_suffix();
    
            // Determine extension and directory based on file path.
            $info = pathinfo($this->file);
            $dir  = ABSPATH."/media/";
            $ext  = $info['extension'];
    
            // Determine image name.
            $name = wp_basename($this->file, ".$ext");
    
            // Allow extension to be changed via method argument.
            $new_ext = strtolower($extension ? $extension : $ext);
    
            // Default to $_dest_path if method argument is not set or invalid.
            if(!is_null($dest_path) && $_dest_path = realpath($dest_path))
                $dir = $_dest_path;
    
            // Return our new prefixed filename.
    		$slug = $current_size_slug;	
            return trailingslashit($dir)."{$slug}/{$name}.{$new_ext}";
        }
    	function multi_resize($sizes) {
    		$sizes = parent::multi_resize($sizes);
    		foreach($sizes as $slug => $data)
    			$sizes[$slug]['file'] = $slug."/".$data['file'];
    			$current_size_slug = $slug;
    		return $sizes;
    	}
    }

    Now the $slug is passed to generate_filename() but the thumbnails are all generated in uploads folder, overwriting each other. How can i do this?

    Any clue is greatly appreciated.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    You probably need to set a temporary uploads path using the “upload_dir” filter. To avoid disrupting other operations, add this filter just before you need it when the file is uploaded and remove it immediately after the resized files are all moved to their permanent locations.

    Have you fully thought through your image storage scheme? While I’m not 100% sure, I believe many WP functions assume all image sizes are together in the same folder. I believe this assumption can be corrected through filters, but there is likely a good number of functions that will need alteration, and you need to find hooks for each function. It may take a lot of digging and testing to ensure you’ve corrected all possible functions.

    I’m not saying this is a bad idea and you shouldn’t do it, I just want you to be aware that altering where the images get stored may only be the beginning of your work to get this fully implemented. Be sure this alternative scheme is worth all the effort.

    Thread Starter Jack Johansson

    (@mhmdshv)

    Thank you. One suggested to define a global variable and pass data through it, but i don’t know how. I’m not a server-side programmer, i do the front.

    However i’m sure the answer it easy and can be written in the fewest lines, but most of the people don’t bother reading the question since it’s long (even on stackoverflow).

    Btw i’m using other filters to implement the rest of the job, the only part i’m stuck at is this. I can’t fix both URL and folder structure at the same time.

    Moderator bcworkz

    (@bcworkz)

    TBH, I didn’t fully read through all the code, I’m certainly missing many details. I think I got enough to offer a possible solution or two. Globals are an option, but you can also add another optional parameter to your generate_filename() by specifying a default value for it. Then it will still work when the default parameters are passed, but you can also pass $slug for your specific need. For those that abhor globals ??

    Then the “upload_dir” filter can deal with the overwritten thumbnail issue by altering the upload’s initial location.

    Thread Starter Jack Johansson

    (@mhmdshv)

    Thank you. That’s why i don’t get an answer, no one reads the code because it’s too long ?? however it’s just a copy of the original class, i only changed 1 line : return trailingslashit($dir)."{$slug}/{$name}.{$new_ext}";

    I will see if i can get it any further using your suggestion.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How can i get a variable inside WP_Image_Editor?’ is closed to new replies.