源码
<?php
namespace app\tools;
/**
* 图像处理
* @author by zsc for 2010.03.24
*/
class ImageMark
{
# 图片上传格式
public static $IMG_EXT = ["png", "jpg", "jepg", "jpeg", "gif"];
/**
* [baseAvatar base64图片流上传]
* @param [type] $base64_string [description]
* @return [type] [description]
*/
public static function uploadBase64($base64_string, $pathmm = 'base64')
{
if ($base64_string && $pathmm) {
# 截取data:image/png;base64, 这个逗号后的字符
$base64_string = explode(',', $base64_string);
if ($base64_string) {
# 获取图像数据
$fileData = base64_decode($base64_string[1]);
# 获取图像后缀名
$ext = substr($base64_string[0], 11, (strrpos($base64_string[0], ";") - 11));
# 生成新图像
return self::markImgPath($ext, $pathmm, $fileData, 2);
}
}
return false;
}
/**
* 图片上传 单张上传
*
* @param [type] $file
* @param string $pathmm
* @return void
* @Author Afree 2020-06-18
*/
public static function uploadImgFile($file, $pathmm = 'images')
{
$fileMax = 1024 * 1024 * 1;
if ($file && $file['name'] && $file["size"] > 0 && $file["size"] < $fileMax && $pathmm) {
//拆分获取图片名
$ext = explode(".", $file['name']);
//取图片的后缀名
$ext = $ext[1];
//获取图像数据
$fileData = $file['tmp_name'];
# 生成新图像
return self::markImgPath($ext, $pathmm, $fileData, 1);
}
return false;
}
/**
* 合并图像处理
*
* @param [type] $ext
* @param string $pathmm
* @return void
* @Author Afree 2020-06-17
*/
public static function markImgPath($ext, $pathmm = 'images', $fileData, $type = 1)
{
if (in_array($ext, self::$IMG_EXT) && $fileData) {
$month = date('Ym', time());
$day = date('Ymd_His', time()) . '_' . rand(10, 99);
# 新文件名
$newFileName = "{$day}.{$ext}";
# 新文件保存地址
$newFilePath = PUBLIC_PATH . "uploads/{$pathmm}/{$month}/";
# 路径不存在时去创建
if ($newFilePath && !is_dir($newFilePath)) {
mkdir($newFilePath, 0777, true);
}
if ($type == 1) {
// 保存数据内容 file上传传数据
$re = move_uploaded_file($fileData, $newFilePath . $newFileName);
} elseif ($type == 2) {
# 保存数据内容 base64
$re = file_put_contents($newFilePath . $newFileName, $fileData);
}
if ($re) {
# 返回地址
return "/uploads/{$pathmm}/{$month}/$newFileName";
}
}
return false;
}
/**
* 将pdf文件转化为多张png图片
* @param string $pdf pdf所在路径 (/www/pdf/abc.pdf pdf所在的绝对路径)
* @param string $path 新生成图片所在路径 (/www/pngs/)
*
* @return array|bool
*/
public static function pdfToPngAll($pdf, $path = '')
{
$path = PUBLIC_PATH . "uploads/pdf/";
if (!extension_loaded('imagick')) {
return false;
}
if (!file_exists($pdf)) {
return false;
}
$im = new \Imagick();
$im->setResolution(120, 120); //设置分辨率 值越大分辨率越高
$im->setCompressionQuality(100);
$im->readImage($pdf);
foreach ($im as $k => $v) {
$v->setImageFormat('png');
$fileName = $path . md5($k . time()) . '.png';
if ($v->writeImage($fileName) == true) {
$return[] = $fileName;
}
}
return $return;
}
/**
* 将pdf转化为单一png图片
* @param string $pdf pdf所在路径 (/www/pdf/abc.pdf pdf所在的绝对路径)
* @param string $path 新生成图片所在路径 (/www/pngs/)
*
* @throws Exception
*/
public static function pdfToPngOne($pdf, $path = '')
{
$pdf = PUBLIC_PATH . "uploads/pdf/11.pdf";
$path = PUBLIC_PATH . "uploads/pdf/";
try {
$im = new \Imagick();
$im->setCompressionQuality(100);
$im->setResolution(120, 120); //设置分辨率 值越大分辨率越高
$im->readImage($pdf);
$canvas = new \Imagick();
$imgNum = $im->getNumberImages();
//$canvas->setResolution(120, 120);
foreach ($im as $k => $sub) {
$sub->setImageFormat('png');
//$sub->setResolution(120, 120);
$sub->stripImage();
$sub->trimImage(0);
$width = $sub->getImageWidth() + 10;
$height = $sub->getImageHeight() + 10;
if ($k + 1 == $imgNum) {
$height += 10;
} //最后添加10的height
$canvas->newImage($width, $height, new \ImagickPixel('white'));
$canvas->compositeImage($sub, \Imagick::COMPOSITE_DEFAULT, 5, 5);
}
$canvas->resetIterator();
$canvas->appendImages(true)->writeImage($path . microtime(true) . '.png');
} catch (Exception $e) {
throw $e;
}
}
}