CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Script to import pictures
Pages: [1]

Author Topic: Script to import pictures  (Read 1605 times)

JoopSJ

  • I post occasionally
  • **
  • Posts: 80
  • Karma: 2
Script to import pictures
May 06, 2009, 04:07:12 am
I have a custom field titled "picture" to upload a contact picture.

I just imported e few thousand contacts and for all these contact I have a picture.
They are all neatly named external_id.jpg and all the files are allready located on the server.

I first ran a manual upload for one picture to see how the information is put into the database.

Then I wrote a (drupal) script to put the picture data in the civicrm tables:

Form civicrm_contact I got the [id] and [external_identifier].
I inserted a record in civicrm_file containing the filename and info and retrieved the resulting [id] (id of file record)
And UPDATED my customtable_picture_field to contain the file_id.

When I display the contact the filename is shown but not the picture. When the filename is clicked an error is generated that the file can not be found.
When I do a manual upload the data stored equally as when using the script with the only exception that a file : 123456.jpg is stored as 12345_jpg_hash.unknown.

Anyone? Thanx a lot!

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Script to import pictures
May 06, 2009, 12:13:30 pm

1. There is also a table called civicrm_entity_file. you might want to check into that table and see if that table is used (my memory is a bit hazy on it now)

2. jpg is a safe file extension. The unknown extension should not have been added, can u check on a url similar to:

 http://drupal.demo.civicrm.org/civicrm/admin/options/safe_file_extension?group=safe_file_extension&reset=1

3. adding the hash might be a good thing so that your files are in a similar format to civicrm's

jump onto the irc channel if u want to discuss this in more detail

lobo

A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

JoopSJ

  • I post occasionally
  • **
  • Posts: 80
  • Karma: 2
Re: Script to import pictures
May 06, 2009, 01:46:09 pm
Your memory is perfect! Indeed, I overlooked the civicrm_entity_file table.
Here the table holding the custom field is groeped together with the entity_id and file_id.
I will update the script and report back here.
Thanx

JoopSJ

  • I post occasionally
  • **
  • Posts: 80
  • Karma: 2
Re: Script to import pictures
May 07, 2009, 05:16:03 am
After correcting the script to also populate the civicrm_entity_file table I get a rectangle now i.s.o the picture.
It's a step forward but I'm not there yet.

JoopSJ

  • I post occasionally
  • **
  • Posts: 80
  • Karma: 2
Re: Script to import pictures
May 07, 2009, 06:07:27 am
 :)
Please disregard previous reply.
The pictures are shown correctly!
Thanx Lobo!

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: Script to import pictures
May 07, 2009, 07:52:28 am
As a note of interest, I will share that I also have a Drupal script which allows the operator to upload images to be used as profile images for contacts. The operator must name the files something like myemail@mydomain.com.jpg and then he can drag/drop onto a Java applet file uploader and that's it. The files are uploaded and assigned to his contacts.

The Java uploader, however, requires a third party license, but it's not very expensive. :)
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

JonGold

  • Ask me questions
  • ****
  • Posts: 638
  • Karma: 81
    • Palante Technology
  • CiviCRM version: 4.1 to the latest
  • CMS version: Drupal 6-7, Wordpress 4.0+
  • PHP version: PHP 5.3-5.5
Re: Script to import pictures
July 31, 2014, 11:13:07 am
Hi Hershel (and JoopSJ),

Would you mind sharing your script?  I have a very similar need!

Thanks,
Jon
Sign up to StackExchange and get free expert CiviCRM advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: Script to import pictures
July 31, 2014, 12:09:50 pm
You are asking me for code from over 5 years ago. I actually still have it and don't mind to post it, but CiviCRM has changed a lot since then and I can't guarantee at all that it will still work, nor that there are not better ways to do this today, but anyhow, here is the relevant code:

Code: [Select]
function raduploadprocessPix($files) {
  if (!civiutils_include(array('DAO','Contact','DAO','Config'))) {
return FALSE;
}
$config = & CRM_Core_Config::singleton();
$save_path = $config->customFileUploadDir;
$output = '';
for($i=0; $i < count($files['name']); $i++) {
$name = split('/',$files['name'][$i]);
    $name = $name[count($name)-1];
// API fails for some reason
// $params = array('email' => substr($name,0,-4), 'is_deleted' => 0);
//    $contact = & civicrm_contact_get( $params );
    $sql = 'select email,display_name,civicrm_contact.id from civicrm_email
      left join civicrm_contact on civicrm_email.contact_id = civicrm_contact.id
      where email="' . substr($name,0,-4) . '" AND is_deleted=0 group by contact_id';
    $dao =& CRM_Core_DAO::executeQuery("SELECT COUNT(*) as c FROM ($sql) AS x");
    $dao->fetch();
      if (1 != $dao->c) {
        $output .= "Ooops! No exact email match was found for the file $name<br><br>";
      }
      else {
        $dao =& CRM_Core_DAO::executeQuery($sql);
        $params = array('contact_id' => $dao->id);
        $contact = & civicrm_contact_get( $params );
        $contact = array_pop($contact);
        $sql = "select id, Profile_Picture from  `civicrm_value_1_Pictures` where `entity_id`='{$contact['contact_id']}';";
        $daoProfile_Picture =& CRM_Core_DAO::executeQuery($sql);
        if ($daoProfile_Picture->fetch() && null != $daoProfile_Picture->Profile_Picture) {
          $output .= "Ooops! The contact named {$contact['display_name']} already has a picture so we did NOT process the file $name<br><br>";
        }
        else {
          $newname = 'id'.$contact['contact_id'].'.'.$name;
          move_uploaded_file($files['tmp_name'][$i], $save_path .$newname);
          $sql = "insert into `civicrm_file`(`mime_type`,`uri`,`upload_date`) values ('{$files['type'][$i]}','$newname',NOW());";
          $dao =& CRM_Core_DAO::executeQuery($sql);
          $newid = mysql_insert_id();
          $sql = "insert into `civicrm_entity_file`(`entity_table`,`entity_id`,`file_id`) values ( 'civicrm_value_1_Pictures',{$contact['contact_id']},$newid);";
          $dao =& CRM_Core_DAO::executeQuery($sql);
          if (null != $daoProfile_Picture->id) {
            $sql = "update `civicrm_value_1_Pictures` set `Profile_Picture`=$newid;";
          }
          else {
            $sql = "insert into `civicrm_value_1_Pictures`(`entity_id`,`Profile_Picture`) values ({$contact['contact_id']},$newid);";
          }
          $dao =& CRM_Core_DAO::executeQuery($sql);
          $output .= "The file $name was saved for the contact named ".$contact['display_name'].' (id #'.$contact['contact_id'].')<br><br>';
        }
      }
    }
return $output;
}

Perhaps the basic idea will be of help to you.
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Script to import pictures

This forum was archived on 2017-11-26.