Commit 7742ffca authored by fuyunnan's avatar fuyunnan

Merge branch 'feature-webp-download'

parents 042a557f ef2323f6
...@@ -83,6 +83,104 @@ class Drawer ...@@ -83,6 +83,104 @@ class Drawer
return $url; return $url;
} }
/**
* description:下载文件到服务端 增加判断是否是webp 格式的图片处理
* author: fuyunnan
* @param string $url
* @param string $path 文件路径
* @return string
* @throws
* Date: 2020/11/20
*/
public function downLoadImgWebpChannel($url, $path = '')
{
//如果shopify 去掉版本号
if (strpos($url, 'cdn.shopify.com') !== false) {
$url = substr($url, 0, strpos($url, '?v='));
}
$savePath = $this->rootPath . '/download/images/' . ($path ? $path . '/' : $this->savePath);
create_file_dir($savePath);
$filePath = $this->toWebpImg($url, $savePath);
if (!file_exists($filePath)) {
$filename = trim(pathinfo($url, PATHINFO_FILENAME));
$ext = strtolower(pathinfo($url, PATHINFO_EXTENSION));
$filePath = $savePath . "$filename.$ext";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$file = curl_exec($ch);
curl_close($ch);
$resource = fopen($filePath, 'a');
fwrite($resource, $file);
fclose($resource);
$filePath = $this->toWebpImg($filePath, $savePath);
}
return $filePath;
}
/**
* description:将图像绘到excel表格上
* author: fuyunnan
* @param string $url 图片地址
* @param string $childPath 图片地址 用sku名作为文件夹名称
* @param int $h 图片高度
* @param string $p 单元格索引
* @param Worksheet $sheet
* @return void
* @throws
* Date: 2020/11/23
*/
public function drawImgExcel($url, $childPath, $h, $p, $sheet)
{
try {
//下载图片到本地
$absPath = $this->downLoadImgWebpChannel($url, $childPath);
$drawing = new Drawing();
$drawing->setPath($absPath)
->setCoordinates($p)
->setHeight($h)
->setOffsetX(1)
->setOffsetY(1)
->setWorksheet($sheet);
} catch (Exception $e) {
put_log($e->getMessage(), 'draw2Excel.log');
}
}
/**
* description:检查文件是否是webp格式的文件
* 如果是,转化为jpeg格式的文件,并且返回文件的绝对地址.
* author: fuyunnan
* @param string $filePath 文件的绝对地址
* @param string $savePath 文件重新保存的地址
* @return string
* @throws
* Date: 2020/11/23
*/
private function toWebpImg($filePath, $savePath)
{
$filename = trim(pathinfo($filePath, PATHINFO_FILENAME));
$ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
$imgInfo = '';
//如果存在直接返回 防止每次获取文件资源
if (file_exists($savePath . "$filename.$ext")) {
return $savePath . "$filename.$ext";
}
$filePath && $imgInfo = getimagesize($filePath);
if ($imgInfo && end($imgInfo) == "image/webp") {
$filename = trim(pathinfo($filePath, PATHINFO_FILENAME));
$im = imagecreatefromwebp($filePath); // 加载 WebP 文件
imagejpeg($im, $savePath . $filename . '.jpeg', 100); // 以 100% 的质量转换成 jpeg 格式
imagedestroy($im);
return $savePath . $filename . '.jpeg';
}
return $savePath . "$filename.$ext";
}
/** /**
* @param $imgPath * @param $imgPath
* @param int $px * @param int $px
...@@ -230,7 +328,7 @@ class Drawer ...@@ -230,7 +328,7 @@ class Drawer
$drawing = new Drawing(); $drawing = new Drawing();
$drawing->setPath($path)->setCoordinates($p)->setHeight($h)->setOffsetX(1)->setOffsetY(1)->setWorksheet($sheet); $drawing->setPath($path)->setCoordinates($p)->setHeight($h)->setOffsetX(1)->setOffsetY(1)->setWorksheet($sheet);
} catch (Exception $e) { } catch (Exception $e) {
put_log($e->getMessage()); put_log($e->getMessage(), 'draw2Excel.log');
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment