Vladimir3261

Download yandex image

May 12th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.94 KB | None | 0 0
  1. <?php
  2. function loadYandexImage($url, $savePath)
  3. {
  4.     $imageFormats = [
  5.         'image/jpeg' => 'jpeg',
  6.         'image/jpg' => 'jpg',
  7.         'image/png' => 'png',
  8.         'image/gif' => 'gif',
  9.         'image/bmp' => 'bmp',
  10.     ];
  11.     $ch = curl_init();
  12.     curl_setopt($ch, CURLOPT_URL, $url);
  13.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  14.     $server_output = curl_exec ($ch);
  15.     $yandexData = curl_getinfo($ch);
  16.     $extension = (isset($yandexData['content_type']) && $yandexData['content_type'] && isset($imageFormats[$yandexData['content_type']])) ? $imageFormats[$yandexData['content_type']] : 'png';
  17.     $realImagePath = (isset($yandexData['redirect_url']) && $yandexData['redirect_url'] ) ? $yandexData['redirect_url'] : $url;
  18.     return copy($realImagePath, $savePath.'/'.date('d-m-y').'.'.$extension);
  19. }
  20.  
  21. loadYandexImage("https://avatars.mds.yandex.net/get-afishanew/35821/e2a1ae77cd2b19ae9cbe6271b3d6ce5c/960x500", __DIR__);
Add Comment
Please, Sign In to add comment