


PHP generates verification code, thumbnail and watermark image class sharing, verification code watermark_PHP tutorial
Jul 12, 2016 am 08:54 AMPHP generates verification codes, thumbnails and watermarks for class sharing. Verification code watermark
encapsulates a class that can generate verification codes, thumbnails, and watermarks for sharing. For everyone
<?php class image{ session_start(); //驗(yàn)證碼類(lèi) static public function verify($code,$width=75,$height=25,$n=4){ header("content-type:image/png"); // 創(chuàng)建畫(huà)布 $img=imagecreatetruecolor($width,$height); // 設(shè)置背景色 $bgcolor=imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),rand(200,255)); // 將背景色填充 imagefill($img,0,0,$bgcolor); // 繪制五條弧線 for($i=0;$i<5;$i++){ $arccolor=imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),rand(200,255)); imagearc($img,mt_rand(5,($width-5)),mt_rand(5,($height-5)),mt_rand(5,($width-5)),mt_rand(5,($height-5)),mt_rand(0,360),mt_rand(0,360),$arccolor); } // 繪制一百個(gè)點(diǎn) for($i=0;$i<100;$i++){ $pixelcolor=imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),rand(200,255)); imagesetpixel($img,mt_rand(1,($width-1)),mt_rand(1,($height-1)),$pixelcolor); } // 繪制五條線段 for($i=0;$i<5;$i++){ $linecolor=imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),rand(200,255)); imageline($img,mt_rand(1,($width-1)),mt_rand(1,($height-1)),mt_rand(1,($width-1)),mt_rand(1,($height-1)),$linecolor); } // 設(shè)置邊框顏色 $bdcolor=imagecolorallocate($img,mt_rand(150,200),mt_rand(150,200),rand(150,200)); // 繪制一個(gè)矩形無(wú)填充邊框 imagerectangle($img,0,0,($width-1),($height-1),$bdcolor); // 設(shè)置驗(yàn)證碼字符串 $str=''; for($i=1;$i<=$n;$i++){ $str.=substr(str_shuffle($code),0,1); } $_SESSION['a']=$str; // for($i=0;$i<$n;$i++){ $textcolor=imagecolorallocate($img,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150)); imagettftext($img,1/($n+1)*$width,mt_rand(-20,20),((1-$n/(5+$n))*$width/($n-1)+$i*1/($n+1)*$width),1/2*($height+3/5*$height),$textcolor,'1.ttf',substr($str,$i,1)); } // 打印圖像 imagepng($img); // 釋放資源 imagedestroy($img); return $str; } //縮略圖類(lèi) /* *$source需要縮略的圖片 */ static public function thumbnail($source,$deletesource=false,$width=180){ $info=getimagesize($source); //getimagesize方法獲得圖像的詳細(xì)信息 $createFun=str_replace('/','createfrom',$info['mime']); //將getimagesize中的['mime']中的/替換,變?yōu)閕magecreatefromjpeg方法 $src=$createFun($source); //創(chuàng)建圖像 $dst_w=$width; //縮略圖的寬 $dst_h=$width/$info[0]*$info[1]; //計(jì)算縮略圖的高 $dst=imagecreatetruecolor($dst_w,$dst_h); //新建一個(gè)真彩色圖像 imagecopyresampled($dst,$src,0,0,0,0,$dst_w,$dst_h,$info['0'],$info['1']); //重采樣拷貝部分圖像并調(diào)整大小 $saveFun=str_replace('/','',$info['mime']); //設(shè)置保存函數(shù) $ext=strrchr($source,'.'); //strrchr查找指定字符在字符串中的最后一次出現(xiàn),返回包括其自身在內(nèi)的剩余的字符串 $thumbnailName=str_replace($ext,'',$source).'_thumbnail'.$ext; //設(shè)置保存路徑 if(!$deletesource){ $saveFun($dst,$thumbnailName); //保存圖像 }else{ $saveFun($dst,$thumbnailName); unlink($source); //刪除原圖 } imagedestroy($src); //銷(xiāo)毀資源 imagedestroy($dst); //銷(xiāo)毀資源 return $thumbnailName; } //水印圖類(lèi) /* *$srcing水印圖 *$dsting原圖 */ static public function watermark($dstimg,$srcimg){ $dstinfo=getimagesize($dstimg); //getimagesize方法獲取該圖片的詳細(xì)信息(該方法返回的是一個(gè)詳細(xì)的數(shù)組) $srcinfo=getimagesize($srcimg); $createdst=str_replace('/','createfrom',$dstinfo['mime']); //將getimagesize方法返回的數(shù)組中的mime值中的/替換,結(jié)果為imagecreatefromjpeg $createsrc=str_replace('/','createfrom',$srcinfo['mime']); $dst=$createdst($dstimg); //替換后為imagecreatefromjpeg函數(shù),創(chuàng)建一個(gè)圖像 $dst_w=imagesx($dst); //獲取大圖的寬 $dst_h=imagesy($dst); //獲取大圖的高 $src=$createsrc($srcimg); //替換后為imagecreatefromjpeg函數(shù),創(chuàng)建一個(gè)圖像 $src_w=imagesx($src); //獲取水印圖的寬 $src_h=imagesy($src); //獲取水印圖的高 $watermaker=imagecopy($dst,$src,($dst_w-$src_w),($dst_h-$src_h),0,0,$src_w,$src_h); //將水印圖復(fù)制在大圖上 $saveFun=str_replace('/','',$dstinfo['mime']); //將getimagesize返回?cái)?shù)組中的['mime']中的斜杠替換為空,替換后的值為imagejpeg,即為保存函數(shù) $ext=strrchr($dstimg,'.'); //strrchr查找指定字符在字符串中的最后一次出現(xiàn),返回包括其自身在內(nèi)的剩余的字符串 $watermaker=str_replace($ext,'',$dstimg).'_water'.time().$ext; //保存名 $saveFun($dst,$watermaker); //保存圖片,第一個(gè)參數(shù)為要保存的圖片,第二個(gè)參數(shù)為保存路徑或保存名 imagedestroy($dst); //銷(xiāo)毀資源 imagedestroy($src); //銷(xiāo)毀資源 return $watermaker; } } ?>
Articles you may be interested in:
- php image verification code code
- PHP dynamically randomly generated verification code code
- PHP verification code code ( Latest modification, fully customized! )
- A beautiful PHP verification code class (share)
- Solution to "the image cannot be displayed because of its own errors" when PHP generates the verification code
- PHP generates image verification code, click to switch instance
- php generate verification code function
- thinkphp3.2 click refresh to generate verification code
- PHP implementation to generate verification code example Share

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)

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

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
