Wordpress curl save Images

Posted by Jeton Ramadani on Stack Overflow See other posts from Stack Overflow or by Jeton Ramadani
Published on 2012-11-08T23:36:15Z Indexed on 2012/11/09 5:01 UTC
Read the original article Hit count: 79

Filed under:
|
|
|

I am working on saving images from external sites into a folder in my wordpress theme. And I was wondering if its Ok to call curl twice or can it be done with one time.

Example:

$data = get_url('http://www.veoh.com/watch/v19935546Y8hZPgbZ'); // getting the url first curl instance
preg_match('/fullHighResImagePath="(.*?)"/', $data, $thumbnail); // find the image from content
savePhoto($thumbnail, $post->ID); //2nd instance of curl to save the image

function get_url($url) {
$user_agent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2)";
    $ytc = curl_init(); // initialize curl handle 
    curl_setopt($ytc, CURLOPT_URL, $url); // set url to post to 
    curl_setopt($ytc, CURLOPT_FAILONERROR, 1);  // Fail on errors 
    curl_setopt($ytc, CURLOPT_FOLLOWLOCATION, 1); // allow redirects 
    curl_setopt($ytc, CURLOPT_RETURNTRANSFER, 1); // return into a variable 
    curl_setopt($ytc, CURLOPT_PORT, 80); //Set the port number 
    curl_setopt($ytc, CURLOPT_TIMEOUT, 15); // times out after 15s 
    curl_setopt($ytc, CURLOPT_HEADER, 1); // include HTTP headers
    curl_setopt($ytc, CURLOPT_USERAGENT, $user_agent);
    $source = curl_exec($ytc);
    curl_close($ytc);

     $data = trim( $source );
    return $data;
}

function savePhoto($remoteImage, $isbn) {
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $remoteImage);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
    $fileContents = curl_exec($ch);
    curl_close($ch);
    if (DIRECTORY_SEPARATOR=='/'){
        $absolute_path = dirname(__FILE__).'/'; 
    } else { 
        $absolute_path = str_replace('\\', '/', dirname(__FILE__)).'/'; 
    }
    $newImg = imagecreatefromstring($fileContents);
    return imagejpeg($newImg, $absolute_path ."video_images/{$isbn}.jpg",100);
}

© Stack Overflow or respective owner

Related posts about php

Related posts about Wordpress