php image processing class (simple and easy to use)
A PHP image processing class that can scale images and add watermarks to images. Friends in need can refer to it.
The code is as follows: <?php 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 <= imagecolorstotal($srcImg)){ $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 < $imgInfo["width"]){ $size["width"]=$width; } //缩放的高度如果比原图小才重新设置高度 if($height < $imgInfo["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"]< $waterInfo["width"]) ||($groundInfo["height"] < $waterInfo["height"])){ 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); } } Copy after login |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Alipay PHP...

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,
