Skip to content

Make Tonesque work with allow_url_fopen disabled #2

@mightymt

Description

@mightymt

Currently Tonesque doesn't work with remote images when running in environments where allow_url_fopen is disabled because it uses the imagecreatefromgif, imagecreatefrompng and imagecreatefromjpeg functions.

I would suggest that in the color function you replace this...

switch ( $file ) {
    case 'gif' :
        $img = imagecreatefromgif( $image );
        break;
    case 'png' :
        $img = imagecreatefrompng( $image );
        break;
    case 'jpg' :
    case 'jpeg' :
        $img = imagecreatefromjpeg( $image );
        break;
    default:
        return false;
}

...with something like this...

if ( !in_array( $file, array( 'gif', 'jpg', 'jpeg', 'png' ) ) ) {
    return false;
}   

$response = wp_remote_request( $image, array( 'method' => 'GET' ) );

if ( is_wp_error( $response ) || empty( $response[ 'body' ] ) || $response['response']['code'] != 200 ) {
    return false;
}

$img = imagecreatefromstring( wp_remote_retrieve_body( $response ) );

I tested the above code on my shared hosted site where allow_url_fopen is disabled and it works as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions