const BUFSIZE = 1024;
const SHARE_IMG = "share_logo.jpg";
public function download($src) {
$url = parse_url($src);
$host = $url['host'];
$protocol = $url['scheme']; // http, https
$uri = $url['path'];
if (0==strncmp($protocol, "https", 5)) {
$port = 443;
} else {
$port = 80;
}
$socket = fsockopen($host, $port, $errno, $errMsg);
if (!$socket) {
return ["errno" => $errno, "errMsg" => $errMsg, "data" => null];
}
fwrite($socket, sprintf("GET %s HTTP/1.1\r\n", $uri));
fwrite($socket, "Host: ".$host."\r\n");
fwrite($socket, "Connection: keep-alive\n\n");
fwrite($socket, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\r\n");
fwrite($socket, "Accept-Encoding: gzip, deflate\r\n");
fwrite($socket, "User-Agent: Socket_Backstage\r\n");
fwrite($socket, "\r\n\r\n");
// jump over http headers
$header = "";
while ($str = trim(fgets($socket, self::BUFSIZE))) {
$header .= $str."\r\n";
}
Log::info($header);
$dir = self::getDir();
$path = $dir."/".self::SHARE_IMG;
$outFile = fopen($path, "w");
$bytesWritten = 0;
while (!feof($socket)) {
$s = fread($socket, self::BUFSIZE);
$bytesWritten += fwrite($outFile, $s, strlen($s));
}
fclose($socket);
fclose($outFile);
return ["errno" => $errno, "errMsg" => $errMsg, "data" => ['bytesWritten' => $bytesWritten]];
}
private static function getDir() {
$dir = app_path().'/../storage/app';
$absDir = Str::absPath($dir);
if (!is_dir($absDir)) {
unlink($absDir);
}
if (!file_exists($absDir)) {
mkdir($absDir, 0755);
}
return $absDir;
}
public function image() {
$dir = self::getDir();
$img = $dir."/".self::SHARE_IMG;
$info = getimagesize($img);
// 获取文件后缀
$imgExt = image_type_to_extension($info[2], false);
$fun = "imagecreatefrom".$imgExt;
// 1.由文件或 URL 创建一个新图象。如:imagecreatefrompng ( string $filename )
$imgInfo = $fun($img);
//$mime = $info['mime'];
$mime = image_type_to_mime_type(exif_imagetype($img)); //获取图片的 MIME 类型
header('Content-Type:'.$mime);
$quality = 100;
// 输出质量,JPEG格式(0-100),PNG格式(0-9)
if($imgExt == 'png') $quality = 9;
$getImgInfo = "image{$imgExt}";
//2.将图像输出到浏览器或文件。如: imagepng ( resource $image )
$getImgInfo($imgInfo, null, $quality);
imagedestroy($imgInfo);
}
路径转化