get_extension
seems to be a protected method dunno why, seems to me there is no way to get file extension using image editor object...
I know I can use getimagesize
with php and be done with it but that would mean fetching the image a second time which would be nice to avoid
1 Answer
I tried using generate_filename
yesterday but probably it was kind of late and I was trying and didnt manage to make it work, today I managed to workaround the problem without fetching the image another time like this:
$save_path = WP_CONTENT_DIR . '/tmp/' . $img_name . "-" . $nw . "x" . $nh;
$wp_img->save($save_path);
$img_name = basename($wp_img->generate_filename('suffix'));
$img_name = str_replace('-suffix', '', $img_name);
$img_url = content_url() . '/tmp/' . $img_name;
$filepath = WP_CONTENT_DIR . '/tmp/' . $img_name;
As you can see a suffix must be specified because currently the method adds a default suffix as the image size by default, and to retrieve the correct image filename + extension it must be removed afterwards.
pathinfo
save()
method of wp image editor object which adds the file extension when saving the file so I cannot even use pathinfo of the saved object, I guess I will use php directly to save the images...anyway the image editor object in wp seems really silly in this way.pathinfo
on the result from thegenerate_filename
method? I'm just guessing by reading the source at core.trac.wordpress.org/browser/tags/4.1.1/src/wp-includes/…generate_filename
adds the extension itself so there is no need forpathinfo
, basically every method in the image editor obj retrieves the extension, but it just can't be accessed from the outside...the only problem is the fact that it adds a suffix and it needs to be removed as I specified in the answer