单文件上传、远程保存、加水印、缩放、html中图片替换.php 0, 'info' = '参数不正确'));}/** * 远程多图保存,一行一个 * @param string $content 只包含图片地址和换行符的文本内容 * @param array
0, 'info' => '参数不正确'));
}
/**
* 远程多图保存,一行一个
* @param string $content 只包含图片地址和换行符的文本内容
* @param array $config 配置
* @return string 处理后的文本内容
*/
function urls($content, $config = array()){
$separator = strpos($content, "\r\n") === false ? "\n" : "\r\n";
$urls = explode($separator, $content);
$result = "";
if(!empty($urls)){
foreach ($urls as $v) {
$img = save($v, $config);
if ($img['status'] == 1) {
$result .= $img['info']. $separator;
}
}
}
return !empty($result) ? $result : $content;
}
/**
* ubb解析多图替换
* @param string $content 文本内容
* @param array $config 配置
* @return string 替换后的文本内容
*/
function ubb($content, $config = array()) {
// 匹配图片的src
preg_match_all('#\[img\](.+?)\[/img\]#i', $content, $match);
foreach ($match[1] as $imgurl) {
if (strpos($imgurl, 'http') === false) {
continue;
}
$img = save($imgurl, $config);
if ($img['status'] == 1) {
$content = str_replace('[img]'.$imgurl.'[/img]', $img['info'], $content);
}
}
return $content;
}
/**
* 替换html内容中图片地址
* @param string $content html内容
* @param array $config 配置
* @return string 替换后的html内容
*/
function html($content, $config = array()) {
// 匹配图片的src
preg_match_all('#
]*>#i', $content, $match);
foreach ($match[1] as $imgurl) {
if (strpos($imgurl, 'http') === false) {
continue;
}
$img = save($imgurl, $config);
if ($img['status'] == 1) {
$content = str_replace($imgurl, $img['info'], $content);
}
}
return $content;
}
/**
* 单图片上传
* @param object $file $_FILES['attach']
* @param array $config 配置
* @return string 图片地址
*/
function upload($file, $config = array()) {
if ($file['error'] != 0) {
/**
* 1; 超过了文件大小php.ini中即系统设定的大小。
* 2; 超过了文件大小MAX_FILE_SIZE 选项指定的值。
* 3; 文件只有部分被上传。
* 4; 没有文件被上传。
* 5; 上传文件大小为0。
*/
return array('status' => -$file['error'], 'info' => '图片上传失败');
}
$path = isset($config['path']) ? $config['path'] : date('Y/m/d');
$path = rtrim(ltrim($path, '/'), '/');
$path .= '/';
$path = 'upload/' . $path;
if (!file_exists($path)) {
@mkdir($path, 0777, true); //原图路径
}
$tempf = $file['tmp_name'];
$name = $file['name'];
$file = $path . $name;
if (file_exists($file)) {
$info = pathinfo($file);
$file = $path . getRandName($info['filename']) . '.' . strtolower($info['extension']);
}
$ext = getExt($file);
if (!in_array($ext, array('jpg', 'png', 'gif', 'bmp'))) {
return array('status' => 0, 'info' => '只能上传图片');
}
$true_ext = function_exists("finfo_open") ? getExtensionByMime($tempf) : getExtensionByCode($tempf);
if ($true_ext != $ext) {
return array('status' => 0, 'info' => '图片格式不正确');
}
@move_uploaded_file($tempf, $file);
$tmp = false;
if (isset($config['text']) || isset($config['water']) || isset($config['width']) || isset($config['height'])) {
$tmp = imageHandle($file, $config);
}
if ($tmp != false && !empty($tmp) && $tmp != $file) {
@unlink($file);
$file = $tmp;
}
return array('status' => 1, 'info' => 'http://' . $_SERVER['HTTP_HOST'] . '/' . $file);
}
/**
* 保存单张远程图片
* @param string $url 图片地址
* @param array $config 配置
* @return string 图片地址
*/
function save($url, $config = array()) {
$url = trim(urldecode($url));
$path = isset($config['path']) ? $config['path'] : date('Y/m/d');
$path = rtrim(ltrim($path, '/'), '/');
$path .= '/';
$path = 'upload/' . $path;
if (!file_exists($path)) {
@mkdir($path, 0777, true); //原图路径
}
if (strpos($url, "://") === false) {
return array('status' => 0, 'info' => '图片下载地址不正确');
}
$file = $path . pathinfo($url, PATHINFO_BASENAME);
if (file_exists($file)) {
$info = pathinfo($file);
$file = $path . getRandName($info['filename']) . '.' . strtolower($info['extension']);
}
$file = httpDown($url, $file);
if ($file === false) {
return array('status' => 0, 'info' => '图片下载失败');
}
$ext = getExt($file);
if (!in_array($ext, array('jpg', 'png', 'gif', 'bmp'))) {
@unlink($file);
return array('status' => 0, 'info' => '只能下载图片');
}
$true_ext = function_exists("finfo_open") ? getExtensionByMime($file) : getExtensionByCode($file);
if ($true_ext != $ext) {
@unlink($file);
return array('status' => 0, 'info' => '图片格式不正确');
}
$tmp = false;
if (isset($config['text']) || isset($config['water']) || isset($config['width']) || isset($config['height'])) {
$tmp = imageHandle($file, $config);
}
if ($tmp != false && !empty($tmp) && $tmp != $file) {
@unlink($file);
$file = $tmp;
}
return array('status' => 1, 'info' => 'http://' . $_SERVER['HTTP_HOST'] . '/' . $file);
}
/**
* 图片处理,包括水印和缩放,使用ThinkImage类实现
* @param string $file 图片地址
* @param array $config 配置
* @return string 处理后的图片地址
*/
function imageHandle($file, $config = array()) {
include_once 'ThinkImage/ThinkImage.php';
$img = new ThinkImage(THINKIMAGE_GD, $file);
if (isset($config['text'])) {
$font = isset($config['font']) ? $config['font'] : 'mysh.ttf';
$size = isset($config['size']) ? $config['size'] : '12';
$color = isset($config['color']) ? $config['color'] : '#00000000';
$locate = isset($config['locate']) ? $config['locate'] : THINKIMAGE_WATER_SOUTHEAST;
$offset = isset($config['offset']) ? $config['offset'] : 0;
$angel = isset($config['angel']) ? $config['angel'] : 0;
$img->text($config['text'], $font, $size, $color, $locate, $offset, $angel);
}
if (isset($config['water'])) {
if (!file_exists($config['water'])) {
return false;
}
$locate = isset($config['locate']) ? $config['locate'] : THINKIMAGE_WATER_SOUTHEAST;
$img->water($config['water'], $locate);
}
if (isset($config['width'])) {
$width = $config['width'];
$height = isset($config['height']) ? $config['height'] : $config['width'];
$img->thumb($width, $height);
}
;
if (isset($config['rename'])) {
$info = pathinfo($file);
$path = $info['dirname'];
$file = $path . '/' . $config['rename'];
$cover = isset($config['cover']) ? $config['cover'] : true;
if (file_exists($file) && !$cover) {
$file = $path . '/' . getRandName($config['rename']) . '.' . strtolower($info['extension']);
}
}
$img->save($file);
return $file;
}
/**
* 获取随机文件名
* @param string $name 文件名前缀
* @return string md5后的文件名
*/
function getRandName($name='') {
return md5(uniqid(rand()) . $name);
}
/**
* 使用pathinfo获得扩展名
* @param string $file 图片文件名
* @return string 扩展
*/
function getExt($file) {
return strtolower(pathinfo($file, PATHINFO_EXTENSION));
}
/**
* 读取mime获得扩展名
* @param string $file 图片文件名
* @return string 扩展名
*/
function getExtensionByMime($file) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeTipe = finfo_file($finfo, $file);
finfo_close($finfo);
$mimes = array('image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/bmp' => 'bmp', 'image/png' => 'png');
return isset($mimeTipe[$mimes]) ? strtolower($mimeTipe[$mimes]) : "";
}
function getExtensionByCode($file) {
$tempfile = @fopen($file, "rb");
$bin = fread($tempfile, 2); //只读2字节
fclose($tempfile);
$strInfo = @unpack("C2chars", $bin);
$typeCode = intval($strInfo['chars1'] . $strInfo['chars2']);
$fileType = '';
switch ($typeCode) {
// 6677:bmp 255216:jpg 7173:gif 13780:png 7790:exe 8297:rar 8075:zip tar:109121 7z:55122 gz 31139
case '255216':
$fileType = 'jpg';
break;
case '7173':
$fileType = 'gif';
break;
case '13780':
$fileType = 'png';
break;
case '6677':
$fileType = 'bmp';
break;
default:
$fileType = '';
}
return $fileType;
}
function httpDown($url, $file = "", $timeout = 60) {
$file = empty($file) ? pathinfo($url, PATHINFO_BASENAME) : $file;
$dir = pathinfo($file, PATHINFO_DIRNAME);
!is_dir($dir) && @mkdir($dir, 0755, true);
$url = str_replace(" ", "%20", $url);
if (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$temp = curl_exec($ch);
if (@file_put_contents($file, $temp) && !curl_error($ch)) {
return $file;
} else {
return false;
}
} else {
$opts = array(
"http" => array(
"method" => "GET",
"header" => "",
"timeout" => $timeout),
);
$context = stream_context_create($opts);
if (@copy($url, $file, $context)) {
//$http_response_header
return $file;
} else {
return false;
}
}
}
