0

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
1
  • Searchin for info on wp-load.php pointed me to several reasons why not to do this. Would ajax be a better way to do this? Could anyone give me an idea where to start researching this. Feb 11 '13 at 9:28
0

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 );
}
1
  • The problem with that is timing. I don't know the image url when the meta box is created. The user enters a url and then I want to load a re sized image into an image tag in the meta box. Feb 11 '13 at 8:30
0

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.

-2

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");
2
  • That makes sense. I will try that and see how it works. (check mark pending). Feb 11 '13 at 8:31
  • 1
    This is a bad practice! Never 'just' load WordPress this way
    – xvilo
    Sep 2 '17 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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