Resize a picture to the new size
static public resizePicture ( $source_path, $dest_path, $new_width = 71, $new_height = 71, $img_y, $img_x, $img_width, $img_height, $max_size = 500 ) : boolean | ||
$source_path | string path of the picture to be resized | |
$dest_path | string path of the new resized picture | |
$new_width | string new width after resized (default 71) | |
$new_height | string new height after resized (default 71) | |
$img_y | string y axis of picture (default 0) | |
$img_x | string x axis of picture (default 0) | |
$img_width | string width of picture (default 0) | |
$img_height | string height of picture (default 0) | |
$max_size | integer max size of the picture (default 500, is set to 0 no resize) | |
return | boolean | : true or false |
/** * Synchronise picture (photo) of the user * * ÷@since version 0.85 * * @return string : the filename to be stored in user picture field **/ function syncLdapPhoto() { if (isset($this->fields["authtype"]) && ($this->fields["authtype"] == Auth::LDAP || Auth::isAlternateAuth($this->fields['authtype']))) { if (isset($this->fields["id"]) && $this->fields["id"] > 0) { $config_ldap = new AuthLDAP(); $ds = false; //connect ldap server if ($config_ldap->getFromDB($this->fields['auths_id'])) { $ds = $config_ldap->connect(); } if ($ds) { //get picture fields $picture_field = $config_ldap->fields['picture_field']; if (empty($picture_field)) { return false; } //get picture content in ldap $info = AuthLdap::getUserByDn($ds, $this->fields['user_dn'], array($picture_field), false); //getUserByDn returns an array. If the picture is empty, //$info[$picture_field][0] is null if (!isset($info[$picture_field][0]) || empty($info[$picture_field][0])) { return ""; } //prepare paths $img = array_pop($info[$picture_field]); $filename = uniqid($this->fields['id'] . '_'); $sub = substr($filename, -2); /* 2 hex digit */ $file = GLPI_PICTURE_DIR . "/{$sub}/{$filename}.jpg"; $oldfile = GLPI_PICTURE_DIR . "/" . $this->fields["picture"]; // update picture if not exist or changed if (!file_exists($oldfile) || sha1_file($oldfile) !== sha1($img)) { if (!is_dir(GLPI_PICTURE_DIR . "/{$sub}")) { mkdir(GLPI_PICTURE_DIR . "/{$sub}"); } //save picture $outjpeg = fopen($file, 'wb'); fwrite($outjpeg, $img); fclose($outjpeg); //save thumbnail $thumb = GLPI_PICTURE_DIR . "/{$sub}/{$filename}_min.jpg"; Toolbox::resizePicture($file, $thumb); return "{$sub}/{$filename}.jpg"; } return $this->fields["picture"]; } } } return false; }
/** * add files (from $this->input['_filename']) to an ITIL object * create document if needed * create link from document to ITIL object * * @param $donotif Boolean if we want to raise notification (default 1) * @param $disablenotif (default 0) * * @return array of doc added name **/ function addFiles($donotif = 1, $disablenotif = 0) { global $CFG_GLPI; if (!isset($this->input['_filename']) || count($this->input['_filename']) == 0) { return array(); } $docadded = array(); foreach ($this->input['_filename'] as $key => $file) { $doc = new Document(); $docitem = new Document_Item(); $docID = 0; $filename = GLPI_TMP_DIR . "/" . $file; $input2 = array(); // Crop/Resize image file if needed if (isset($this->input['_coordinates']) && !empty($this->input['_coordinates'][$key])) { $image_coordinates = json_decode(urldecode($this->input['_coordinates'][$key]), true); Toolbox::resizePicture($filename, $filename, $image_coordinates['img_w'], $image_coordinates['img_h'], $image_coordinates['img_y'], $image_coordinates['img_x'], $image_coordinates['img_w'], $image_coordinates['img_h'], 0); } else { Toolbox::resizePicture($filename, $filename, 0, 0, 0, 0, 0, 0, 0); } //If file tag is present if (isset($this->input['_tag_filename']) && !empty($this->input['_tag_filename'][$key])) { $this->input['_tag'][$key] = $this->input['_tag_filename'][$key]; } // Check for duplicate if ($doc->getFromDBbyContent($this->fields["entities_id"], $filename)) { if (!$doc->fields['is_blacklisted']) { $docID = $doc->fields["id"]; } // File already exist, we replace the tag by the existing one if (isset($this->input['_tag'][$key]) && $docID > 0 && isset($this->input['content'])) { $this->input['content'] = preg_replace('/' . Document::getImageTag($this->input['_tag'][$key]) . '/', Document::getImageTag($doc->fields["tag"]), $this->input['content']); $docadded[$docID]['tag'] = $doc->fields["tag"]; } } else { //TRANS: Default document to files attached to tickets : %d is the ticket id $input2["name"] = addslashes(sprintf(__('Document Ticket %d'), $this->getID())); if ($this->getType() == 'Ticket') { $input2["tickets_id"] = $this->getID(); // Insert image tag if (isset($this->input['_tag'][$key])) { $input2["tag"] = $this->input['_tag'][$key]; } } $input2["entities_id"] = $this->fields["entities_id"]; $input2["documentcategories_id"] = $CFG_GLPI["documentcategories_id_forticket"]; $input2["_only_if_upload_succeed"] = 1; $input2["entities_id"] = $this->fields["entities_id"]; $input2["_filename"] = array($file); $docID = $doc->add($input2); } if ($docID > 0) { if ($docitem->add(array('documents_id' => $docID, '_do_notif' => $donotif, '_disablenotif' => $disablenotif, 'itemtype' => $this->getType(), 'items_id' => $this->getID()))) { $docadded[$docID]['data'] = sprintf(__('%1$s - %2$s'), stripslashes($doc->fields["name"]), stripslashes($doc->fields["filename"])); if (isset($input2["tag"])) { $docadded[$docID]['tag'] = $input2["tag"]; unset($this->input['_filename'][$key]); unset($this->input['_tag'][$key]); } if (isset($this->input['_coordinates'][$key])) { unset($this->input['_coordinates'][$key]); } } } // Only notification for the first New doc $donotif = 0; } // Ticket update if (isset($this->input['content'])) { if ($CFG_GLPI["use_rich_text"]) { $this->input['content'] = $this->convertTagToImage($this->input['content'], true, $docadded); $this->input['_forcenotif'] = true; } else { $this->fields['content'] = $this->setSimpleTextContent($this->input['content']); $this->updateInDB(array('content')); } } return $docadded; }
/** * Synchronise picture (photo) of the user * * ÷@since version 0.85 * * @return string : the filename to be stored in user picture field **/ function syncLdapPhoto() { if (isset($this->fields["authtype"]) && $this->fields["authtype"] == Auth::LDAP) { if (isset($this->fields["id"]) && $this->fields["id"] > 0) { $config_ldap = new AuthLDAP(); $ds = false; //connect ldap server if ($config_ldap->getFromDB($this->fields['auths_id'])) { $ds = $config_ldap->connect(); } if ($ds) { //get picture fields $picture_field = $config_ldap->fields['picture_field']; if (empty($picture_field)) { return false; } //get picture content in ldap $info = AuthLdap::getUserByDn($ds, $this->fields['user_dn'], array($picture_field), false); //getUserByDn returns an array. If the picture is empty, //$info[$picture_field][0] is null if (!isset($info[$picture_field][0]) || empty($info[$picture_field][0])) { return ""; } //prepare paths $img = array_pop($info[$picture_field]); $filename = $this->fields["id"]; $file = GLPI_PICTURE_DIR . "/" . $filename . '.jpg'; //save picture $outjpeg = fopen($file, 'wb'); fwrite($outjpeg, $img); fclose($outjpeg); //save thumbnail $thumb = GLPI_PICTURE_DIR . "/" . $filename . '_min.jpg'; Toolbox::resizePicture($file, $thumb); return $filename . ".jpg"; } } } return false; }