How can i get a variable inside WP_Image_Editor?
-
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 frommulti_resize()
togenerate_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 togenerate_filename()
but the thumbnails are all generated in uploads folder, overwriting each other. How can i do this?Any clue is greatly appreciated.
- The topic ‘How can i get a variable inside WP_Image_Editor?’ is closed to new replies.