PHP uploads pictures and adds watermarks (picture watermarks, text watermarks)_PHP tutorial

WBOY
Release: 2016-07-13 10:45:07
Original
832 people have browsed it

php upload pictures and add watermarks (picture watermarks, text watermarks) This is a relatively complete software that automatically adds watermarks to pictures when users upload them. This watermark adding function can add text watermarks and picture watermarks.

php tutorial to upload pictures and add watermarks (picture watermarks, text watermarks)
This is a relatively complete software that automatically adds watermarks to pictures when users upload them. This watermark adding function can add text watermarks and picture watermarks.

/*
* created on 2010-6-21
*
* the class for control image
*
* made by s71ence
*
* @$img_path image path
* @$is_auto_reduce Whether the image is automatically compressed according to the size level 1 is
* @$is_appoint Whether to manually compress or amplify 1 Yes
* @$multiple Manually specify compression/amplification ratio
* @$is_water_str Whether to add watermark text 1 is
* @$water_str watermark text
* @$is_watermark Whether to add watermark to the picture 1 Yes
* @$logo_path Watermark image path
* @$is_display Whether to display pictures 1 is
* @$is_create Whether to generate compressed images 1 is
*
* Note:
* 1. Images cannot be displayed when generating new images, that is, $isdisplay and $iscreate cannot be set to 1 at the same time
* 2. When the image width or height is less than 1000, you need to manually set the compression ratio for compression
* 3. It is not recommended to enable watermarks. If you want to enable it, it is recommended that the original image size should be within 1000
* 4. The watermark text cannot contain Chinese
* 5. The newly generated image is in the original directory file and supports 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 image
$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 image, text size, text rotation, watermark starting coordinate x, watermark starting coordinate 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);
}

//Picture 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 image, watermark image, watermark coordinate x, watermark coordinate y, watermark image start coordinate x, watermark image start coordinate y, 'watermark image width', 'watermark image height');
Imagecopy($img_get,$logo,0,0,0,0,$logo_info[0],$logo_info[1]);
}

//Automatic image compression Automatic compression based on image size classification
//imagecopyresized(canvas, original image, canvas starting x coordinate, canvas starting y coordinate, original image starting x coordinate, original image starting x coordinate, new image width, new image height, original image width, original image height );
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 image compression
//imagecopyresized(canvas, original image, canvas starting x coordinate, canvas starting y coordinate, original image starting x coordinate, original image starting x coordinate, new image width, new image height, original image width, original image height );
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 "Image generated successfully!
New image: ".$new_img_path."
Original image: ".$this->img_path;
}
else
{
Return "The image generation failed, please check whether the configuration is correct!";
}
}
}

function __desctruct()
{
//clear
}
}

//Call method

/* $img_path="../users/user_photo/t2.jpg"; //The path of the image being manipulated
$is_auto_reduce=1;//Whether the image is automatically compressed according to the size level 1 is
$is_appoint=0;//Whether to compress manually 1 is
$multiple=0.5;//Manually specify the compression ratio
$is_water_str=0;//Whether to add watermark text
$water_str="www.bKjia.c0m";//Watermark text
$is_watermark=0;//Whether to add watermark to the picture 1 is
$logo_path="../image/logo_about.gif";//Watermark image path
$is_display=0;//Whether to display the picture 1 is
$is_create=1;//Whether to generate compressed images 1 is
$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();*/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633043.htmlTechArticlephp upload pictures and add watermarks (picture watermarks, text watermarks) This is a relatively complete and reasonable method for users to upload pictures Automatically add watermarks to pictures whenever you want. This watermark adding function can add text...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!