class Image {
private $path;
//构造方法用来对图片所在位置进行初使化
function __construct($path="./"){
$this->path=rtrim($path, "/")."/";
}
/* 对图片进行缩放
*
* 参数$name: 是需要处理的图片名称
* 参数$width:是缩放后的宽度
* 参数$height:是缩放后的高度
* 参数$qz: 是新图片的名称前缀
* 返回值:就是缩放后的图片名称,失败则返回false
*
*/
function thumb($name, $width, $height, $qz="th_"){
//获取图片信息
$imgInfo=$this->getInfo($name); //图片的宽度,高度,类型
//获取图片资源, 各种类型的图片都可以创建资源 jpg, gif, png
$srcImg=$this->getImg($name, $imgInfo);
//获取计算图片等比例之后的大小, $size["width"], $size["height"]
$size=$this->getNewSize($name, $width, $height, $imgInfo);
//获取新的图片资源, 处理一下gif透明背景
$newImg=$this->kidOfImage($srcImg, $size, $imgInfo);
//另存为一个新的图片,返回新的缩放后的图片名称
return $this->createNewImage($newImg, $qz.$name, $imgInfo);
}
private function createNewImage($newImg, $newName, $imgInfo){
switch($imgInfo["type"]){
case 1://gif
$result=imageGif($newImg, $this->path.$newName);
break;
case 2://jpg
$result=imageJPEG($newImg, $this->path.$newName);
break;
case 3://png
$return=imagepng($newImg, $this->path.$newName);
break;
}
imagedestroy($newImg);
return $newName;
}
private function kidOfImage($srcImg, $size, $imgInfo){
$newImg=imagecreatetruecolor($size["width"], $size["height"]);
$otsc=imagecolortransparent($srcImg);
if($otsc >=0 && $otsc
$tran=imagecolorsforindex($srcImg, $otsc);
$newt=imagecolorallocate($newImg, $tran["red"], $tran["green"], $tran["blue"]);
imagefill($newImg, 0, 0, $newt);
imagecolortransparent($newImg, $newt);
}
imagecopyresized($newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"]);
imagedestroy($srcImg);
return $newImg;
}
private function getNewSize($name, $width, $height, $imgInfo){
$size["width"]=$imgInfo["width"];
$size["height"]=$imgInfo["height"];
//缩放的宽度如果比原图小才重新设置宽度
if($width
$size["width"]=$width;
}
//缩放的高度如果比原图小才重新设置高度
if($height
$size["height"]=$height;
}
//图片等比例缩放的算法
if($imgInfo["width"]*$size["width"] > $imgInfo["height"] * $size["height"]){
$size["height"]=round($imgInfo["height"]*$size["width"]/$imgInfo["width"]);
}else{
$size["width"]=round($imgInfo["width"]*$size["height"]/$imgInfo["height"]);
}
return $size;
}
private function getInfo($name){
$data=getImageSize($this->path.$name);
$imageInfo["width"]=$data[0];
$imageInfo["height"]=$data[1];
$imageInfo["type"]=$data[2];
return $imageInfo;
}
private function getImg($name, $imgInfo){
$srcPic=$this->path.$name;
switch($imgInfo["type"]){
case 1: //gif
$img=imagecreatefromgif($srcPic);
break;
case 2: //jpg
$img=imageCreatefromjpeg($srcPic);
break;
case 3: //png
$img=imageCreatefrompng($srcPic);
break;
default:
return false;
}
return $img;
}
/* 功能:为图片加水印图片
* 参数$groundName: 背景图片,即需要加水印的图片
* 参数$waterName: 水钱图片
* 参数#aterPost:水印位置, 10种状态,
* 0为随机位置
*
* 1. 为顶端居左 2. 为顶端居中 3 为顶端居右
* 4 为中部居左 5. 为中部居中 6 为中部居右
* 7 . 为底端居左 8. 为底端居中, 9. 为底端居右
*
* 参数$qz : 是加水印后的图片名称前缀
* 返回值:就是处理后图片的名称
*
*/
function waterMark($groundName, $waterName, $waterPos=0, $qz="wa_"){
if(file_exists($this->path.$groundName) && file_exists($this->path.$waterName)){
$groundInfo=$this->getInfo($groundName);
$waterInfo=$this->getInfo($waterName);
//水印的位置
if(!$pos=$this->position($groundInfo, $waterInfo, $waterPos)){
echo "水印不应该比背景图片小!";
return;
}
$groundImg=$this->getImg($groundName, $groundInfo);
$waterImg=$this->getImg($waterName, $waterInfo);
$groundImg=$this->copyImage($groundImg, $waterImg, $pos, $waterInfo);
return $this->createNewImage($groundImg, $qz.$groundName, $groundInfo);
}else{
echo "图片或水印图片不存在";
return false;
}
}
private function copyImage($groundImg, $waterImg, $pos, $waterInfo){
imagecopy($groundImg, $waterImg, $pos["posX"], $pos["posY"], 0, 0, $waterInfo["width"], $waterInfo["height"]);
imagedestroy($waterImg);
return $groundImg;
}
private function position($groundInfo, $waterInfo, $waterPos){
//需要背景比水印图片大
if(($groundInfo["width"]
return false;
}
switch($waterPos){
case 1:
$posX=0;
$posY=0;
break;
case 2:
$posX=($groundInfo["width"]-$waterInfo["width"])/2;
$posY=0;
break;
case 3:
$posX=$groundInfo["width"]-$waterInfo["width"];
$posY=0;
break;
case 4:
$posX=0;
$posY=($groundInfo["height"]-$waterInfo["height"]) /2;
break;
case 5:
$posX=($groundInfo["width"]-$waterInfo["width"])/2;
$posY=($groundInfo["height"]-$waterInfo["height"]) /2;
break;
case 6:
$posX=$groundInfo["width"]-$waterInfo["width"];
$posY=($groundInfo["height"]-$waterInfo["height"]) /2;
break;
case 7:
$posX=0;
$posY=$groundInfo["height"]-$waterInfo["height"];
break;
case 8:
$posX=($groundInfo["width"]-$waterInfo["width"])/2;
$posY=$groundInfo["height"]-$waterInfo["height"];
break;
case 9:
$posX=$groundInfo["width"]-$waterInfo["width"];
$posY=$groundInfo["height"]-$waterInfo["height"];
break;
case 0:
default:
$posX=rand(0, ($groundInfo["width"]-$waterInfo["width"]));
$posY=rand(0, ($groundInfo["height"]-$waterInfo["height"]));
break;
}
return array("posX"=>$posX, "posY"=>$posY);
}
}
class Image {
private $path;
//构造方法用来对图片所在位置进行初使化
function __construct($path="./"){
$this->path=rtrim($path, "/")."/";
}
/* 对图片进行缩放
*
* 参数$name: 是需要处理的图片名称
* 参数$width:是缩放后的宽度
* 参数$height:是缩放后的高度
* 参数$qz: 是新图片的名称前缀
* 返回值:就是缩放后的图片名称,失败则返回false
*
*/
function thumb($name, $width, $height, $qz="th_"){
//获取图片信息
$imgInfo=$this->getInfo($name); //图片的宽度,高度,类型
//获取图片资源, 各种类型的图片都可以创建资源 jpg, gif, png
$srcImg=$this->getImg($name, $imgInfo);
//获取计算图片等比例之后的大小, $size["width"], $size["height"]
$size=$this->getNewSize($name, $width, $height, $imgInfo);
//获取新的图片资源, 处理一下gif透明背景
$newImg=$this->kidOfImage($srcImg, $size, $imgInfo);
//另存为一个新的图片,返回新的缩放后的图片名称
return $this->createNewImage($newImg, $qz.$name, $imgInfo);
}
private function createNewImage($newImg, $newName, $imgInfo){
switch($imgInfo["type"]){
case 1://gif
$result=imageGif($newImg, $this->path.$newName);
break;
case 2://jpg
$result=imageJPEG($newImg, $this->path.$newName);
break;
case 3://png
$return=imagepng($newImg, $this->path.$newName);
break;
}
imagedestroy($newImg);
return $newName;
}
private function kidOfImage($srcImg, $size, $imgInfo){
$newImg=imagecreatetruecolor($size["width"], $size["height"]);
$otsc=imagecolortransparent($srcImg);
if($otsc >=0 && $otsc $tran=imagecolorsforindex($srcImg, $otsc);
$newt=imagecolorallocate($newImg, $tran["red"], $tran["green"], $tran["blue"]);
imagefill($newImg, 0, 0, $newt);
imagecolortransparent($newImg, $newt);
}
imagecopyresized($newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"]);
imagedestroy($srcImg);
return $newImg;
}
private function getNewSize($name, $width, $height, $imgInfo){
$size["width"]=$imgInfo["width"];
$size["height"]=$imgInfo["height"];
//缩放的宽度如果比原图小才重新设置宽度
if($width
$size["width"]=$width;
}
//缩放的高度如果比原图小才重新设置高度
if($height
$size["height"]=$height;
}
//图片等比例缩放的算法
if($imgInfo["width"]*$size["width"] > $imgInfo["height"] * $size["height"]){
$size["height"]=round($imgInfo["height"]*$size["width"]/$imgInfo["width"]);
}else{
$size["width"]=round($imgInfo["width"]*$size["height"]/$imgInfo["height"]);
}
return $size;
}
private function getInfo($name){
$data=getImageSize($this->path.$name);
$imageInfo["width"]=$data[0];
$imageInfo["height"]=$data[1];
$imageInfo["type"]=$data[2];
return $imageInfo;
}
private function getImg($name, $imgInfo){
$srcPic=$this->path.$name;
switch($imgInfo["type"]){
case 1: //gif
$img=imagecreatefromgif($srcPic);
break;
case 2: //jpg
$img=imageCreatefromjpeg($srcPic);
break;
case 3: //png
$img=imageCreatefrompng($srcPic);
break;
default:
return false;
}
return $img;
}
/* 功能:为图片加水印图片
* 参数$groundName: 背景图片,即需要加水印的图片
* 参数$waterName: 水钱图片
* 参数#aterPost:水印位置, 10种状态,
* 0为随机位置
*
* 1. 为顶端居左 2. 为顶端居中 3 为顶端居右
* 4 为中部居左 5. 为中部居中 6 为中部居右
* 7 . 为底端居左 8. 为底端居中, 9. 为底端居右
*
* 参数$qz : 是加水印后的图片名称前缀
* 返回值:就是处理后图片的名称
*
*/
function waterMark($groundName, $waterName, $waterPos=0, $qz="wa_"){
if(file_exists($this->path.$groundName) && file_exists($this->path.$waterName)){
$groundInfo=$this->getInfo($groundName);
$waterInfo=$this->getInfo($waterName);
//水印的位置
if(!$pos=$this->position($groundInfo, $waterInfo, $waterPos)){
echo "水印不应该比背景图片小!";
return;
}
$groundImg=$this->getImg($groundName, $groundInfo);
$waterImg=$this->getImg($waterName, $waterInfo);
$groundImg=$this->copyImage($groundImg, $waterImg, $pos, $waterInfo);
return $this->createNewImage($groundImg, $qz.$groundName, $groundInfo);
}else{
echo "图片或水印图片不存在";
return false;
}
}
private function copyImage($groundImg, $waterImg, $pos, $waterInfo){
imagecopy($groundImg, $waterImg, $pos["posX"], $pos["posY"], 0, 0, $waterInfo["width"], $waterInfo["height"]);
imagedestroy($waterImg);
return $groundImg;
}
private function position($groundInfo, $waterInfo, $waterPos){
//需要背景比水印图片大
if(($groundInfo["width"]
return false;
}
switch($waterPos){
case 1:
$posX=0;
$posY=0;
break;
case 2:
$posX=($groundInfo["width"]-$waterInfo["width"])/2;
$posY=0;
break;
case 3:
$posX=$groundInfo["width"]-$waterInfo["width"];
$posY=0;
break;
case 4:
$posX=0;
$posY=($groundInfo["height"]-$waterInfo["height"]) /2;
break;
case 5:
$posX=($groundInfo["width"]-$waterInfo["width"])/2;
$posY=($groundInfo["height"]-$waterInfo["height"]) /2;
break;
case 6:
$posX=$groundInfo["width"]-$waterInfo["width"];
$posY=($groundInfo["height"]-$waterInfo["height"]) /2;
break;
case 7:
$posX=0;
$posY=$groundInfo["height"]-$waterInfo["height"];
break;
case 8:
$posX=($groundInfo["width"]-$waterInfo["width"])/2;
$posY=$groundInfo["height"]-$waterInfo["height"];
break;
case 9:
$posX=$groundInfo["width"]-$waterInfo["width"];
$posY=$groundInfo["height"]-$waterInfo["height"];
break;
case 0:
default:
$posX=rand(0, ($groundInfo["width"]-$waterInfo["width"]));
$posY=rand(0, ($groundInfo["height"]-$waterInfo["height"]));
break;
}
return array("posX"=>$posX, "posY"=>$posY);
}
}
摘自 chaojie2009的专栏

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

tomakephpapplicationsfaster,关注台词:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

到ImprovephPapplicationspeed,关注台词:1)启用opcodeCachingwithapCutoredUcescriptexecutiontime.2)实现databasequerycachingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandredececonnection.4 limitsclection.4.4

依赖注入(DI)通过显式传递依赖关系,显着提升了PHP代码的可测试性。 1)DI解耦类与具体实现,使测试和维护更灵活。 2)三种类型中,构造函数注入明确表达依赖,保持状态一致。 3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

databasequeryOptimizationinphpinvolVolVOLVESEVERSEVERSTRATEMIESOENHANCEPERANCE.1)SELECTONLYNLYNESSERSAYCOLUMNSTORMONTOUMTOUNSOUDSATATATATATATATATATATRANSFER.3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

Dreamweaver CS6
视觉化网页开发工具

Atom编辑器mac版下载
最流行的的开源编辑器