You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

27 lines
825 B

<?php
namespace App\Services;
use GuzzleHttp;
class CommonService
{
public static function downloadImage($dir, $imageUrl)
{
if (!file_exists($dir)) {
mkdir($dir);
}
$host = parse_url($imageUrl, PHP_URL_HOST);
$dirList = explode("/", $dir);
$albumName = array_pop($dirList);
$client = new GuzzleHttp\Client(['headers' => [
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
'Referer' => $host
]]);
$fileParts = pathinfo($imageUrl);
$completeFile = $dir . "/" . $albumName . "-" . $fileParts["filename"] . "." . $fileParts["extension"];
$client->request('GET', $imageUrl, ['sink' => $completeFile]);
}
}