Php image processing class .? PhpclassImage {private $ path; constructor is used to initialize function _ construct ($ path .) {$ this-pathrtrim ($ path ,).;} * scale the image Class Image {
Private $ path;
// The constructor is used to initialize the image location.
Function _ construct ($ path = "./"){
$ This-> path = rtrim ($ path ,"/")."/";
}
/* Scale the image
*
* Parameter $ name: indicates the name of the image to be processed.
* Parameter $ width: the scaled width.
* Parameter $ height: the scaled height.
* Parameter $ qz: the prefix of the new image name.
* Return value: the scaled image name. if the scaling fails, false is returned.
*
*/
Function thumb ($ name, $ width, $ height, $ qz = "th _"){
// Obtain image information
$ ImgInfo = $ this-> getInfo ($ name); // the width, height, and type of the image.
// Obtain image resources. you can create resources jpg, gif, and png for all types of images.
$ SrcImg = $ this-> getImg ($ name, $ imgInfo );
// Obtain the size after calculating the image proportion, $ size ["width"], $ size ["height"]
$ Size = $ this-> getNewSize ($ name, $ width, $ height, $ imgInfo );
// Obtain New Image resources and process the gif transparent background
$ NewImg = $ this-> kidOfImage ($ srcImg, $ size, $ imgInfo );
// Save as a new image and return the 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"];
// If the scaled width is smaller than the source image, the width is reset.
If ($ width <$ imgInfo ["width"]) {
$ Size ["width"] = $ width;
}
// Reset the height if the scaled height is smaller than the original image size.
If ($ height <$ imgInfo ["height"]) {
$ Size ["height"] = $ height;
}
// Proportional scaling algorithm for images
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 a watermark to an image
* Parameter $ groundName: specifies the background image to be watermark.
* Parameter $ waterName: shuiqian image
* Parameter # aterPost: watermark position, 10 statuses,
* 0 is a random position.
*
* 1. top left 2. Top Center 3: top right
* 4: center-left; 5: Center-6: center-right
* 7. center at the bottom, center at the bottom, and right at the bottom.
*
* Parameter $ qz: prefix of the watermark image name
* Return value: 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 );
// Watermark Position
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 image 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 must 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 );
}
}
Class Image {
Private $ path;
// The constructor is used to initialize the image location.
Function _ construct ($ path = "./"){
$ This-> path = rtrim ($ path ,"/")."/";
}
/* Scale the image
*
* Parameter $ name: indicates the name of the image to be processed.
* Parameter $ width: the scaled width.
* Parameter $ height: the scaled height.
* Parameter $ qz: the prefix of the new image name.
* Return value: the scaled image name. if the scaling fails, false is returned.
*
*/
Function thumb ($ name, $ width, $ height, $ qz = "th _"){
// Obtain image information
$ ImgInfo = $ this-> getInfo ($ name); // the width, height, and type of the image.
// Obtain image resources. you can create resources jpg, gif, and png for all types of images.
$ SrcImg = $ this-> getImg ($ name, $ imgInfo );
// Obtain the size after calculating the image proportion, $ size ["width"], $ size ["height"]
$ Size = $ this-> getNewSize ($ name, $ width, $ height, $ imgInfo );
// Obtain New Image resources and process the gif transparent background
$ NewImg = $ this-> kidOfImage ($ srcImg, $ size, $ imgInfo );
// Save as a new image and return the 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"];
// If the scaled width is smaller than the source image, the width is reset.
If ($ width <$ imgInfo ["width"]) {
$ Size ["width"] = $ width;
}
// Reset the height if the scaled height is smaller than the original image size.
If ($ height <$ imgInfo ["height"]) {
$ Size ["height"] = $ height;
}
// Proportional scaling algorithm for images
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 a watermark to an image
* Parameter $ groundName: specifies the background image to be watermark.
* Parameter $ waterName: shuiqian image
* Parameter # aterPost: watermark position, 10 statuses,
* 0 is a random position.
*
* 1. top left 2. Top Center 3: top right
* 4: center-left; 5: Center-6: center-right
* 7. center at the bottom, center at the bottom, and right at the bottom.
*
* Parameter $ qz: prefix of the watermark image name
* Return value: 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 );
// Watermark Position
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 image 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 must 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 );
}
}
From chaojie2009
Http://www.bkjia.com/PHPjc/478376.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478376.htmlTechArticle? Php class Image {private $ path; // Constructor is used to initialize function _ construct ($ path =. /) {$ this-path = rtrim ($ path ,/). //}/* scale the image...