Php image processing class, scaling down, watermarking _ PHP Tutorial

Source: Internet
Author: User
Php image processing class, scaling down, watermarks. Php image processing class, scaling down, Watermark classImage {*** @ varstring $ fileName File Name * @ accessprivate * private $ fileName; *** @ vargdresource $ imageResource original image php image processing class, scaling down, watermark

Class Image {

/**
* @ Var string $ fileName file name
* @ Access private
*/
Private $ fileName = '';

/**
* @ Var gd resource $ imageResource original image
* @ Access private
*/
Private $ imageResource = NULL;

/**
* @ Var int $ imageWidth original image width
* @ Access private
*/
Private $ imageWidth = NULL;

/**
* @ Var int $ imageHeight original image height
* @ Access private
*/
Private $ imageHeight = NULL;

/**
* @ Var int $ imageType original image type
* @ Access private
*/
Private $ imageType = NULL;

/**
* @ Var int $ newResource New Image
* @ Access private
*/
Private $ newResource = NULL;

/**
* @ Var int $ newResType New Image type
* @ Access private
*/
Private $ newResType = NULL;

/**
* Constructor
* @ Param string $ fileName file name
*/
Public function _ construct ($ fileName = NULL ){
$ This-> fileName = $ fileName;
If ($ this-> fileName ){
$ This-> getSrcImageInfo ();
}
}

/**
* Retrieving source image information
* @ Access private
* @ Return void
*/
Private function getSrcImageInfo (){
$ Info = $ this-> getImageInfo ();
$ This-> imageWidth = $ info [0];
$ This-> imageHeight = $ info [1];
$ This-> imageType = $ info [2];
}

/**
* Retrieving image information
* @ Param string $ fileName file name
* @ Access private
* @ Return array
*/
Private function getImageInfo ($ fileName = NULL ){
If ($ fileName = NULL ){
$ FileName = $ this-> fileName;
}
$ Info = getimagesize ($ fileName );
Return $ info;
}

/**
* Create source image GD resources
* @ Access private
* @ Return void
*/
Private function createSrcImage (){
$ This-> imageResource = $ this-> createImageFromFile ();
}

/**
* Create image GD resources with data files
* @ Param string $ fileName file name
* @ Return gd resource
*/
Public function createImageFromFile ($ fileName = NULL)
{
If (! $ FileName ){
$ FileName = $ this-> fileName;
$ ImgType = $ this-> imageType;
}
If (! Is_readable ($ fileName) |! File_exists ($ fileName )){
Throw new Exception ('unable to open file "'. $ fileName .'"');
}

If (! $ ImgType ){
$ ImageInfo = $ this-> getImageInfo ($ fileName );
$ ImgType = $ imageInfo [2];
}

Switch ($ imgType ){
Case IMAGETYPE_GIF:
$ TempResource = imagecreatefromgif ($ fileName );
Break;
Case IMAGETYPE_JPEG:
$ TempResource = imagecreatefromjpeg ($ fileName );
Break;
Case IMAGETYPE_PNG:
$ TempResource = imagecreatefrompng ($ fileName );
Break;
Case IMAGETYPE_WBMP:
$ TempResource = imagecreatefromwbmp ($ fileName );
Break;
Case IMAGETYPE_XBM:
$ TempResource = imagecreatefromxbm ($ fileName );
Break;
Default:
Throw new Exception ('incorrect image format, or the image has a question

Question! ');
}
Return $ tempResource;
}
/**
* Changing the image size
* @ Param int $ width
* @ Param int $ height
* @ Param string $ How does the flag change? 0 = the length and width are converted to the value specified by the parameter. 1 = the ratio.

For example, the length and width constraints are within the specified parameter. 2 = the width constraints are used for scaling, and 3 = the height constraints are used for scaling.
* @ Return string
*/
Public function resizeImage ($ width, $ height, $ flag = 1 ){
Global $ cfg;
$ WidthRatio = $ width/$ this-> imageWidth;
$ HeightRatio = $ height/$ this-> imageHeight;
Switch ($ flag ){
Case 1:
If ($ this-> imageHeight <$ height & $ this-

> ImageWidth <$ width ){
$ EndWidth = $ this-> imageWidth;
$ EndHeight = $ this-> imageHeight;
// Return;
} Elseif ($ this-> imageHeight * $ widthRatio)

> $ Height ){
$ EndWidth = ceil ($ this-> imageWidth *

$ HeightRatio );
$ EndHeight = $ height;
} Else {
$ EndWidth = $ width;
$ EndHeight = ceil ($ this-> imageHeight *

$ WidthRatio );
}
Break;
Case 2:
$ EndWidth = $ width;
$ EndHeight = ceil ($ this-> imageHeight * $ widthRatio );
Break;
Case 3:
$ EndWidth = ceil ($ this-> imageWidth * $ heightRatio );
$ EndHeight = $ height;
Break;
Case 4:
$ Endwid2= $ width;
$ EndHeight2 = $ height;
If ($ this-> imageHeight <$ height & $ this-

> ImageWidth <$ width ){
$ EndWidth = $ this-> imageWidth;
$ EndHeight = $ this-> imageHeight;
// Return;
} Elseif ($ this-> imageHeight * $ widthRatio)

<$ Height ){
$ EndWidth = ceil ($ this-> imageWidth *

$ HeightRatio );
$ EndHeight = $ height;
} Else {
$ EndWidth = $ width;
$ EndHeight = ceil ($ this-> imageHeight *

$ WidthRatio );
}
Break;
Case 5:
$ Endwid2= $ width;
$ EndHeight2 = $ height;
If ($ this-> imageHeight> $ height & $ this-

> ImageWidth> $ width ){
// Large
$ Ratio = max ($ this-> imageHeight/

$ Height, $ this-> imageWidth/$ width );
} Elseif ($ this-> imageHeight> $ height ){
$ Ratio = $ this-> imageHeight/$ height;
} Elseif ($ this-> imageWidth> $ width ){
$ Ratio = $ this-> imageWidth/$ width;
} Else {
$ Ratio = 1;
}

$ EndWidth = $ this-> imageWidth/$ ratio;
$ EndHeight = $ this-> imageHeight/$ ratio;

Break;
Default:
$ EndWidth = $ width;
$ EndHeight = $ height;
Break;
}
If ($ this-> imageResource = NULL ){
$ This-> createSrcImage ();
}
If ($ flag = 5 ){
// Scale down
$ This-> newResource = imagecreatefromjpeg ($ cfg

Using 'path'{'data'}.'blank_thumb.jpg ');
} Elseif ($ flag = 4 ){
$ This-> newResource = imagecreatetruecolor

($ Endwid2,$ endHeight2 );
} Else {
$ This-> newResource = imagecreatetruecolor

($ EndWidth, $ endHeight );
}
$ This-> newResType = $ this-> imageType;
If ($ flag = 5 ){
$ Dest_x = ($ width-$ endWidth)/2;
$ Dest_y = ($ height-$ endHeight)/2;
Imagecopyresampled ($ this-> newResource, $ this-

> ImageResource, $ dest_x, $ dest_y, 0, 0, $ endWidth, $ endHeight, $ this-

> ImageWidth, $ this-> imageHeight );
} Else {
Imagecopyresampled ($ this-> newResource, $ this-

> ImageResource, 0, 0, 0, 0, $ endWidth, $ endHeight, $ this-> imageWidth, $ this-

> ImageHeight );
}
}

/**
* Watermarking an image
* @ Param string $ the watermark content can be the image file name or text

Word
* @ Param int $ pos position 0-9 can be an array
* @ Param int $ textFont font: valid when the watermark content is text
* @ Param string $ textColor: the text color, which is valid when the watermark content is text.
* @ Return string
*/
Public function waterMark ($ waterContent, $ pos = 0, $ textFont = 5,

$ TextColor = "# ffffff "){
$ IsWaterImage = file_exists ($ waterContent );
If ($ isWaterImage ){
$ WaterImgRes = $ this-> createImageFromFile

($ WaterContent );
$ WaterImgInfo = $ this-> getImageInfo ($ waterContent );
$ WaterWidth = $ waterImgInfo [0];
$ WaterHeight = $ waterImgInfo [1];
} Else {
$ WaterText = $ waterContent;
// $ Temp = @ imagettfbbox (ceil

($ TextFont * 2.5), 0, "./cour. ttf", $ waterContent );
If ($ temp ){
$ WaterWidth = $ temp [2]-$ temp [6];
$ WaterHeight = $ temp [3]-$ temp [7];
} Else {
$ WaterWidth = 100;
$ WaterHeight = 12;
}
}
If ($ this-> imageResource = NULL ){
$ This-> createSrcImage ();
}
Switch ($ pos)
{
Case 0: // random
$ PosX = rand (0, ($ this-> imageWidth-$ waterWidth ));
$ PosY = rand (0, ($ this-> imageHeight-$ waterHeight ));
Break;
Case 1: // 1 is the top left
$ PosX = 0;
$ PosY = 0;
Break;
Case 2: // 2 center the top
$ PosX = ($ this-> imageWidth-$ waterWidth)/2;
$ PosY = 0;
Break;
Case 3: // 3: top right
$ PosX = $ this-> imageWidth-$ waterWidth;
$ PosY = 0;
Break;
Case 4: // 4 is left in the middle
$ PosX = 0;
$ PosY = ($ this-> imageHeight-$ waterHeight)/2;
Break;
Case 5: // 5 center in the middle
$ PosX = ($ this-> imageWidth-$ waterWidth)/2;
$ PosY = ($ this-> imageHeight-$ waterHeight)/2;
Break;
Case 6: // 6 is the center and right
$ PosX = $ this-> imageWidth-$ waterWidth;
$ PosY = ($ this-> imageHeight-$ waterHeight)/2;
Break;
Case 7: // 7 is left at the bottom
$ PosX = 0;
$ PosY = $ this-> imageHeight-$ waterHeight;
Break;
Case 8: // 8 is centered at the bottom
$ PosX = ($ this-> imageWidth-$ waterWidth)/2;
$ PosY = $ this-> imageHeight-$ waterHeight;
Break;
Case 9: // 9: right
$ PosX = $ this-> imageWidth-$ waterWidth-20;
$ PosY = $ this-> imageHeight-$ waterHeight-10;
Break;
Default: // random
$ PosX = rand (0, ($ this-> imageWidth-$ waterWidth ));
$ PosY = rand (0, ($ this-> imageHeight-$ waterHeight ));
Break;
}
Imagealphablending ($ this-> imageResource, true );
If ($ isWaterImage ){
Imagecopy ($ this-> imageResource, $ waterImgRes, $ posX,

$ PosY, 0, 0, $ waterWidth, $ waterHeight );
} Else {
$ R = hexdec (substr ($ textColor, 1, 2 ));
$ G = hexdec (substr ($ textColor, 3, 2 ));
$ B = hexdec (substr ($ textColor, 5 ));

$ TextColor = imagecolorallocate ($ this-

> ImageResource, $ R, $ G, $ B );
Imagestring ($ this-> imageResource, $ textFont, $ posX,

$ PosY, $ waterText, $ textColor );
}
$ This-> newResource = $ this-> imageResource;
$ This-> newResType = $ this-> imageType;
}

/**
* Generate a verification code Image
* @ Param int $ width
* @ Param string $ height
* @ Param int $ length
* @ Param int $ validType 0 = digit, 1 = letter, 2 = digit plus letter
* @ Param string $ textColor text color
* @ Param string $ backgroundColor background color
* @ Return void
*/
Public function imageValidate ($ width, $ height, $ length = 4,

$ ValidType = 1, $ textColor = '#000000', $ backgroundColor = '# ffff '){
If ($ validType = 1 ){
// $ ValidString =

'Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy ';
// $ ValidLength = 52;
// No I no l
$ ValidString =

'Abcdefghjkmnopqrstuvwxyzabcdefghjkmnopqrstuvwxyxy ';
$ ValidLength = 48;
} Elseif ($ validType = 2 ){
// $ ValidString =

'0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy ';
// $ ValidLength = 62;
// No I no l no 1
$ ValidString =

'023456789abcdefghjkmnopqrstuvwxyzabcdefghjkmnopqrstuvwxy ';
$ ValidLength = 57;
} Else {
$ ValidString = '000000 ';
$ ValidLength = 10;
}

Srand (int) time ());
$ Valid = '';
For ($ I = 0; $ I <$ length; $ I ++ ){
$ Valid. = $ validString {rand (0, $ validLength-1 )};
}
$ This-> newResource = imagecreate ($ width, $ height );
$ BgR = hexdec (substr ($ backgroundColor, 1, 2 ));
$ BgG = hexdec (substr ($ backgroundColor, 3, 2 ));
$ BgB = hexdec (substr ($ backgroundColor, 5, 2 ));
$ BackgroundColor = imagecolorallocate ($ this-> newResource,

$ BgR, $ bgG, $ bgB );
$ TR = hexdec (substr ($ textColor, 1, 2 ));
$ TG = hexdec (substr ($ textColor, 3, 2 ));
$ TB = hexdec (substr ($ textColor, 5, 2 ));
$ TextColor = imagecolorallocate ($ this-> newResource, $ tR,

$ TG, $ tB );
For ($ I = 0; $ I Imagestring ($ this-> newResource, 5, $ I * $ width/

$ Length + 3, 2, $ valid [$ I], $ textColor );
}
$ This-> newResType = IMAGETYPE_JPEG;
Return $ valid;

}

/**
* Display output images
* @ Return void
*/
Public function display ($ fileName = '', $ quality = 60 ){

$ ImgType = $ this-> newResType;
$ ImageSrc = $ this-> newResource;
Switch ($ imgType ){
Case IMAGETYPE_GIF:
If ($ fileName = ''){
Header ('content-type: image/gif ');
}
Imagegif ($ imageSrc, $ fileName, $ quality );
Break;
Case IMAGETYPE_JPEG:
If ($ fileName = ''){
Header ('content-type: image/jpeg ');
}
Imagejpeg ($ imageSrc, $ fileName, $ quality );
Break;
Case IMAGETYPE_PNG:
If ($ fileName = ''){
Header ('content-type: image/png ');
Imagepng ($ imageSrc );
} Else {
Imagepng ($ imageSrc, $ fileName );
}
Break;
Case IMAGETYPE_WBMP:
If ($ fileName = ''){
Header ('content-type: image/wbmp ');
}
Imagewbmp ($ imageSrc, $ fileName, $ quality );
Break;
Case IMAGETYPE_XBM:
If ($ fileName = ''){
Header ('content-type: image/xbm ');
}
Imagexbm ($ imageSrc, $ fileName, $ quality );
Break;
Default:
Throw new Exception ('unsupport image type ');
}
Imagedestroy ($ imageSrc );
}

/**
* Save the image
* @ Param int $ fileNameType File Name Type 0 use the original file name, 1 use the specified file name

File name. 2 is added to the original file name, and 3 is used to generate a random file name.
* @ Param string $ folder the folder path is empty and is the same as the original file
* @ Param string $ param parameter $ fileNameType adds a suffix to the file name when fileNameType is set to 2.
* @ Return void
*/
Public function save ($ fileNameType = 0, $ folder = NULL, $ param =

'_ Miniature '){
If ($ folder = NULL ){
$ Folder = dirname ($ this-

> FileName). DIRECTORY_SEPARATOR;

}
$ FileExtName = FileSystem: fileExt ($ this-> fileName, true );
$ FileBesicName = FileSystem: getBasicName ($ this-> fileName,

False );
Switch ($ fileNameType ){
Case 1:
// $ NewFileName = $ folder. $ param;
$ NewFileName = $ folder. basename ($ this-

> FileName );
// Var_dump ($ newFileName );
Break;
Case 2:
$ NewFileName =

$ Folder. $ fileBesicName. $ param. $ fileExtName;
Break;
Case 3:
$ Tmp = date ('ymdhis ');
$ FileBesicName = $ tmp;
$ I = 0;
While (file_exists

($ Folder. $ fileBesicName. $ fileExtName )){
$ FileBesicName = $ tmp. $ I;
$ I ++;
}
$ NewFileName =

$ Folder. $ fileBesicName. $ fileExtName;
Break;
Default:
$ NewFileName = $ this-> fileName;
Break;
}
$ This-> display ($ newFileName );
Return $ newFileName;
}
/**
* Cut out the selected area
*
* @ Param string $ srcimgurl source image
* @ Param string $ image processed by endimgurl
* @ Param int $ x coordinate origin X
* @ Param int $ y coordinate origin Y
* @ Param int $ endimg_w final graph width
* @ Param int $ endimg_h final graph height
* @ Param int $ border_w coordinate X
* @ Param int $ border_h coordinate Y
* @ Param int $ scale percentage of original image scaling
* @ Param int $ whether the fix is automatically set
*/
Public function cutimg

($ Srcimgurl, $ endimgurl, $ x, $ y, $ endimg_w, $ endimg_h, $ border_w, $ border_h, $ scale =

100, $ fix = 0 ){
$ Path = dirname ($ endimgurl );
If (! Is_dir ($ path )){
If (! @ Mkdir ($ path, 0777 )){
Die ("{$ path} this directory cannot be created, file creation failed ");
}
}
$ Ground_info = getimagesize ($ srcimgurl );
Switch ($ ground_info [2]) {
Case 1: $ im = imagecreatefromgif ($ srcimgurl); break;
Case 2: $ im = imagecreatefromjpeg ($ srcimgurl); break;
Case 3: $ im = imagecreatefrompng ($ srcimgurl); break;
Default: die ("$ srcimgurl not allowed in image format ");
}
If ($ fix) {// convenient part of the Avatar
If ($ ground_info [0] <$ ground_info [1]) {
$ Border_w = $ ground_info [0];
$ Border_h = $ endimg_h * $ ground_info [0]/

$ Endimg_w;
} Elseif ($ ground_info [0]> $ ground_info [1]) {
$ Border_h = $ ground_info [1];
$ Border_w = $ endimg_w * $ ground_info [1]/

$ Endimg_h;
} Else {
$ Border_w = $ ground_info [0];
$ Border_h = $ ground_info [1];
}
}
$ Newim = imagecreatetruecolor ($ endimg_w, $ endimg_h );
$ X = ($ x * 100)/$ scale;
$ Y = ($ y x 100)/$ scale;
$ Border_width = ($ border_w * 100)/$ scale;
$ Border_height = ($ border_h * 100)/$ scale;
Imagecopyresampled ($ newim, $ im, 0, 0, $ x, $ y, $ endimg_w,

$ Endimg_h, $ border_width, $ border_height );
If (function_exists ("imagegif ")){
Switch ($ ground_info [2]) {
Case 1: imagegif ($ newim, $ endimgurl); break;
Case 2: imagejpeg ($ newim, $ endimgurl); break;
Case 3: imagepng ($ newim, $ endimgurl); break;
Default: die ("errorMsg ");
}
} Elseif (function_exists ("imagejpeg ")){
Imagejpeg ($ newim, $ endimgurl );
} Else {
Imagepng ($ newim, $ endimgurl );
}
Imagedestroy ($ newim );
Imagedestroy ($ im );
}
}


Http://www.bkjia.com/PHPjc/445017.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445017.htmlTechArticlephp Image processing class, scaling down, Watermark class Image {/*** @ var string $ fileName File Name * @ access private */private $ fileName = ''; /*** @ var gd resource $ imageResource original image...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.