How do I return an image from a script

I have googled but must not be using the correct terms. I am trying to add a script to my WordPress plugin that takes a url and returns a reformatted image. I know this is frowned upon as hotlinking. The image in my case is supplied by a website imaging service.

The image takes from 3 to 5 seconds to be generated. I have a java script that sets my preview image src tag once the website url is entered (onblur). I need a way to download the full sized image from the service and resize it as a thumbnail (I was resizing it in the browser but it looks horible). I tried accessing wordpress's download_url() function from my script but I get lots of unresolved references. I am sure I am doing something wrong.

My existing code:

?php
require_once('../../../wp-admin/includes/file.php');
$im = download_url('http://www.linux.org/images/logo/linuxorg.gif'); 
header('content-type: image/gif'); 
// resize and send image data here...

I get back the following error:

Fatal error: Call to undefined function __() in /homepages/5/d367772185/htdocs/rodandfly/wp-admin/includes/file.php on line 13

Topic php images Wordpress

Category Web


The core problem is that you need your code to be executed in the context of wordpress, and it seems like you are trying to do it the other way around (executed wordpress code in the context of your script).

The way to achieve it is by sending your image generation request to the main wordpress url and use either $_POST or $_GET variables to indicate that the request is intended to be handled by your code, hook on the init action and detect if the request is for your code, generate whatever you need, and exit.


Fatal error: Call to undefined function __() in /homepages/5/d367772185/htdocs/rodandfly/wp-admin/includes/file.php on line 13

Where is your code? file.php is expecting __() to be defined which it should be if your loading it from within an activated plugin.

The best way to do what your trying to do is use either media_handle_sidelaod() or media_sideload_image() which will cause WordPress to create the thumbnails from the full image.

$resized_img_url = wpse_download_resize( 'http://www.linux.org/images/logo/linuxorg.gif' );

function wpse_download_resize( $url ) {
    $tmp = download_url( $url );
    $file = array(
        'name' => basename( $url ),
        'tmp_name' => $tmp
    );

    if ( is_wp_error( $tmp ) ) {
        @unlink( $file[ 'tmp_name' ] );
    return $tmp
    }

    $id = media_handle_sideload( $file, 0 );
    // Check for handle sideload errors.
    if ( is_wp_error( $id ) ) {
        @unlink( $file['tmp_name'] );
    return $id;
    }

    return wp_get_attachment_thumb_url( $id );
}

That's because you do not have default WordPress with all it's functions loaded. You'll have to prepend this line:

include("../../../wp-load.php");

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.