1

I wrote this function in my functions.php

function header_resized_img ($path, $width, $height) {
    $image = wp_get_image_editor($path);
    if (!is_wp_error($image)) {
        $image->resize(9999, $height, false);
        $orig_size = $image->get_size();
        $image->crop($orig_size['width']/2-$width/2, $orig_size['height']/2-$height/2, $width, $height);
        $image->stream( $mime_type = 'image/jpeg');
    }
}

To make it work, i created a file named page-image.php, then a page with permalink http://www.example.com/image/. Inside page-image.php i've put (just a test)

header_resized_img (get_header_image(), 414, 700);

which, in facts, does output the resized/cropped image when i visit it. Now i want to make it more flexible and pass parameters through URL. I tried query strings using $_GET, then i found get_query_arg, but none of these seems to do the trick. How could i do this? Thank you.

9
  • Hi, Welcome to WPSE. Can you please make an example of what you want to do? Commented May 15, 2017 at 19:18
  • Sure. If i visit the url like example.com/image/?w=360&h=480&img_path=imgpath it should execute my function on the image in imgpath, resizing and cropping it. Page should just output the image so i can embed it Commented May 15, 2017 at 19:23
  • @JackJohansson Doing this cause i need multiple versions of the same image Commented May 15, 2017 at 19:24
  • I posted an answer, see if it works. If not, check your log and see if there's some useful info in it. Commented May 15, 2017 at 19:28
  • I deleted the answer temporarily to figure out a way to solve your problem. Will be back shortly. Commented May 15, 2017 at 20:51

1 Answer 1

1

This should actually work for you:

function header_resized_img () {
    $image = wp_get_image_editor($_GET['path']);
    $height = $_GET['height'];
    $width =  $_GET['width'];
    if (!is_wp_error($image)) {
        $image->resize(9999, $height, false);
        $orig_size = $image->get_size();
        $image->crop($orig_size['width']/2-$width/2, $orig_size['height']/2-$height/2, $width, $height);
        $image->stream( $mime_type = 'image/jpeg');
    }
}

and include your function somewhere in the template:

header_resized_img();

Then try accessing this URL:

http://example.com/image/?width=500&height=400&path=some-url

To generate your image.

9
  • It opens the 404 template Commented May 15, 2017 at 19:44
  • @DanieleSqualo Sorry my bad, i forgot a slash in the URL. Check out the new URL. Commented May 15, 2017 at 19:45
  • Same thing, a 404 page Commented May 15, 2017 at 19:49
  • replace the url with this : http://example.com/index.php?page_id=YOUR_PAGE_ID&width=500&height=400&path=some-url. If it works, let me know to write a rewrite rule for your page. Commented May 15, 2017 at 19:53
  • Again, the 404 page :/ Commented May 15, 2017 at 20:40

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.