php图片裁剪
2021/04/12
<?php
namespace core\lib;
class img{
/***输出图片,调整大小
目标w/h比除以图片w/h比大于1,把图片的w设为目标的w,然后把绘画位置设为【(缩放后的图片的h-目标的h)除以2的负数】;
目标w/h比除以图片w/h比小于等于1,把图片的h设为目标的h,然后把绘画位置设为【(缩放后的图片的w-目标的w)除以2的负数】;
注意:
Warning: fopen() [function.fopen]: Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?
解决方案:
1.windows下的PHP,只需要到php.ini中把extension=php_openssl.dll前面的;删掉,重启服务就可以了。
2.linux下的PHP,就必须安装openssl模块,安装好了以后就可以访问了。
3.如果服务器你不能修改配置的话,那么就使用curl函数来替代file_get_contents函数,当然不是简单的替换啊。还有相应的参数配置才能正常使用curl函数。
***/
public static function cutimg($imgurl,$w,$h,$imgfile=false){
//类似css的background-size:cover效果
//$imgurl源图片地址,$w目标图片的宽度,$h目标文件的高度,$imgfile目标文件的文件夹
//如果没有$imgfile,则直接输出到图片到浏览器
if($imgfile!==false){
$imgfilename=$imgfile.date("Ymdhis").substr(str_shuffle("abcdefghigklmnopqrstuvwxyz"),0,4);
};
$imgrst=imagecreatetruecolor($w,$h);
// $color=imagecolorallocate($imgrst,255,255,255);
// imagecolortransparent($imgrst,$color);
imagealphablending($imgrst,false);
imagesavealpha($imgrst,true);
$imagesrcInf=getimagesize($imgurl);
switch ($imagesrcInf[2]) {
case 1:
$imagesrc=@imagecreatefromgif($imgurl);
break;
case 2:
$imagesrc=@imagecreatefromjpeg($imgurl);
break;
case 3:
$imagesrc=@imagecreatefrompng($imgurl);
break;
default:
echo 'Image type error!';
break;
exit(0);
};
if($w/$h*$imagesrcInf[1]/$imagesrcInf[0]>1){
$imgtemp=imagecreatetruecolor($w,$imagesrcInf[1]*$w/$imagesrcInf[0]);
// $color=imagecolorallocate($imgtemp,255,255,255);
// imagecolortransparent($imgtemp,$color);
imagealphablending($imgtemp,false);
imagesavealpha($imgtemp,false);
imagecopyresampled($imgtemp,$imagesrc,0,0,0,0,$w,$imagesrcInf[1]*$w/$imagesrcInf[0],$imagesrcInf[0],$imagesrcInf[1]);
imagecopy($imgrst,$imgtemp,0,0,0,($imagesrcInf[1]*$w/$imagesrcInf[0]-$h)/2,$w,$h);
}else{
$imgtemp=imagecreatetruecolor($imagesrcInf[0]*$h/$imagesrcInf[1],$h);
// $color=imagecolorallocate($imgtemp,255,255,255);
// imagecolortransparent($imgtemp,$color);
imagealphablending($imgtemp,false);
imagesavealpha($imgtemp,false);
imagecopyresampled($imgtemp,$imagesrc,0,0,0,0,$imagesrcInf[0]*$h/$imagesrcInf[1],$h,$imagesrcInf[0],$imagesrcInf[1]);
imagecopy($imgrst,$imgtemp,0,0,($imagesrcInf[0]*$h/$imagesrcInf[1]-$w)/2,0,$w,$h);
};
switch ($imagesrcInf[2]) {
case 1:
if($imgfile!==false){
imagegif($imgrst,$_SERVER['DOCUMENT_ROOT'].$imgfilename.".gif",null,9);
return $imgfilename.".gif";
}else{
header('Content-type: image/GIF');
imagegif($imgrst,null,9);
};
break;
case 2:
if($imgfile!==false){
imagejpeg($imgrst,$_SERVER['DOCUMENT_ROOT'].$imgfilename.".jpg",null,95);
return $imgfilename.".jpg";
}else{
header('Content-type: image/JPEG');
imagejpeg($imgrst,null,95);
};
break;
case 3:
if($imgfile!==false){
imagepng($imgrst,$_SERVER['DOCUMENT_ROOT'].$imgfilename.".png",null,9);
return $imgfilename.".png";
}else{
header('Content-type: image/PNG');
imagepng($imgrst,null,9);
};
break;
};
imagedestroy($imgrst);
imagedestroy($imgtemp);
imagedestroy($imagesrc);
}
public static function cutimgContain($imgurl,$w,$h,$bg=[255,255,255],$imgfile=false){
//https://blog.csdn.net/eadoucn/article/details/24725457
//类似css的background-size:contain效果
//$imgurl源图片地址,$w目标图片的宽度,$h目标文件的高度,$imgfile目标文件的文件夹
//如果没有$imgfile,则直接输出到图片到浏览器
if($imgfile!==false){
$imgfilename=$imgfile.date("Ymdhis").substr(str_shuffle("abcdefghigklmnopqrstuvwxyz"),0,4);
//$imgfilename = $imgfile;
};
$imgrst=imagecreatetruecolor($w,$h);
$color = imagecolorallocate($imgrst,$bg[0],$bg[1],$bg[2]); //填充背景色
imagefill( $imgrst, 0, 0, $color );
// imagecolortransparent($imgrst,$color);
imagealphablending($imgrst,false);
imagesavealpha($imgrst,true);
$imagesrcInf=getimagesize($imgurl);
switch ($imagesrcInf[2]) {
case 1:
$imagesrc=@imagecreatefromgif($imgurl);
break;
case 2:
$imagesrc=@imagecreatefromjpeg($imgurl);
break;
case 3:
$imagesrc=@imagecreatefrompng($imgurl);
break;
default:
echo 'Image type error!';
break;
exit(0);
};
if($w/$h*$imagesrcInf[1]/$imagesrcInf[0]<1){
$imgtemp=imagecreatetruecolor($w,$imagesrcInf[1]*$w/$imagesrcInf[0]);
//$color=imagecolorallocate($imgtemp,$bg[0],$bg[1],$bg[2]);
// imagefill($imgtemp, 0, 0, $color );
// imagecolortransparent($imgtemp,$color);
imagealphablending($imgtemp,false);
imagesavealpha($imgtemp,false);
imagecopyresampled($imgtemp,$imagesrc,0,0,0,0,$w,$imagesrcInf[1]*$w/$imagesrcInf[0],$imagesrcInf[0],$imagesrcInf[1]);
imagecopy($imgrst,$imgtemp,0,($h - $imagesrcInf[1]*$w/$imagesrcInf[0])/2,0,0,$w,$imagesrcInf[1]*$w/$imagesrcInf[0]);
}else{
$imgtemp=imagecreatetruecolor($imagesrcInf[0]*$h/$imagesrcInf[1],$h);
// $color=imagecolorallocate($imgtemp,255,255,255);
// imagecolortransparent($imgtemp,$color);
imagealphablending($imgtemp,false);
imagesavealpha($imgtemp,false);
imagecopyresampled($imgtemp,$imagesrc,0,0,0,0,$imagesrcInf[0]*$h/$imagesrcInf[1],$h,$imagesrcInf[0],$imagesrcInf[1]);
imagecopy($imgrst,$imgtemp,($w - $imagesrcInf[0]*$h/$imagesrcInf[1])/2,0,0,0,$imagesrcInf[0]*$h/$imagesrcInf[1],$h);
};
switch ($imagesrcInf[2]) {
case 1:
if($imgfile!==false){
imagegif($imgrst,$_SERVER['DOCUMENT_ROOT'].$imgfilename.".gif",null,9);
return $imgfilename.".gif";
}else{
header('Content-type: image/GIF');
imagegif($imgrst,null,9);
};
break;
case 2:
if($imgfile!==false){
imagejpeg($imgrst,$_SERVER['DOCUMENT_ROOT'].$imgfilename.".jpg",null,100);
return $imgfilename.".jpg";
}else{
header('Content-type: image/JPEG');
imagejpeg($imgrst,null,95);
};
break;
case 3:
if($imgfile!==false){
imagepng($imgrst,$_SERVER['DOCUMENT_ROOT'].$imgfilename.".png",null,9);
return $imgfilename.".png";
}else{
header('Content-type: image/PNG');
imagepng($imgrst,null,9);
};
break;
};
imagedestroy($imgrst);
imagedestroy($imgtemp);
imagedestroy($imagesrc);
}
}
?>静水缘首页
文章分类
最新文章
- nodejs私钥加密公钥解密的一个例子
- uniapp和微信小程序判断程序运行在开发或者测试或者线上版本的方法分别是什么
- electron使用electron-builder打包后模块包含exe文件执行失败
- Compile is disallowed on the main thread, if the buffer size is larger than 4KB
- better-sqlite3简介及常用操作
- nodejs 操作数据库的库
- nodejs使用http-proxy库实现多个域名代理和同时代理websocket的例子,代理包含https和http两种协议
- iis配置反向代理
- javascript伪多线程代码
- ip所在地址段判断