PHP Upload image plus watermark (image watermark, text watermark) _php Tutorial

Source: Internet
Author: User
Tags imagecopy imagejpeg
PHP Upload image plus watermark (image watermark, text watermark) This is a more complete rationale in the user upload pictures automatically to the image to increase the water mark, this increase watermark function can increase the text watermark and picture watermark Oh.

PHP Tutorial upload image plus watermark (image watermark, text watermark)
This is a more complete rationale in the user upload pictures automatically to the image to increase the water mark, this increase watermark function can increase the text watermark and image watermark Oh.

/*
* Created on 2010-6-21
*
* The class for control image
*
* Made by S71ence
*
* @ $img _path picture path
* @ $is _auto_reduce pictures are automatically compressed by size level 1 Yes
* @ $is _appoint whether to manually compress or enlarge 1 yes
* @ $multiple Manually specify the compression/magnification ratio
* @ $is _water_str watermark Text 1 Yes
* @ $water _str watermark Text
* @ $is _watermark watermark Picture 1 Yes
* @ $logo _path watermark Picture Path
* @ $is _display whether to show picture 1 yes
* @ $is _create whether to generate compressed picture 1 Yes
*
Note
* 1. Images cannot be displayed when generating new diagrams, i.e. $isdisplay and $iscreate cannot be set to 1 simultaneously
* 2. When the picture width or height is less than 1000, you need to manually set the compression ratio to compress
* 3. Do not recommend the watermark, to enable, suggest the original picture size preferably within 1000
* 4. The watermark text cannot contain Chinese
* 5. Newly generated images in the original directory file, support n levels
*/

Class Image_control
{
Private $img _path;
Private $is _auto_reduce;
Private $is _appoint;
Private $multiple;
Private $is _water_str;
Private $water _str;
Private $is _watermark;
Private $logo _path;
Private $is _display;
Private $is _create;

function __construct ($img _path, $is _auto_reduce, $is _appoint, $multiple, $is _water_str, $water _str, $is _watermark,$ Logo_path, $is _display, $is _create)
{
$this->img_path= $img _path;
$this->is_auto_reduce= $is _auto_reduce;
$this->is_appoint= $is _appoint;
$this->multiple= $multiple;
$this->is_water_str= $is _water_str;
$this->water_str= $water _str;
$this->is_watermark= $is _watermark;
$this->logo_path= $logo _path;
$this->is_display= $is _display;
$this->is_create= $is _create;
}

function Img_control ()
{
Get the original
$img _info=getimagesize ($this->img_path);

Switch ($img _info[2])
{
Case 1:
$img _get= @imagecreatefromgif ($this->img_path);
Break

Case 2:
$img _get= @imagecreatefromjpeg ($this->img_path);
Break

Case 3:
$img _get= @imagecreatefrompng ($this->img_path);
Break
}

Text watermark
if ($this->is_water_str==1)
{
Imagettftext (original, text size, text rotation, watermark starting coordinates x, watermark starting coordinates y, $te, ' Simhei.ttf ', $str);
$te =imagecolorallocate ($img _get,255,255,255);
$str =iconv ("GBK", "Utf-8", $this->water_str);//Watermark Text
Imagettftext ($img _get,16,0, $img _info[0]-200, $img _info[1]-20, $te, ' Msyh.ttf ', $str);
}

Image watermark
if ($this->is_watermark==1)
{
Watermark Image Processing
$logo _info=getimagesize ($this->logo_path);

Switch ($logo _info[2])
{
Case 1:
$logo = @imagecreatefromgif ($this->logo_path);
Break

Case 2:
$logo = @imagecreatefromjpeg ($this->logo_path);
Break

Case 3:
$logo = @imagecreatefrompng ($this->logo_path);
Break
}

Watermark logo Image
Function Description: Imagecopy (original, watermark image, watermark coordinates x, watermark coordinates y, watermark picture start coordinate x, watermark picture start coordinate y, ' watermark picture width ', ' watermark Picture High ');
Imagecopy ($img _get, $logo, 0,0,0,0, $logo _info[0], $logo _info[1]);
}

Automatic picture compression is automatically compressed by image size Rating
Imagecopyresized (Canvas, original, canvas starting x-coordinate, canvas starting y-coordinate, original x-coordinate, original x-coordinate, new image width, new picture height, original image width, original picture high);
if ($this->is_auto_reduce==1)
{
if ($img _info[0]>=3000 | | $img _info[1]>=3000)
{
$new _image_get=imagecreatetruecolor ($img _info[0]*0.03, $img _info[1]*0.03);//Generate Canvas
Imagecopyresized ($new _image_get, $img _get,0,0,0,0, $img _info[0]*0.03, $img _info[1]*0.03, $img _info[0], $img _info[1] );
}
else if ($img _info[0]>=2500 | | $img _info[1]>=2500)
{
$new _image_get=imagecreatetruecolor ($img _info[0]*0.04, $img _info[1]*0.04);
Imagecopyresized ($new _image_get, $img _get,0,0,0,0, $img _info[0]*0.04, $img _info[1]*0.04, $img _info[0], $img _info[1] );
}
else if ($img _info[0]>=2000 | | $img _info[1]>=2000)
{
$new _image_get=imagecreatetruecolor ($img _info[0]*0.05, $img _info[1]*0.05);
Imagecopyresized ($new _image_get, $img _get,0,0,0,0, $img _info[0]*0.05, $img _info[1]*0.05, $img _info[0], $img _info[1] );
}
else if ($img _info[0]>=1500 | | $img _info[1]>=1500)
{
$new _image_get=imagecreatetruecolor ($img _info[0]*0.08, $img _info[1]*0.08);
Imagecopyresized ($new _image_get, $img _get,0,0,0,0, $img _info[0]*0.08, $img _info[1]*0.08, $img _info[0], $img _info[1] );
}
else if ($img _info[0]>=1000 | | $img _info[1]>=1000)
{
$new _image_get=imagecreatetruecolor ($img _info[0]*0.1, $img _info[1]*0.1);
Imagecopyresized ($new _image_get, $img _get,0,0,0,0, $img _info[0]*0.1, $img _info[1]*0.1, $img _info[0], $img _info[1]);
}
else if ($img _info[0]>=500 | | $img _info[1]>=500)
{
$new _image_get=imagecreatetruecolor ($img _info[0]*0.2, $img _info[1]*0.2);
Imagecopyresized ($new _image_get, $img _get,0,0,0,0, $img _info[0]*0.2, $img _info[1]*0.2, $img _info[0], $img _info[1]);
}
else if ($img _info[0]>=300 | | $img _info[1]>=300)
{
$new _image_get=imagecreatetruecolor ($img _info[0]*0.3, $img _info[1]*0.3);
Imagecopyresized ($new _image_get, $img _get,0,0,0,0, $img _info[0]*0.3, $img _info[1]*0.3, $img _info[0], $img _info[1]);
}
Else
{
$new _image_get=imagecreatetruecolor ($img _info[0]*1, $img _info[1]*1);
Imagecopyresized ($new _image_get, $img _get,0,0,0,0, $img _info[0]*1, $img _info[1]*1, $img _info[0], $img _info[1]);
}
}

Manual picture Compression
Imagecopyresized (Canvas, original, canvas starting x-coordinate, canvas starting y-coordinate, original x-coordinate, original x-coordinate, new image width, new picture height, original image width, original picture high);
if ($this->is_appoint)
{
$new _image_get=imagecreatetruecolor ($img _info[0]* $this->multiple, $img _info[1]* $this->multiple);//Generate Canvas
Imagecopyresized ($new _image_get, $img _get,0,0,0,0, $img _info[0]* $this->multiple, $img _info[1]* $this Multiple, $img _info[0], $img _info[1]);
}

Image output
if ($this->is_display==1)
{
Header ("Content-type:image/jpeg");
Return imagejpeg ($new _image_get);
}

New Image generation
if ($this->is_create==1)
{
$new _name=explode ("/", $this->img_path);
$new _name_string= "";

for ($i =0; $i<>
{
$new _name_string.= $new _name[$i]. " /";
}

$new _img_path= $new _name_string. " New ". $new _name[$i];

if (imagejpeg ($new _image_get, $new _img_path) && imagejpeg ($img _get, $this->img_path))
{
Setcookie ("Img_new_path", $new _img_path);
Return "Picture generated successfully!
New figure: ". $new _img_path."
Original: ". $this->img_path;
}
Else
{
Return "picture generation failed, please check the configuration is correct!";
}
}
}

function __desctruct ()
{
Clear
}
}

Calling methods

/* $img _path= ". /users/user_photo/t2.jpg "; Manipulated picture Path
$is _auto_reduce=1;//pictures are automatically compressed by size level 1 Yes
$is _appoint=0;//whether to manually compress 1 is
$multiple =0.5;//Manually specify the compression scale
$is _water_str=0;//whether to add watermark text
$water _str= "www.bKjia.c0m";//Watermark Text
$is _watermark=0;//whether to add a watermark picture 1 is
$logo _path= ". /image/logo_about.gif ";//Watermark Picture Path
$is _display=0;//whether to show picture 1 yes
$is _create=1;//whether to generate compressed picture 1 Yes
$img =new Image_control ($img _path, $is _auto_reduce, $is _appoint, $multiple, $is _water_str, $water _str, $is _watermark,$ Logo_path, $is _display, $is _create);
echo $img->img_control (); */

http://www.bkjia.com/PHPjc/633043.html www.bkjia.com true http://www.bkjia.com/PHPjc/633043.html techarticle php upload image plus watermark (image watermark, text watermark) This is a more complete rationale in the user upload pictures automatically to the image to increase the water mark, this increase the watermark function can increase the text ...

  • Related Article

    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.