业务场景是微信小程序图片上传需要压缩原图 且保留原图(php5.6? ?thinkphp3? 无需引入其他扩展文件)按道理来说php其他版本以及框架都可用(我也没试过)
先上通用函数以及主函数(调用示例再最下面)
/**
* 图片压缩处理(主函数)
* @param string $sFile 源图片路径
* @param int $iWidth 自定义图片宽度
* @param int $iHeight 自定义图片高度
* @return string 压缩后的图片路径
*/
protected function getThumb($sFile,$iWidth,$iHeight){
//图片公共路径
$public_path = '';
//判断该图片是否存在
if(!file_exists($public_path.$sFile)) return $sFile;
//判断图片格式(图片文件后缀)
$extend = explode("." , $sFile);
$attach_fileext = strtolower($extend[count($extend) - 1]);
if (!in_array($attach_fileext, array('jpg','png','jpeg'))){
return false;
}
//压缩图片文件名称
$sFileNameS = str_replace(".".$attach_fileext, "_".$iWidth.'_'.$iHeight.'.'.$attach_fileext, $sFile);
//判断是否已压缩图片,若是则返回压缩图片路径
if(file_exists($public_path.$sFileNameS)){
return $sFileNameS;
}
//生成压缩图片,并存储到原图同路径下
//
$this->resizeImage($public_path.$sFile, $public_path.$sFileNameS, $iWidth, $iHeight);
$this->watermark($public_path.$sFileNameS,$public_path.$sFileNameS);
// $this->resizeImage($public_path.$sFileNameS, $public_path.$sFileNameS, $iWidth, $iHeight);
if(!file_exists($public_path.$sFileNameS)){
return $sFile;
}
return $sFileNameS;
}
/**
* 创建图片水印
* @param string $dst_path 图片
* @param string $src_path 水印图片
*/
public function watermark($dst_path,$bc_path){
$configs = D('Config')->where(array('name' => array('like', '_' . strtoupper('Picture') . '_' . '%')))->limit(999)->select();
$data = array();
foreach ($configs as $k => $v) {
$key = str_replace('_' . strtoupper('Picture') . '_', '', strtoupper($v['name']));
$data[$key] = $v['value'];
}
if($data['WATER_OPEN']!=1){
return false;
}
$src_path ='http://172.0.0.83'.$data['WATER_IMAGE'];
//创建图片的实例
$dst = imagecreatefromstring(file_get_contents($dst_path));
$src = imagecreatefromstring(file_get_contents($src_path));
//获取水印图片的宽高
list($src_w, $src_h) = getimagesize($src_path);
//将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果
$space = $this->get_water_postion($src_path,$dst_path,$data['WATER_SPACE']);
$porintLeft = $space['porintLeft'];
$pointTop= $space['pointTop'];
// imagecopymerge($dst, $src, $porintLeft, $pointTop,0 , 0, $src_w, $src_h, 20);
//如果水印图片本身带透明色,则使用imagecopy方法
imagecopy($dst, $src, $porintLeft, $pointTop, 0, 0, $src_w, $src_h);
//输出图片
list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
switch ($dst_type) {
case 1://GIF
header('Content-Type: image/gif');
$res = imagegif($dst,$bc_path);
break;
case 2://JPG
header('Content-Type: image/jpeg');
$res = imagejpeg($dst,$bc_path);
break;
case 3://PNG
header('Content-Type: image/png');
$res = imagepng($dst,$bc_path);
break;
default:
break;
}
return $res;
}
/**
* 生成图片
* @param string $im 源图片路径
* @param string $dest 目标图片路径
* @param int $maxwidth 生成图片宽
* @param int $maxheight 生成图片高
*/
protected function resizeImage($im, $dest, $maxwidth, $maxheight) {
$img = getimagesize($im);
switch ($img[2]) {
case 1:
$im = @imagecreatefromgif($im);
break;
case 2:
$im = @imagecreatefromjpeg($im);
break;
case 3:
$im = @imagecreatefrompng($im);
break;
}
$pic_width = imagesx($im);
$pic_height = imagesy($im);
$resizewidth_tag = false;
$resizeheight_tag = false;
if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
if ($maxwidth && $pic_width > $maxwidth) {
$widthratio = $maxwidth / $pic_width;
$resizewidth_tag = true;
}
if ($maxheight && $pic_height > $maxheight) {
$heightratio = $maxheight / $pic_height;
$resizeheight_tag = true;
}
if ($resizewidth_tag && $resizeheight_tag) {
if ($widthratio < $heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}
if ($resizewidth_tag && !$resizeheight_tag)
$ratio = $widthratio;
if ($resizeheight_tag && !$resizewidth_tag)
$ratio = $heightratio;
$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;
if (function_exists("imagecopyresampled")) {
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
} else {
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
}
imagejpeg($newim, $dest);
imagedestroy($newim);
} else {
imagejpeg($im, $dest);
}
}
/**
* 获取水印位置
*/
protected function get_water_postion($src_path,$dst_path,$point){
list($textWidth, $lineHeight) = getimagesize($src_path);
list($imgWidth, $imgHeight) = getimagesize($dst_path);
$textWidth = (float)$textWidth;
$lineHeight = (float)$lineHeight;
$imgWidth = (float)$imgWidth;
$imgHeight = (float)$imgHeight;
if ($point == 1) { //左上角
$porintLeft = 0;
$pointTop = 0;
} elseif ($point == 2) { //上中部
$porintLeft = floor(($imgWidth - $textWidth) / 2);
$pointTop = 0;
} elseif ($point == 3) { //右上部
$porintLeft = $imgWidth - $textWidth;
$pointTop = 0;
} elseif ($point == 4) { //左中部
$porintLeft = 0;
$pointTop = floor(($imgHeight - $lineHeight) / 2);
} elseif ($point == 5) { //正中部
$porintLeft = floor(($imgWidth - $textWidth) / 2);
$pointTop = floor(($imgHeight - $lineHeight) / 2);
} elseif ($point == 6) { //右中部
$porintLeft = $imgWidth - $textWidth;
$pointTop = floor(($imgHeight - $lineHeight) / 2);
} elseif ($point == 7) { //左下部
$porintLeft = 0;
$pointTop = $imgHeight -$lineHeight;
} elseif ($point == 8) { //中下部
$porintLeft = floor(($imgWidth - $textWidth) / 2);
$pointTop = $imgHeight - $lineHeight ;
} elseif ($point == 9) { //右下部
$porintLeft = $imgWidth - $textWidth ;
$pointTop = $imgHeight - $lineHeight ;
}
return array('porintLeft'=>$porintLeft,'pointTop'=>$pointTop);
}
下面是我自己的接口示例
public function save_weibo()
{
if (checkPost()) {
//接收前端参数
$aTitle = I('post.title', '', 'string');
$uid = I('post.user_id', '', 'intval');
$aContent = I('post.content', '', 'string');
$belong = I('post.cate_id', '', 'intval');
$category = I('post.category');
$images = I('post.images', '', 'string');
$address = I('post.address','','string');
//敏感词过滤
$re = $this->sensitive_words($aContent);
$res = $this->sensitive_words($aTitle);
if($re==false||$res==false){
errorReturn('存在敏感词汇,禁止发送');
}
//我这里的图片是先上传原图,返回给前端id;这里拿id换图片路径;如果是路径则不需要此处
$where['id'] = array('in', explode(',', $images));
$img = M('Picture')->where($where)->getField('path', true);//获取图片路径->数据库
//循环图片(我是多图片所以是用循环)
foreach ($img as $k=>$v){
$re = substr($v, strripos($v, "Uploads/"));//源文件路径->去除域名
$this->getThumb($re,1000,1000);//压缩图片
}
// 执行发布,写入数据库
$weibo_id = send_wb($uid, $aTitle, $aContent, $category, $images, $belong,$address);
if (!$weibo_id) {
errorReturn(L('_FAIL_PUBLISH_'));
}
$result['status'] = 1;
$result['msg'] = 'success';
$result['data'] = [L('_SUCCESS_PUBLISH_') . L('_EXCLAMATION_')];
successReturn($result);
}
}
?
|