public static function cacheThumbnail($backendName, $addressBookId, $contactId, \OCP\Image $image = null, $vCard = null, $options = array()) { $cache = \OC::$server->getCache(); $key = self::THUMBNAIL_PREFIX . $backendName . '::' . $addressBookId . '::' . $contactId; //$cache->remove($key); $haskey = $cache->hasKey($key); if (!array_key_exists('remove', $options) && !array_key_exists('update', $options)) { if ($cache->hasKey($key) && $image === null) { return $cache->get($key); } } else { if (isset($options['remove']) && $options['remove'] === false && (isset($options['update']) && $options['update'] === false)) { return $cache->get($key); } } if (isset($options['remove']) && $options['remove']) { $cache->remove($key); if (!isset($options['update']) || !$options['update']) { return false; } } if (is_null($image)) { if (is_null($vCard)) { $app = new App(); $vCard = $app->getContact($backendName, $addressBookId, $contactId); } $image = new \OCP\Image(); if (!isset($vCard->PHOTO) || !$image->loadFromBase64((string) $vCard->PHOTO)) { return false; } } if (!$image->centerCrop()) { \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t crop thumbnail for ID ' . $key, \OCP\Util::ERROR); return false; } if (!$image->resize(self::THUMBNAIL_SIZE)) { \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t resize thumbnail for ID ' . $key, \OCP\Util::ERROR); return false; } // Cache as base64 for around a month $cache->set($key, strval($image), 3000000); return $cache->get($key); }
public static function cacheThumbnail($backendName, $addressBookId, $contactId, \OCP\Image $image = null, $vCard = null, $options = array()) { $cache = \OC::$server->getCache(); $key = self::THUMBNAIL_PREFIX . $backendName . '::' . $addressBookId . '::' . $contactId; //$cache->remove($key); $haskey = $cache->hasKey($key); if (!array_key_exists('remove', $options) && !array_key_exists('update', $options)) { if ($cache->hasKey($key) && $image === null) { return $cache->get($key); } } else { if (isset($options['remove']) && $options['remove'] === false && (isset($options['update']) && $options['update'] === false)) { return $cache->get($key); } } if (isset($options['remove']) && $options['remove']) { $cache->remove($key); if (!isset($options['update']) || !$options['update']) { return false; } } if (is_null($image)) { if (is_null($vCard)) { $app = new App(); $vCard = $app->getContact($backendName, $addressBookId, $contactId); } if (!isset($vCard->PHOTO)) { return false; } $image = new \OCP\Image(); $photostring = (string) $vCard->PHOTO; if ($vCard->PHOTO instanceof \Sabre\VObject\Property\Uri && substr($photostring, 0, 5) === 'data:') { $mimeType = substr($photostring, 5, strpos($photostring, ',') - 5); if (strpos($mimeType, ';')) { $mimeType = substr($mimeType, 0, strpos($mimeType, ';')); } $photostring = substr($photostring, strpos($photostring, ',') + 1); } if (!$image->loadFromBase64($photostring)) { #\OCP\Util::writeLog('contacts', __METHOD__.', photo: ' . print_r($photostring, true), \OCP\Util::DEBUG); return false; } } if (!$image->centerCrop()) { \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t crop thumbnail for ID ' . $key, \OCP\Util::ERROR); return false; } if (!$image->resize(self::THUMBNAIL_SIZE)) { \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t resize thumbnail for ID ' . $key, \OCP\Util::ERROR); return false; } // Cache as base64 for around a month $cache->set($key, strval($image), 3000000); return $cache->get($key); }