php识别图片是否模糊,php图片模糊代码 可做API

代码可用于做缩略图之类的,需要就看一下

在线测试http://tool.ayangw.com/imgblur/api.php?img=图片地址

把img=后面的链接 改成目标图片地址

还可以加入 blur参数(取值1-5),设置图片的模糊程度http://tool.ayangw.com/imgblur/api.php?img=图片地址&blur=3

效果展示

模糊之前:

9296916d23c480fd0a7eea1bae2dcd33.png

模糊之后:

e63dbf108912477313285a9ed5678961.png

代码(创建一个php文件,复制进去,就可以了)<?php

/*

邻恩博客 / www.0en.cn

*/

class image_blur

{

public function gaussian_blur($srcImg, $blurFactor = 3, $savepath = null, $savename = null)

{

$gdImageResource = $this->image_create_from_ext($srcImg);

$srcImgObj = $this->blur($gdImageResource, $blurFactor);

$temp = pathinfo($srcImg);

$name = $temp['basename'];

$path = $temp['dirname'];

$exte = $temp['extension'];

$savename = $savename ? $savename : $name;

$savepath = $savepath ? $savepath : $path;

$savefile = $savepath . '/' . $savename;

$srcinfo = @getimagesize($srcImg);

switch ($srcinfo[2]) {

case 1:

header("Content-type: image/gif");

imagegif($srcImgObj);

//imagegif($srcImgObj, $savefile);

break;

case 2:

header("Content-type: image/jpeg");

imagejpeg($srcImgObj);

//imagejpeg($srcImgObj, $savefile);

break;

case 3:

header("Content-type: image/png");

imagepng($srcImgObj);

//imagepng($srcImgObj, $savefile);

break;

default:

return '保存失败'; //保存失败

}

return $savefile;

imagedestroy($srcImgObj);

}

private function blur($gdImageResource, $blurFactor = 3)

{

$blurFactor = round($blurFactor);

$originalWidth = imagesx($gdImageResource);

$originalHeight = imagesy($gdImageResource);

$smallestWidth = ceil($originalWidth * pow(0.5, $blurFactor));

$smallestHeight = ceil($originalHeight * pow(0.5, $blurFactor));

$prevImage = $gdImageResource;

$prevWidth = $originalWidth;

$prevHeight = $originalHeight;

for ($i = 0; $i < $blurFactor; $i += 1) {

$nextWidth = $smallestWidth * pow(2, $i);

$nextHeight = $smallestHeight * pow(2, $i);

$nextImage = imagecreatetruecolor($nextWidth, $nextHeight);

imagecopyresized($nextImage, $prevImage, 0, 0, 0, 0,

$nextWidth, $nextHeight, $prevWidth, $prevHeight);

imagefilter($nextImage, IMG_FILTER_GAUSSIAN_BLUR);

$prevImage = $nextImage;

$prevWidth = $nextWidth;

$prevHeight = $nextHeight;

}

imagecopyresized($gdImageResource, $nextImage,

0, 0, 0, 0, $originalWidth, $originalHeight, $nextWidth, $nextHeight);

imagefilter($gdImageResource, IMG_FILTER_GAUSSIAN_BLUR);

imagedestroy($prevImage);

return $gdImageResource;

}

private function image_create_from_ext($imgfile)

{

$info = getimagesize($imgfile);

$im = null;

switch ($info[2]) {

case 1:

$im = imagecreatefromgif($imgfile);

break;

case 2:

$im = imagecreatefromjpeg($imgfile);

break;

case 3:

$im = imagecreatefrompng($imgfile);

break;

}

return $im;

}

}

$img = $_GET['img'];

$blur = intval($_GET['blur']);

if($blur > 5 || $blur < 1){

$blur = 1;

}

if(!isImage($img)){

exit("图片异常");

}

$image_blur = new image_blur();

$image_blur->gaussian_blur($img, $blur);

function isImage($name){

$type = strtolower(substr($name, strrpos($name, '.') + 1)); //得到文件类型,并且都转化成小写

$allow_type = array('jpg', 'jpeg', 'gif', 'png'); //定义允许上传的类型

//判断文件类型是否被允许上传

if (!in_array($type, $allow_type)) {

return 0;

}else{

return 1;

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值