php image processing class_PHP tutorial
Jul 13, 2016 pm 05:49 PM
class Image {
??????? private $path;
//Construction method is used to initialize the location of the picture
function __construct($path="./"){
???????????????? $this->path=rtrim($path, "/")."/";
?????????}?
/* A shrinking of the picture
*
* Parameter $name: is the name of the image that needs to be processed
* Parameter $width: is the width after scaling
* Parameter $height: is the scaled height
* Parameter $qz: is the name prefix of the new picture
?????????????? * Return value: It is the name of the zoomed image, if it fails, it returns false
*
????????? */?
???????? function thumb($name, $width, $height, $qz="th_"){
//Get picture information
??????????????? $imgInfo=$this->getInfo($name); //Width, height, type of image
//Get image resources, resources can be created for various types of pictures jpg, gif, png
???????????????? $srcImg=$this->getImg($name, $imgInfo);
//Get the size of the image after calculating the equal proportions, $size["width"], $size["height"]
$size=$this->getNewSize($name, $width, $height, $imgInfo);
//Get new image resources and process the gif transparent background
??????????????? $newImg=$this->kidOfImage($srcImg, $size, $imgInfo);
//Save as a new image and return the new scaled image name
????????????????? 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"];?
?
??????????? //縮放的寬度如果比原圖小才重新設(shè)置寬度??
??????????? if($width < $imgInfo["width"]){?
??????????????? $size["width"]=$width;?
??????????? }?
??????????? //縮放的高度如果比原圖小才重新設(shè)置高度??
??????????? 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;?
??????? }?
??????? /* 功能:為圖片加水印圖片
???????? * 參數(shù)$groundName: 背景圖片,即需要加水印的圖片
???????? * 參數(shù)$waterName: 水錢圖片
???????? * 參數(shù)#aterPost:水印位置, 10種狀態(tài),?
???????? *? 0為隨機位置
???????? *
???????? *? 1. 為頂端居左? 2. 為頂端居中? 3 為頂端居右
???????? *? 4? 為中部居左? 5. 為中部居中? 6 為中部居右
???????? *? 7 . 為底端居左 8. 為底端居中, 9. 為底端居右
???????? *
???????? * 參數(shù)$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 "水印不應(yīng)該比背景圖片小!";?
??????????????????? 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);?
??????? }?
?
??? }?
?class Image {
??private $path;
??//構(gòu)造方法用來對圖片所在位置進行初使化
??function __construct($path="./"){
???$this->path=rtrim($path, "/")."/";
??}
??/* 對圖片進行縮放
?? *
* Parameter $name: is the name of the image that needs to be processed
* Parameter $width: is the width after scaling
* Parameter $height: is the scaled height
* Parameter $qz: is the name prefix of the new picture
* Return value: It is the name of the zoomed image. If it fails, it returns false
*
*/
function thumb($name, $width, $height, $qz="th_"){
//Get picture information
$imgInfo=$this->getInfo($name); //Width, height, type of image
//Get picture resources, you can create resources for various types of pictures jpg, gif, png
$srcImg=$this->getImg($name, $imgInfo);
//Get the size of the image after calculating the equal proportions, $size["width"], $size["height"]
$size=$this->getNewSize($name, $width, $height, $imgInfo);
//Get new image resources and process the gif transparent background
$newImg=$this->kidOfImage($srcImg, $size, $imgInfo);
//Save as a new picture and return the new scaled picture name
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 the zoomed width is smaller than the original image, reset the width
if($width < $imgInfo["width"]){
$size["width"]=$width;
}
//If the zoomed height is smaller than the original image, reset the height
if($height < $imgInfo["height"]){
$size["height"]=$height;
}
//Algorithm for image scaling
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;
}
/* Function: Add watermark to pictures
* Parameter $groundName: background image, that is, the image that needs to be watermarked
* Parameter $waterName: water money picture
* Parameter #aterPost: watermark position, 10 states,
* 0 is a random position
*
* 1. For top left 2. For top center 3 For top right
* 4 means the middle is on the left 5. means the middle is in the middle 6 means the middle is on the right
* 7. The bottom is on the left, 8. The bottom is in the middle, 9. The bottom is on the right
*
* Parameter $qz: is the watermarked image name prefix
* Return value: It is the name of the processed image
*
*/
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);
//Position of watermark
If(!$pos=$this->position($groundInfo, $waterInfo, $waterPos)){
echo "The watermark should not be smaller than the background image!";
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 "The picture or watermark image does not exist";
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){
//The background needs to be larger than the watermark image
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);
??}
?}
?摘自 chaojie2009的專欄

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

The method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.

To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.

UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat

In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

In PHP, to pass a session variable to another page, the key is to start the session correctly and use the same $_SESSION key name. 1. Before using session variables for each page, it must be called session_start() and placed in the front of the script; 2. Set session variables such as $_SESSION['username']='JohnDoe' on the first page; 3. After calling session_start() on another page, access the variables through the same key name; 4. Make sure that session_start() is called on each page, avoid outputting content in advance, and check that the session storage path on the server is writable; 5. Use ses

ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach
