2

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

4
  • Just use pathinfo Commented Mar 14, 2015 at 22:37
  • $filename in pathinfo requires the path ending with the extension so I would need the extension to get the extension..The thing is that I save the file using the 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.
    – untore
    Commented Mar 14, 2015 at 22:55
  • Could you use pathinfo on the result from the generate_filename method? I'm just guessing by reading the source at core.trac.wordpress.org/browser/tags/4.1.1/src/wp-includes/…
    – kingkool68
    Commented Mar 15, 2015 at 4:03
  • generate_filename adds the extension itself so there is no need for pathinfo, 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
    – untore
    Commented Mar 15, 2015 at 10:46

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.