Пример #1
0
 /**
  * @param string $url Image's URL.
  * @return integer Saved file identifier
  */
 protected static function saveImage($url)
 {
     $fileId = false;
     $file = new \CFile();
     $httpClient = new HttpClient();
     $httpClient->setTimeout(5);
     $httpClient->setStreamTimeout(5);
     $urlComponents = parse_url($url);
     if ($urlComponents && strlen($urlComponents["path"]) > 0) {
         $tempPath = $file->GetTempName('', bx_basename($urlComponents["path"]));
     } else {
         $tempPath = $file->GetTempName('', bx_basename($url));
     }
     $httpClient->download($url, $tempPath);
     $fileName = $httpClient->getHeaders()->getFilename();
     $localFile = \CFile::MakeFileArray($tempPath);
     if (is_array($localFile)) {
         if (strlen($fileName) > 0) {
             $localFile['name'] = $fileName;
         }
         if (\CFile::CheckImageFile($localFile, 0, 0, 0, array("IMAGE")) === null) {
             $fileId = $file->SaveFile($localFile, 'urlpreview', true);
         }
     }
     return $fileId === false ? null : $fileId;
 }