PHP add watermark to picture six

Source: Internet
Author: User
Tags abs add watermark to picture imagecopy mkdir

?

function _countmaskpos ()


{


if ($this->_isfull ())


{


Switch ($this->mask_position)


{


Case 1:


Top left


$this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;


$this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;


Break





Case 2:


Lower left


$this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;


$this->mask_pos_y = $this->src_h-$this->mask_h-$this->mask_offset_y;


Break





Case 3:


Top Right


$this->mask_pos_x = $this->src_w-$this->mask_w-$this->mask_offset_x;


$this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;


Break





Case 4:


Lower right


$this->mask_pos_x = $this->src_w-$this->mask_w-$this->mask_offset_x;


$this->mask_pos_y = $this->src_h-$this->mask_h-$this->mask_offset_y;


Break





Default


By default, the watermark is placed to the lower right, offset the specified pixel


$this->mask_pos_x = $this->src_w-$this->mask_w-$this->mask_offset_x;


$this->mask_pos_y = $this->src_h-$this->mask_h-$this->mask_offset_y;


Break


}


}


Else


{


Switch ($this->mask_position)


{


Case 1:


Top left


$this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;


$this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;


Break





Case 2:


Lower left


$this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;


$this->mask_pos_y = $this->dst_h-$this->mask_h-$this->mask_offset_y-$this->img_border_size;


Break





Case 3:


Top Right


$this->mask_pos_x = $this->dst_w-$this->mask_w-$this->mask_offset_x-$this->img_border_size;


$this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;


Break





Case 4:


Lower right


$this->mask_pos_x = $this->dst_w-$this->mask_w-$this->mask_offset_x-$this->img_border_size;


$this->mask_pos_y = $this->dst_h-$this->mask_h-$this->mask_offset_y-$this->img_border_size;


Break





Default


By default, the watermark is placed to the lower right, offset the specified pixel


$this->mask_pos_x = $this->dst_w-$this->mask_w-$this->mask_offset_x-$this->img_border_size;


$this->mask_pos_y = $this->dst_h-$this->mask_h-$this->mask_offset_y-$this->img_border_size;


Break


}


}


}





/**


* Set Font information


*/


function _setfontinfo ()


{


if (Is_numeric ($this->font))


{


$this->font_w = imagefontwidth ($this->font);


$this->font_h = imagefontheight ($this->font);





Calculate the width of watermark font height


$word _length = strlen ($this->mask_word);


$this->mask_w = $this->font_w* $word _length;


$this->mask_h = $this->font_h;


}


Else


{


$arr = Imagettfbbox ($this->font_size,0, $this->font, $this->mask_word);


$this->mask_w = ABS ($arr [0]-$arr [2]);


$this->mask_h = ABS ($arr [7]-$arr [1]);


}


}





/**


* Set the new diagram size


*


* @param integer $img _w target width


* @param integer $img _h target height


*/


function _setnewimgsize ($img _w, $img _h=null)


{


$num = Func_num_args ();


if (1 = $num)


{


$this->img_scale = $img _w;//width as a proportion


$this->fill_w = Round ($this->src_w * $this->img_scale/100)-$this->img_border_size*2;


$this->fill_h = Round ($this->src_h * $this->img_scale/100)-$this->img_border_size*2;





Source file Start coordinates


$this->src_x = 0;


$this->src_y = 0;


$this->copy_w = $this->src_w;


$this->copy_h = $this->src_h;





Target size


$this->dst_w = $this->fill_w + $this->img_border_size*2;


$this->dst_h = $this->fill_h + $this->img_border_size*2;


}





if (2 = $num)


{


$fill _w = (int) $img _w-$this->img_border_size*2;


$fill _h = (int) $img _h-$this->img_border_size*2;


if ($fill _w < 0 | | $fill _h < 0)


{


Die ("The picture border is too large to exceed the width of the picture");


}


$rate _w = $this->src_w/$fill _w;


$rate _h = $this->src_h/$fill _h;





Switch ($this->cut_type)


{


Case 0:


If the artwork is larger than the thumbnail, it shrinks, otherwise it doesn't shrink.


if ($rate _w < 1 && $rate _h < 1)


{


$this->fill_w = (int) $this->src_w;


$this->fill_h = (int) $this->src_h;


}


Else


{


if ($rate _w >= $rate _h)


{


$this->fill_w = (int) $fill _w;


$this->fill_h = round ($this->src_h/$rate _w);


}


Else


{


$this->fill_w = round ($this->src_w/$rate _h);


$this->fill_h = (int) $fill _h;


}


}





$this->src_x = 0;


$this->src_y = 0;





$this->copy_w = $this->src_w;


$this->copy_h = $this->src_h;





Target size


$this->dst_w = $this->fill_w + $this->img_border_size*2;


$this->dst_h = $this->fill_h + $this->img_border_size*2-10;//compensation round Error elimination white edge;


Break





Automatic Trimming


Case 1:


If the picture is to shrink the cut before you operate


if ($rate _w >= 1 && $rate _h >=1)


{


if ($this->src_w > $this->src_h)


{


$src _x = round ($this->src_w-$this->src_h)/2;


$this->setsrccutposition ($src _x, 0);


$this->setrectanglecut ($fill _h, $fill _h);





$this->copy_w = $this->src_h;


$this->copy_h = $this->src_h;





}


ElseIf ($this->src_w < $this->src_h)


{


$src _y = round ($this->src_h-$this->src_w)/2;


$this->setsrccutposition (0, $src _y);


$this->setrectanglecut ($fill _w, $fill _h);





$this->copy_w = $this->src_w;


$this->copy_h = $this->src_w;


}


Else


{


$this->setsrccutposition (0, 0);


$this->copy_w = $this->src_w;


$this->copy_h = $this->src_w;


$this->setrectanglecut ($fill _w, $fill _h);


}


}


Else


{


$this->setsrccutposition (0, 0);


$this->setrectanglecut ($this->src_w, $this->src_h);





$this->copy_w = $this->src_w;


$this->copy_h = $this->src_h;


}





Target size


$this->dst_w = $this->fill_w + $this->img_border_size*2;


$this->dst_h = $this->fill_h + $this->img_border_size*2;





Break





Hand cut


Case 2:


$this->copy_w = $this->fill_w;


$this->copy_h = $this->fill_h;





Target size


$this->dst_w = $this->fill_w + $this->img_border_size*2;


$this->dst_h = $this->fill_h + $this->img_border_size*2;





Break


Default


Break





}


}





Target file Start coordinates


$this->start_x = $this->img_border_size;


$this->start_y = $this->img_border_size;


}





/**


* Check whether the watermark is larger than the resulting picture width


*/


function _isfull ()


{


Return ($this->mask_w + $this->mask_offset_x > $this->fill_w


|| $this->mask_h + $this->mask_offset_y > $this->fill_h)


? true:false;


}





/**


* Check whether the watermark is larger than the original image


*/


function _checkmaskvalid ()


{


if ($this->mask_w + $this->mask_offset_x > $this->src_w


|| $this->mask_h + $this->mask_offset_y > $this->src_h)


{


Die ("watermark picture size larger than the original image, please reduce the watermark map");


}


}





/**


* Get Picture type


*


* @param string $file _path file path


*/


function _getimgtype ($file _path)


{


$type _list = Array ("1" => "gif", "2" => "jpg", "3" => "PNG", "4" => "SWF", "5" => "PSD", "6" => "BMP", "" "=> "Wbmp");


if (file_exists ($file _path))


{


$img _info = @getimagesize ($file _path);


if (Isset ($type _list[$img _info[2]))


{


return $type _list[$img _info[2]];


}


}


Else


{


Die ("file does not exist, cannot get file type!");


}


}





/**


* Check the picture type is legitimate, call the Array_key_exists function, this function requires


* PHP version is larger than 4.1.0


*


* @param string $img _type file type


*/


function _checkvalid ($img _type)


{


if (!array_key_exists ($img _type, $this->all_type))


{


return false;


}


}





/**


* Generate directory by specified path


*


* @param string $path path


*/


function _mkdirs ($path)


{


$adir = explode ('/', $path);


$dirlist = ';


$rootdir = Array_shift ($adir);


if ($rootdir!= '. ') $rootdir!= ' ... ') &&!file_exists ($rootdir))


{


@mkdir ($rootdir);


}


foreach ($adir as $key => $val)


{


if ($val!= '. ') && $val!= ' ... ')


{


$dirlist. = "/". $val;


$dirpath = $rootdir. $dirlist;


if (!file_exists ($dirpath))


{


@mkdir ($dirpath);


@chmod ($dirpath, 0777);


}


}


}


}





/**


* Flip Vertically


*


* @param string $src picture source


*/


function _FLIPV ($SRC)


{


$src _x = $this->getimgwidth ($SRC);


$src _y = $this->getimgheight ($SRC);





$new _im = Imagecreatetruecolor ($src _x, $src _y);


for ($y = 0; $y < $src _y; $y + +)


{


Imagecopy ($new _im, $src, 0, $src _y-$y-1, 0, $y, $src _x, 1);


}


$this->h_src = $new _im;


}





/**


* Flip Horizontally


*


* @param string $src picture source


*/


function _fliph ($SRC)


{


$src _x = $this->getimgwidth ($SRC);


$src _y = $this->getimgheight ($SRC);





$new _im = Imagecreatetruecolor ($src _x, $src _y);


for ($x = 0; $x < $src _x; $x + +)


{


Imagecopy ($new _im, $src, $src _x-$x-1, 0, $x, 0, 1, $src _y);


}


$this->h_src = $new _im;


}


}


?>

Here's how to use it:

Require_once (' lib/imagehandler.phpcls ');
$imgHandler _obj = new ImageHandler ();

/* Default Zoom zoom/
$imgHandler _obj->setsrcimg ("img/xrk.jpg");
$imgHandler _obj->setdstimg ("tmp/new_image_0.jpg");
$imgHandler _obj->setcuttype (0);
Specify zoom proportions
$imgHandler _obj->createimg (20);//50 100 200 Try

/* 1, Text watermark image shrink

$imgHandler _obj->setsrcimg ("img/xrk.jpg");
$imgHandler _obj->setmaskfont ("Fonts/stxingka. TTF ");
$imgHandler _obj->setmaskfontsize (15);
$imgHandler _obj->setmaskfontcolor ("#0061A7");
$imgHandler _obj->setmasktxtpct (20);
$imgHandler _obj->setdstimgborder (0, "#dddddd");
$imgHandler _objext = "Happy Rambler";
$str = mb_convert_encoding ($imgHandler _objext, "UTF-8", "GBK");
$imgHandler _obj->setmaskword ($imgHandler _objext);

Specify fixed-width-high output to browsers
$imgHandler _obj->createimg (400,400);
*/


/* 2, add watermark image Zoom Out
$imgHandler _obj->setsrcimg ("img/test.jpg");
$imgHandler _obj->setdstimg ("tmp/new_image_2.jpg");
$imgHandler _obj->setmaskimg ("Img/t.gif");
$imgHandler _obj->setmaskposition (0);//0 to add a watermark to the lower right corner of the picture
$imgHandler _obj->setmaskimgpct (60);
$imgHandler _obj->setdstimgborder (1, "#dddddd");

Specify zoom proportions
$imgHandler _obj->createimg (400,400);
*/

/* 3, Sync shrink
$imgHandler _obj->setsrcimg ("img/test.jpg");
$imgHandler _obj->setcuttype (0);//sync Shrink
$imgHandler _obj->setdstimg ("tmp/new_image_3.jpg");
$imgHandler _obj->createimg (150,160);

*/

/* 4, trim picture
  $imgHandler _obj->setsrcimg ("img/test.jpg");
    $imgHandler _obj-> Setcuttype (2);//indicates manual trim
    $imgHandler _obj->setsrccutposition (50, 50);//source map start coordinates
     $imgHandler _obj->setrectanglecut (300, 200);//Trim size
    $imgHandler _obj-> Setdstimg ("tmp/new_image_4.jpg");
    $imgHandler _obj->createimg (300,200);
*/

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.