Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
share|improve this question
    
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. –  Chris Strutton Feb 11 '13 at 9:28

2 Answers 2

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 );
}
share|improve this answer
    
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. –  Chris Strutton Feb 11 '13 at 8:30

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");
share|improve this answer
    
That makes sense. I will try that and see how it works. (check mark pending). –  Chris Strutton Feb 11 '13 at 8:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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