|
|
<?php
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
use GuzzleHttp;
|
|
|
use GuzzleHttp\Cookie\CookieJar;
|
|
|
use function Zend\Diactoros\parseCookieHeader;
|
|
|
|
|
|
|
|
|
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);
|
|
|
// $cookieStr = "__cfduid=d959b19dbaa25a5fac7c5fe1d3988d15e1610778634; frontend=36ab879e6d75ae4e19e4843a1bdf2e45; _gid=GA1.2.22159331.1610804846; __cf_bm=19f3172d7b8d3c97ce28f86ec56af4da3c3560a2-1610805781-1800-Adr3Ph9l2oeHE9Ms+YrTrKfE1uwMO7Tpcw/WxfRrPldspHy/AcwPovSpDcrz+DE3UHWhrw60voIaOsPy3VvpvTV9fjU9F1Hk3y1U5O6V1RZeclh6+YoIpZc1UfRyixPrKw==; frontend-rmu=qUl6oIQSib76RH6A4ZJGALhooK63; frontend-rmt=3d1i%2F0DU5cuBB%2BII5MzosWl0MPonPRchZyJyhL73PzaHWBzoq9RFDWJG%2FDPtCncI; _ga_170M3FX3HZ=GS1.1.1610804845.1.1.1610806191.0; _ga=GA1.2.6396742.1610804846; _gat_UA-140713725-1=1";
|
|
|
// $cookieArr = parseCookieHeader($cookieStr);
|
|
|
// $cookieJar = CookieJar::fromArray($cookieArr, ".v2ph.com");
|
|
|
$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,
|
|
|
// 'cookies' => $cookieJar
|
|
|
]]);
|
|
|
$fileParts = pathinfo($imageUrl);
|
|
|
$completeFile = $dir . "/" . $albumName . "-" . $fileParts["filename"] . "." . $fileParts["extension"];
|
|
|
$client->request('GET', $imageUrl, ['sink' => $completeFile]);
|
|
|
}
|
|
|
|
|
|
public static function mergeImage($path, $picList)
|
|
|
{
|
|
|
$imgInfo = array();//图片信息
|
|
|
$tmp = 1;
|
|
|
$width = 720;//生成的图像宽度
|
|
|
$picCount = count($picList);//图片总数
|
|
|
$imgInfo['width'] = $width;
|
|
|
$imgInfo['height'] = 0;
|
|
|
//首先等比例缩放,让所有图片宽度一样,计算出图片等比例缩放后的长和宽
|
|
|
for ($i = 0; $i < $picCount; $i++) {
|
|
|
usleep(100);
|
|
|
$imgInfo[$i]['url'] = $picList[$i]['src'];
|
|
|
$imgInfo[$i] = getimagesize($imgInfo[$i]['url']);
|
|
|
$tmp = ($imgInfo[$i][0]) / $width;
|
|
|
$imgInfo[$i]['width'] = $width;
|
|
|
$imgInfo[$i]['height'] = ($imgInfo[$i][1]) / $tmp;
|
|
|
$imgInfo[$i]['url'] = $picList[$i]['src'];
|
|
|
$imgInfo[$i]['x'] = 0;
|
|
|
$imgInfo[$i]['y'] = $imgInfo['height'];
|
|
|
$imgInfo['height'] += $imgInfo[$i]['height'];
|
|
|
|
|
|
}
|
|
|
$new_img = ImageCreateTrueColor($imgInfo['width'], $imgInfo['height']); // 创建一个画布,作为拼接后的图片
|
|
|
for ($i = 0; $i < $picCount; $i++) {
|
|
|
usleep(100);
|
|
|
//以下的三行代码 缩放原图
|
|
|
$img_r = imagecreatefromjpeg($imgInfo[$i]['url']); // 获取原图
|
|
|
$dst_r = ImageCreateTrueColor($imgInfo[$i]['width'], $imgInfo[$i]['height']); // 获取新图
|
|
|
imagecopyresampled($dst_r, $img_r, 0, 0, 0, 0,
|
|
|
$imgInfo[$i]['width'], $imgInfo[$i]['height'], $imgInfo[$i][0], $imgInfo[$i][1]);
|
|
|
|
|
|
//把缩放后的图片放在画布上
|
|
|
imagecopyresampled($new_img, $dst_r, $imgInfo[$i]['x'], $imgInfo[$i]['y'], 0, 0,
|
|
|
$imgInfo['width'], $imgInfo[$i]['height'], $imgInfo[$i]['width'], $imgInfo[$i]['height']);
|
|
|
|
|
|
imagedestroy($dst_r);//销毁掉缩放的图片
|
|
|
}
|
|
|
//获取时间戳,以时间戳的名字存放
|
|
|
$name = strval(time());
|
|
|
$img_path = $path . $name . ".jpg";
|
|
|
//存放拼接后的图片到本地
|
|
|
imagejpeg($new_img, $img_path);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|