Php: easy watermark and thumbnail graphics class _ PHP Tutorial

Source: Internet
Author: User
Tags image identifier
Php facilitates the watermark and thumbnail graphics class. Copy the code as follows :? Php ** @ author sleep 27262681@qq.com * copyrightwww.gowake.cn * classimg {function _ construct ($ argnull) {$ argsfunc_get_args (); if ($ The code is as follows:



/*
* @ Author sleepless 27262681@qq.com
* Copyright http://www.gowake.cn
*/

Class img {
Function _ construct ($ arg = null ){
$ Args = func_get_args ();
If ($ arg = null ){
Return null;
}
$ This-> im = call_user_func_array (array ($ this, 'create'), $ args );
}

Function _ call ($ func, $ arg ){
If (function_exists ('image'. $ func )){
$ Funcstr = 'image'. $ func;
} Elseif (function_exists ($ func )){
$ Funcstr = $ func;
} Else {
Error ("This class method or function is not available ");
}
Return call_user_func_array ($ funcstr, $ arg );
}

/*
* Create an image
* @ Param string/int path or width of the image file
* @ Param int height, which can be omitted
* @ Param string 6-digit hexadecimal integer
*/
Function create ($ arg = null ){
$ Args = func_get_args ();
If (is_file ($ args [0]) {
$ This-> file = $ args [0];
$ Size = getimagesize ($ this-> file) or error ('image type error ');
$ This-> size = $ this-> size? $ This-> size: $ size;
$ Type = image_type_to_extension ($ size [2], false );
$ This-> type = $ this-> type? $ This-> type: $ type;
$ Createfunc = 'imagecreatefrom '. $ type;
$ Im = $ createfunc ($ this-> file );
} Elseif (int) $ args [0]> 0 and (int) $ args [1]> 0 ){
$ Im = imagecreatetruecolor (int) $ args [0], (int) $ args [1]) or error ("Sorry, Parameter error! ");
If (! $ Args [2]) {
$ Color = hexdec ('7fffffff ');
Imagecolortransparent ($ im, $ color );
} Else {
$ Color = hexdec (str_replace ('#', '', $ args [2]);
}
$ This-> size = $ this-> size? $ This-> size: array (int) $ args [0], (int) $ args [1]);
Imagefill ($ im, 1, 1, $ color );
} Else {
Error ("Sorry, Parameter error! ");
}
// Imagealphablending ($ im, false); // These two rows are used to record transparent channels.
Imagesavealpha ($ im, true );
Imageinterlace ($ im, true); // enable interlace scan
Return $ im;
}

/*
* Generating thumbnails
* @ Param int $ w width of the new image
* @ Param int $ h width of the new image
* @ Param string/bool $ color (optional) specifies the background color of the new image. if it is false or empty, it is transparent.
* @ Param bool $ lashen (optional) whether to stretch. no stretch is performed by default.
*/
Function suolue ($ w = null, $ h = null, $ color = false, $ lashen = false ){
$ W_o = imagesx ($ this-> im );
$ H_o = imagesy ($ this-> im );
If ($ w = null and $ h! = Null ){
$ W = $ h * $ w_o/$ h_o;
} Elseif ($ w! = Null and $ h = null ){
$ H = $ w * $ h_o/$ w_o;
}
$ This-> size = null;
$ Im = $ this-> create ($ w, $ h, $ color );
$ W_n = $ w;
$ H_n = $ h;
If ($ w_o/$ h_o> $ w/$ h ){
$ H_n = $ w * $ h_o/$ w_o;
$ Y = ($ h-$ h_n)/2;
} Elseif ($ w_o/$ h_o <$ w/$ h ){
$ W_n = $ h * $ w_o/$ h_o;
$ X = ($ w-$ w_n)/2;
}
If ($ lashen ){
$ W_n = $ w;
$ H_n = $ h;
$ X = 0;
$ Y = 0;
}
Imagecopyresampled ($ im, $ this-> im, $ x, $ y, 0, $ w_n, $ h_n, $ w_o, $ h_o );
// Imagedestroy ($ this-> im );
$ This-> im = $ im;
Return $ im;
}

/*
* Write on an image
* @ Param string $ str string to be written
* @ Param array $ parameters related to arg string, which is an associated array. left is the left distance, right is the right distance, left is the first, and top is the top distance, bottom is the bottom distance, top priority; angle is the angle, color is the 6-digit hexadecimal color, touming is the text transparency, font is the font file
*/
Function write ($ str = '', $ arg = array ()){
$ Size = $ arg ['size']? $ Arg ['size']: 20;
$ Angle = $ arg ['ange']? $ Arg ['ange']: 0;
$ Color = $ arg ['color']? $ Arg ['color']: '20140901 ';
$ Touming = $ arg ['touming']? $ Arg ['touming']: 100;
$ Touming = dechex (100-$ touming) * 127/100 );
$ Color = hexdec ($ touming. str_replace ("#", "", $ color ));
$ Font = $ arg ['font']? $ Arg ['font']: 'Arial. ttf ';
$ Boxarr = imagettfbbox ($ size, $ angle, $ font, $ str );
$ W = imagesx ($ this-> im );
$ H = imagesy ($ this-> im );

$ X_l = $ x_r = $ boxarr [0];
$ Y_t = $ y_ B = $ boxarr [1];
For ($ I = 0; $ I <7; $ I = $ I + 2 ){
$ X_l = $ boxarr [$ I] <$ x_l? $ Boxarr [$ I]: $ x_l;
$ X_r = $ boxarr [$ I]> $ x_r? $ Boxarr [$ I]: $ x_r;
$ Y_t = $ boxarr [$ I + 1] <$ y_t? $ Boxarr [$ I + 1]: $ y_t;
$ Y_ B = $ boxarr [$ I + 1]> $ y_ B? $ Boxarr [$ I + 1]: $ y_ B;
}
$ Width = $ x_r-$ x_l;
$ Height = $ y_ B-$ y_t;

/* Get the exact offset */
$ Im = $ this-> create ($ width * 4, $ height * 4 );
$ Tm = hexdec ('7fffffff ');
Imagettftext ($ im, $ size, $ angle, $ width * 2, $ height * 2, $ color, $ font, $ str );
For ($ I = 0; $ I <$ width * 4; $ I ++ ){
For ($ ii = 0; $ ii <$ height * 4; $ ii ++ ){
If (imagecolorat ($ im, $ I, $ ii )! = $ Tm ){
$ X_l = $ I;
Break (2 );
}
}
}
For ($ I = 0; $ I <$ height * 4; $ I ++ ){
For ($ ii = $ x_l; $ ii <$ width * 4; $ ii ++ ){
If (imagecolorat ($ im, $ ii, $ I )! = $ Tm ){
$ Y_t = $ I;
Break (2 );
}
}
}
For ($ I = $ width * 4-1; $ I> 0; $ I --){
For ($ ii = $ y_t; $ ii <$ height * 4; $ ii ++ ){
If (imagecolorat ($ im, $ I, $ ii )! = $ Tm ){
$ X_r = $ I;
Break (2 );
}
}
}
For ($ I = $ height * 4-1; $ I> 0; $ I --){
For ($ ii = $ x_l; $ ii <= $ x_r; $ ii ++ ){
If (imagecolorat ($ im, $ ii, $ I )! = $ Tm ){
$ Y_ B = $ I;
Break (2 );
}
}
}
$ X_off = $ x_l-$ width * 2;
$ Y_off = $ y_ B-$ height * 2;
$ Width = $ x_r-$ x_l; // exact width
$ Height = $ y_ B-$ y_t; // precise height
Imagedestroy ($ im );

If (isset ($ arg ['left']) {
$ X = (int) $ arg ['left']-$ x_off;
} Elseif (isset ($ arg ['right']) {
$ X = $ w-(int) $ arg ['right']-$ width-$ x_off;
} Else {
$ X = ($ w-$ width)/2-$ x_off;
}
If (isset ($ arg ['top']) {
$ Y = (int) $ arg ['top']-$ y_off + $ height;
} Elseif (isset ($ arg ['bottom ']) {
$ Y = $ h-(int) $ arg ['bottom ']-$ y_off;
} Else {
$ Y = ($ h + $ height)/2-$ y_off;
}

Imagettftext ($ this-> im, $ size, $ angle, $ x, $ y, $ color, $ font, $ str );
Return $ this-> im;
}

/*
* Merge images (water Shadows)
* @ Param string/resource $ file path of the image file or the image identifier
* @ Param array $ parameters related to arg string, which is an associated array. left is the left distance, right is the right distance, left is the first, and top is the top distance, bottom is the distance from the bottom, top priority; touming is the text transparency
*/
Function merge ($ file, $ arg = array ()){
If (is_file ($ file )){
$ Imc = $ this-> create ($ file );
} Elseif (gettype ($ file) = 'resource '){
$ Imc = $ file;
} Else {
Error ("No image ");
}
$ Touming = $ arg ['touming']? (Int) $ arg ['touming']: 100;
$ W = imagesx ($ this-> im );
$ H = imagesy ($ this-> im );
$ Width = imagesx ($ imc );
$ Height = imagesy ($ imc );
If (isset ($ arg ['left']) {
$ X = (int) $ arg ['left'];
} Elseif (isset ($ arg ['right']) {
$ X = $ w-(int) $ arg ['right']-$ width;
} Else {
$ X = ($ w-$ width)/2;
}
If (isset ($ arg ['top']) {
$ Y = (int) $ arg ['top'];
} Elseif (isset ($ arg ['bottom ']) {
$ Y = $ h-$ height-$ arg ['bottom '];
} Else {
$ Y = ($ h-$ height)/2;
}
Imagecopymergegray ($ this-> im, $ imc, $ x, $ y, 0, $ width, $ height, $ touming );
}

/*
* Output image
* @ Param string $ type
* @ Param string $ path of the file to be stored in filename
* @ Param int $ zhiliang jpeg image exclusive, image definition
*/
Function display ($ type = null, $ filename = null, $ zhiliang = null ){
If ($ type = null ){
$ Type = $ this-> type? $ This-> type: 'jpg ';
}
If ($ type = 'jpeg 'or $ type = 'jpg') and $ zhiliang = null ){
$ Type = 'jpeg ';
$ Zhiliang = 100;
}
If ($ filename = null ){
Header ('content-type: image/'. $ type );
}
$ Displayfunc = 'image'. $ type;
$ Displayfunc ($ this-> im, $ filename, $ zhiliang );
Imagedestroy ($ this-> im );
}

Function randcolor ($ a, $ B ){
$ A = $ a> 255? 255: (int) $;
$ A = $ a <0? 0: (int) $;
$ B = $ B> 255? 255: (int) $ B;
$ B = $ B <0? 0: (int) $ B;
For ($ I = 0; $ I <3; $ I ++ ){
$ Color. = str_pad (dechex (mt_rand ($ a, $ B), 2, "0", STR_PAD_LEFT );
}
Return $ color;
}
}

/*
Function error ($ msg, $ debug = false ){
$ Err = new Exception ($ msg );
$ Str ="

\ N error: \ n ". print_r ($ err-> getTrace (), 1)." \ n
";
If ($ debug = true ){
File_put_contents (date ('Y-m-D'). ". log", $ str );
Return $ str;
} Else {
Die ($ str );
}
}
*/
?>


This is a simple usage example.

The code is as follows:


$ Img = new img('a.png ');
$ M = $ img-> im;
$ Im = $ img-> suolue (100 );
$ Img-> im = $ m;
$ Img-> suolue (300 );
$ Img-> merge ($ m, array ('left' => 0, 'top' => 0, 'touming' => 60 ));
$ Img-> merge ($ im, array ('right' => 0, 'top' => 0, 'touming' => 60 ));
$ Img-> merge ($ im, array ('left' => 0, 'bottom '=> 0, 'touming' => 60 ));
$ Img-> merge ($ im, array ('right' => 0, 'bottom '=> 0, 'touming' => 60 ));

$ Img-> write ("Spring is Coming", array ('left' => 0, 'top' => 0, 'size' => 30, 'color' => $ img-> randcolor (0,180), 'angle '=>-45, 'font' => 'simfang. ttf', 'touming' => 80 ));
$ Img-> write ("Spring is Coming", array ('left' => 0, 'bottom '=> 0, 'size' => 30, 'color' => $ img-> randcolor (0,180), 'angle '=> 45, 'font' => 'simfang. ttf', 'touming' => 80 ));
$ Img-> write ("Spring is Coming", array ('right' => 0, 'bottom '=> 0, 'size' => 30, 'color' => $ img-> randcolor (0,180), 'angle '=>-45, 'font' => 'simfang. ttf', 'touming' => 80 ));
$ Img-> write ("Spring is Coming", array ('right' => 0, 'top' => 0, 'size' => 30, 'color' => $ img-> randcolor (0,180), 'angle '=> 45, 'font' => 'simfang. ttf', 'touming' => 80 ));
$ Img-> display ("gif ");

The http://www.bkjia.com/PHPjc/320104.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/320104.htmlTechArticle code is as follows :? Php/** @ author sleep 27262681@qq.com * copyrighthttp: // www.gowake.cn */class img {function _ construct ($ arg = null) {$ args = func_get_args (); if ($...

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.