Copy CodeThe code is as follows:
/*
* @author night without sleep 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 ("No such method or function");
}
Return Call_user_func_array ($funcstr, $arg);
}
/*
* Create images
* @param string/int picture file path or width
* @param int height, can be omitted
* @param string 6-bit 16 binary integer
*/
function Create ($arg = null) {
$args = Func_get_args ();
if (Is_file ($args [0])) {
$this->file = $args [0];
$size = getimagesize ($this->file) or error (' Picture 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 is wrong! ");
}
Imagealphablending ($im, false);//These two lines are used to record transparent channels
Imagesavealpha ($im, true);
Imageinterlace ($im, true);//Turn on interlaced scanning
return $im;
}
/*
* Generate thumbnail image
* @param int $w The width of the new picture
* @param int $h The width of the new picture
* @param string/bool $color Optional, new picture background color, false or empty for transparent
* @param bool $lashen Optional, whether stretched, not stretched 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,0, $w _n, $h _n, $w _o, $h _o);
Imagedestroy ($this->im);
$this->im = $im;
return $im;
}
/*
* Write on the picture
* @param string $str to be written
* @param array $arg string-related parameters, is an associative array, left is the distance from the right, the distance is from the right, the leftmost priority, top is the distance from the top, bottom is the distance from the bottom, top priority; angle is the angle, Color is 6-digit 16 colors, touming is text transparency, font is fonts file
*/
function Write ($str = ", $arg = Array ()) {
$size = $arg [' Size ']? $arg [' Size ']: 20;
$angle = $arg [' Angle ']? $arg [' angle ']: 0;
$color = $arg [' Color ']? $arg [' Color ']: ' 000000 ';
$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 exact offset */
$im = $this->create ($width, $height);
$tm = Hexdec (' 7fffffff ');
Imagettftext ($im, $size, $angle, $width * * *, $height * *, $color, $font, $STR);
for ($i =0; $i < $width, $i + +) {
for ($ii =0; $ii < $height, $ii + +) {
if (Imagecolorat ($im, $i, $ii)! = $tm) {
$x _l = $i;
Break (2);
}
}
}
for ($i =0; $i < $height, $i + +) {
for ($ii = $x _l; $ii < $width; $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; $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 * *;
$y _off = $y _b-$height * *;
$width = $x _r-$x _l; Precision 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 picture (piece of water shadow)
* @param string/resource $file picture file path or this picture identifier
* @param array $arg string-related parameters, is an associative array, left is the distance from the right side, the distance to the front, and the top is the distance from the edge, bottom is the distance from the bottom, top priority; touming is text transparency
*/
function merge ($file, $arg = Array ()) {
if (Is_file ($file)) {
$IMC = $this->create ($file);
}elseif (GetType ($file) = = ' Resource ') {
$IMC = $file;
}else {
Error ("no picture");
}
$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,0, $width, $height, $touming);
}
/*
* Output Image
* @param string $type
* @param string $filename The file path to be dumped
* @param int $zhiliang jpeg picture-specific, image sharpness
*/
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 = $a <0? 0: (int) $a;
$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 = "
\ nthe 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 an example of a simple usage
Copy CodeThe 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 Comes", array (' left ' =>0, ' top ' =>0, ' size ' =>30, ' color ' = $img->randcolor (0,180), ' Angle ' =>-45, ' font ' = ' simfang.ttf ', ' touming ' =>80);
$img->write ("Spring Comes", array (' left ' =>0, ' bottom ' =>0, ' size ' =>30, ' color ' = $img->randcolor (0,180), ' Angle ' =>45, ' font ' = ' simfang.ttf ', ' touming ' =>80);
$img->write ("Spring Comes", array (' Right ' =>0, ' bottom ' =>0, ' size ' =>30, ' color ' = $img->randcolor (0,180), ' Angle ' =>-45, ' font ' = ' simfang.ttf ', ' touming ' =>80);
$img->write ("Spring Comes", array (' Right ' =>0, ' top ' =>0, ' size ' =>30, ' color ' = = $img->randcolor (0,180), ' Angle ' =>45, ' font ' = ' simfang.ttf ', ' touming ' =>80);
$img->display ("gif");
http://www.bkjia.com/PHPjc/320104.html www.bkjia.com true http://www.bkjia.com/PHPjc/320104.html techarticle Copy the code as follows: PHP/* * @author sleepless 27262681@qq.com *copyrighthttp://www.gowake.cn */class img {function __construct ($arg = null) {$args = Func_get_args (); if ($a ...