insert image into imagecache with cck computed field

hoppurr - December 2, 2008 - 20:01

the problem is such, i'm trying to use a computed field based on the value of another field to add an image in the server's path into the imagecache for the node

such as
if ($field->code['value'] == "red") {
set image to red image
}

here is something i did that

hoppurr - May 26, 2009 - 18:24

looks like i figured it out myself, hope it helps anyone else doing this

$picture_name=$node->field_link[0]['value']
if (!file_exists("files/".$picture_name..jpg")) {
$urlb=$[octire_name;
/* STEP 2. visit the homepage to set the cookie properly */
$ch = curl_init ($urlb);
//curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$picture = curl_exec ($ch);
$destination=fopen("files/".$picture_name.".jpg",'wb');
fwrite($destination,$picture);
//fclose($picture);
fclose($destination);
}

$check = db_fetch_array(db_query("SELECT * FROM {content_field_image_cache} WHERE nid=%d",$node->nid));
if ($check == NULL) {
    $delta=0;
    $file['filename']=$picture_name.".jpg";
    $file['filepath']="files/".$file['filename'];
    $file['filemime']="image/jpeg";
    $file['filesize']=filesize($file['filepath']);
    $file['fid'] = db_next_id('{files}_fid');
    $file['title']="";
    $file['alt']="";
  db_query("INSERT into {files} (fid, nid, filename, filepath, filemime, filesize)
             VALUES (%d, %d, '%s','%s','%s',%d)",
            $file['fid'], $node->nid, $file['filename'], $file['filepath'], $file['filemime'], $file['filesize']);
db_query("INSERT INTO {content_field_image_cache} (nid,vid,delta,field_image_cache_fid,field_image_cache_title,field_image_cache_alt) VALUES  (%d,%d,%d,%d,'%s','%s')",$node->nid,$node->nid,$node->nid,$file['fid'],$file['filename'],$file['filename']);

}

You rock .

markalosey - June 10, 2009 - 02:11

Thank you for doing this research...and making it easy for me. I have been fretting over this for under an hour thanks to your fine work.

Mark A Losey
tng worldwide

 
 

Drupal is a registered trademark of Dries Buytaert.