1

I need to resize a few existing images in my upload directory so doing some tests with wp_get_image_editor. Please check the example code below.

$img = wp_get_image_editor( ABSPATH.'test/flower.jpg' );
if ( ! is_wp_error( $img )) {
    $img->resize( 800, NULL, false ); //resize to max-width of 800px.
    $filename = $img->generate_filename( 'custom-suffix-string', NULL, 'jpg');
    $saved = $img->save($filename);

    //information returned by $saved
    echo "Filename: ".$saved['file']."<br>";
    }

As you can see I can add a custom suffix with line-4 of the code. So the name of my resized file will be flower-custom-suffix-string.jpg. The problem is I need it to return as NULL.

If I use generate_filename( NULL, NULL, 'jpg'); the echoed result is Filename: flower-800x667.jpg. So basically I am trying to get the name of the RESIZED image as flower.jpg same as original and get rid of -800x667 from the name. If the process overwrites/deletes the original file, that would not be a problem. Any suggestions or any other way to achieve this ? TIA.


UPDATE : I ended up using the code below. Thanks for the tip @Zakir

$filename = $img->generate_filename( 'temp', NULL, NULL);
$new_name = str_replace('-temp', '', $filename);
$resized_img = $img->save($new_name);

1 Answer 1

0

You can simply use preg_replace() functions to remove -800x667 (or any height) from the image. Like this-

$image = generate_filename( NULL, NULL, 'jpg');
echo preg_replace( '/-800x[0-9]{2,4}/', '', $image );
3
  • 1
    Every image would have dynamic height value, I doubt this would work.
    – gurung
    Jul 18, 2015 at 18:17
  • code updated. Feel free to test and review
    – Zak
    Jul 18, 2015 at 18:33
  • i will test n update, thnx
    – gurung
    Jul 18, 2015 at 18:55

Your Answer

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

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