I simply wrote a PHP image processing library. Although there are fewer functions, it is not used too advanced at the moment. If you use it in the future, please fill in it. Or who can add some suggestions to some functions, or if you have any requirements, you can tell me that I have time to add it. If anyone has expanded this library, please share it. The code can be used now. There are not many things to consider. If you have any better suggestions, please tell me. Thank you.
<?php /** * Created by PhpStorm. * User: MCtion * Date: 2015/5/14 0014 * Time: 15:36 * Simple image class library, all relative paths in this class are based on the website root directory. If you need to modify it, just modify the constant __WEBROOT__ * Function: Specify text content to create pictures (not supported in Chinese), create verification code pictures, create thumbnails, and other functions to be continued * method: * Style(array $Options) Sets the picture style, which will be reset to the default style before each setting. * Create_Img_Png() Creates a PNG image, and the relevant attributes are specified by Style * Create_Img_Jpeg() Creates a JPEG image, and the relevant attributes are specified by Style * Create_Verify() Create a verification code image, and the relevant attributes are specified by Style * Get_Verify() Get the created verification code value, MD5 encryption * Load_Img(string $FilePath) Load the image, create the image source, and other methods to call the source. FilePath is the image relative path * Create_Thumb(string $FileName,string $FilePath) Creates a thumbnail of the image loaded by Load_Img(). FileName is the saved image prefix, FilePath is the relative path to save the image, and does not include the file name (example: /Uploads/images/Thumb/) */ if(!defined("__WEBROOT__")) define("__WEBROOT__",$_SERVER['DOCUMENT_ROOT']); class Img { protected $_Img; //Picture source protected $_FileImg; //Loaded image source protected $_FileInfo; //The information array of loaded pictures protected $_PicInfo; //Array of width and height information of the loaded image protected $_Rand = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ1234567890'; //Random factor protected $_Code = ''; //Verification code public $Width = 300;//The picture is width by default public $Height = 80; //The picture default is high public $BackgroundColor = "000000"; public $Font = "/phps/Public/Font/"; //Default font public $FontSize = 16; //Default font size public $FontColor = "ffffff"; //Default font color public $Content = "Test Word"; public $Align = "left"; public $Codes = 4; //The number of verification codes public $Line = 6; //The number of disturbing lines public $LoadErr = ''; //error message //public function __construct(){} /** Set picture properties * @param array $Options attribute array * @return $this Return object */ public function Style($Options){ $this -> _Re_Set(); foreach($Options as $K=>$V){ if(in_array($K,array('Width','Height','BackgroundColor','Font','FontSize','FontColor','Content','Align','Codes','Line','Snow'))){ if($K == "BackgroundColor" || $K == "FontColor"){ if(preg_match("#([a-zA-Z0-9]{6})#",$V)) $this -> $K = $V; }else{ $this -> $K = $V; } } } return $this; } /** * Reset properties, no external access is provided */ protected function _Re_Set(){ $this -> Width = 100; $this -> Height = 30; $this -> BackgroundColor = "000000"; $this -> Font = "/phps/Public/Font/"; $this -> FontSize = 16; $this -> FontColor = "ffffff"; $this -> Align = "left"; $this -> Codes =4; $this -> Line = 6; } /** * Create image source, add background, create image * @param bool $BGC Specifies whether to create background color and rectangular blocks */ protected function _Create_Img_GB($BGC = True){ $this -> _Img = imagecreatetruecolor($this -> Width,$this -> Height); //Create background source if($BGC){ preg_match("#([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})#",$this -> BackgroundColor,$ColorArr); //Separate the color values into three groups of 16-bit binary numbers $Color = imagecolorallocate($this -> _Img,hexdec($ColorArr[1]),hexdec($ColorArr[2]),hexdec($ColorArr[3])); //Add background color to Img image source imagefilledrectangle($this -> _Img,0,$this -> Height,$this -> Width,0,$Color); //Create an image } } /** * Create a random verification code */ protected function _Create_Code(){ $Len = strlen($this -> _Rand) - 1; for($i = 0;$i < $this -> Codes;$i++){ $this -> _Code .= $this -> _Rand[mt_rand(0,$Len)]; } } /** * Write strings to the image, Chinese is not supported yet */ protected function _Write_Text(){ $FontWidth = imagefontwidth($this -> FontSize); //Get the width of a character of the font size preg_match_all('/(.)/us', $this -> Content, $TextArr); //Separate the content into an array and count the number $FontHeight = imagefontheight($this -> FontSize); //Get the height of the font size $X = ceil(($this -> Width - ($FontWidth * count($TextArr[0]))) / 2); //Set the distance of the left margin of the X wheelbase $Y = ceil(($this -> Height + $FontHeight) / 2); //Set the distance on the margin of the Y-wheelbase preg_match("#([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})#",$this -> FontColor,$ColorArr); $Color = imagecolorallocate($this -> _Img,hexdec($ColorArr[1]),hexdec($ColorArr[2]),hexdec($ColorArr[3])); //Set text color imagettftext($this -> _Img,$this -> FontSize,0,$X,$Y,$Color,__WEBROOT__.$this -> Font,$this -> Content); //Write content } /** * Write verification code to the image */ protected function _Write_Code(){ $_X = $this -> Width / $this -> Codes; //Set aspect ratio for($i = 0;$i < $this -> Codes;$i++){ //Change Codes times, generate one verification code value each time $Color = imagecolorallocate($this -> _Img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); //The color of the verification code value is randomly generated imagettftext($this -> _Img,$this -> FontSize,mt_rand(-30,30),$_X*$i+mt_rand(1,5),$this -> Height / 1.3,$Color,__WEBROOT__.$this -> Font,$this -> _Code[$i]); //Generate a verification code value } } /** * Write disturb lines into the image */ protected function _Write_Line() { //Generate interference lines for ($i=0;$i < $this -> Line;$i++) { $Color = imagecolorallocate($this -> _Img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); imageline($this -> _Img,mt_rand(0,$this -> Width),mt_rand(0,$this -> Height),mt_rand(0,$this -> Width),mt_rand(0,$this -> Height),$Color); } } /** * Set the image type to JPEG */ protected function _Img_Jpeg(){ header('Content-type:image/jpeg'); imagejpeg($this -> _Img); imagedestroy($this -> _Img); } /** * Set the image type to PNG */ protected function _Img_Png(){ header('Content-type:image/png'); imagepng($this -> _Img); imagedestroy($this -> _Img); } /** * Create a JPEG string image */ public function Create_Img_Jpg(){ $this -> _Create_Img_GB(True); $this -> _Write_Text(); $this -> _Img_Jpeg(); } /** * Create a PNG string image */ public function Create_Img_Png(){ $this -> _Create_Img_GB(True); $this -> _Write_Text(); $this -> _Img_Png(); } /** * Create a PNG image of verification code */ public function Create_Verify(){ $this -> BackgroundColor = ''; for($I = 0;$I < 3;$I++){ $this -> BackgroundColor .= dechex(mt_rand(20,155)); } $this -> _Create_Img_GB(True); $this -> _Create_Code(); $this -> _Write_Line(); $this -> _Write_Code(); $this -> _Img_Png(); } /** * Externally obtain the MD5 encrypted verification code * @return string */ public function Get_Verify(){ return md5($this -> _Code); } /** * Load an image file and obtain image-related information * @param string $FilePath image relative path address * @return $this|bool successfully returns the object, otherwise it returns FALSE */ public function Load_Img($FilePath){ $FilePath = __WEBROOT__.$FilePath; if(!is_file($FilePath)){ $this -> LoadErr = "Path error, file does not exist"; Return False; } $this -> _PicInfo = getimagesize($FilePath); $this -> _FileInfo = pathinfo($FilePath); switch($this -> _PicInfo[2]){ case 1:$this ->_FileImg = imagecreatefromgif($FilePath);break; case 2:$this ->_FileImg = imagecreatefromjpeg($FilePath);break; case 3:$this ->_FileImg = imagecreatefrompng($FilePath);break; default:$this -> LoadErr = "Type error, unsupported image type";Return False; } Return True; } /** * Create thumbnails * @param string $FileName Saved image name prefix * @param string $FilePath Relative path to save the image * @return mixed Returns the information array of generated images */ public function Create_Thumb($FileName,$FilePath){ $SavePath = __WEBROOT__.$FilePath; if(!file_exists($SavePath)){ mkdir($SavePath,0777,true); } $FileName = $("YmdHis").rand(100,999).'.'.$this -> _FileInfo['extension']; $FilePath = $FilePath.$FileName; $SavePath = $SavePath.$FileName; $this -> _Create_Img_GB(False); imagecopyresampled($this -> _Img,$this -> _FileImg,0,0,0,0,$this -> Width,$this -> Height,$this -> _PicInfo[0],$this -> _PicInfo[1]); switch($this -> _PicInfo[2]){ case 1:imagegif($this -> _Img,$SavePath);break; case 2:imagejpeg($this -> _Img,$SavePath);break; case 3:imagepng($this -> _Img,$SavePath);break; } $FIleInfo['FileName'] = $FileName; $FIleInfo['FilePath'] = $FilePath; Return $FIleInfo; } }
Example of usage
$Img = new Img(); $Options['Width'] = 300; $Options['Height'] = 100; $Options['Content'] = "Test Create Img"; $Options['FontColor'] = "FF0000"; $Options['BackgroundColor'] = "AAAAAA"; $Img -> Style($Options) -> Create_Img_Jpg(); if($Img -> Load_Img("/Public/images/")){ $FileInfo = $Img -> Style(array('Width'=>30,'Height'=>30)) -> Create_Thumb("Thumb","/Uploads/images/"); var_dump($FileInfo); }else{ die("The image failed to load,".$Img -> LoadErr); }
The above is the entire content of this article, I hope you like it.