Home > Backend Development > PHP Tutorial > PHP function code for adding watermark function and thumbnail to pictures_PHP tutorial

PHP function code for adding watermark function and thumbnail to pictures_PHP tutorial

WBOY
Release: 2016-07-20 11:07:02
Original
855 people have browsed it

php function code for adding watermark function and thumbnail to pictures
/**
* Add watermark to the image
* @param string $desImg The target image parameter format is ./images/pic.jpg
* @param string $waterImg The watermark image parameter format is the same as above, and the watermark image is in png format. Background transparent
* @param int positon Watermark position 1: Top left 2: Top right 3: Center 4: Bottom left 5: Bottom right
* @param bool $saveas Whether to save as, default value false, Indicates overwriting the original image
* @param int $alpha The opacity of the watermark image
* @return string $savepath The path of the new image
* **/
function watermark($desImg,$waterImg,$positon=1,$saveas=false,$alpha =30)
{
//Get the basic information of the target image
$temp=pathinfo($desImg);
$name=$temp["basename"];//File name
$path=$temp["dirname"];//The folder where the file is located
$extension=$temp["extension"];//File extension
if($saveas)
{
//Need to save as
$name=rtrim($name,".$extension")."_2.";//Rename
$savepath=$path."/".$name .$extension;
}
else
{
//Overwrite the original image if there is no need to save as
$savepath=$path."/".$name;
}
$info=getImageInfo($desImg);//Get the information of the target image
$info2=getImageInfo($waterImg);//Get the information of the watermark image

$desImg=create($desImg );//Create from the original image
$waterImg=create($waterImg);//Create from the watermark image
//Position 1: top left
if($positon==1)
{
$x=0;
$y=0;
}
//Position 2: Top right
if($positon==2)
{
$x=$info[0]-$info2[0];
$y=0;
}
//Position 3: Centered
if($positon==3)
{
$x=($info[0]-$info2[0])/2;
$y=($info[1]-$info2[1])/2;
}
//Position 4: bottom left
if($positon==4)
{
$x=0;
$y=$info[1]-$info2[1];
}
//Position 5: bottom right
if($positon==5)
{
$x=$info[0]-$info2[0];
$y=$info[1]-$info2[1];
}
imagecopymerge($desImg,$waterImg,$x,$y,0,0,$info2[0],$info2[ 1],$alpha);
imagejpeg($desImg,$savepath);
imagedestroy($desImg);
imagedestroy($waterImg);
return $savepath;
}
/**
* Get image information, width, height, image/type
* @param string $src image path
* @return array
* **/
function getImageInfo($src)
{
return getimagesize($src);
}
/**
* Create an image and return the resource type
* @param string $src Image path
* @return resource $im Return the resource type
* **/
function create($src)
{
$info=getImageInfo($src);
switch ($info[2])
{
case 1:
$im=imagecreatefromgif ($src);
break;
case 2:
$im=imagecreatefromjpeg($src);
break;
case 3:
$im=imagecreatefrompng($src) ;
break;
}
return $im;
}
/**
* Thumbnail main function
* @param string $src image path
* @param int $w thumbnail width
* @param int $h thumbnail height
* @return mixed Return thumbnail path
* **/

function resize($src,$w,$h)
{
$temp=pathinfo($src);
$name=$temp["basename"];//File name
$dir=$temp["dirname"];// The folder where the file is located
$extension=$temp["extension"];//File extension
$savepath="{$dir}/{$name}.thumb.jpg";//Thumbnail Save path, the new file name is *.thumb.jpg

//Get the basic information of the image
$info=getImageInfo($src);
$width=$info[0]; //Get the image width
$height=$info[1];//Get the image height
$per1=round($width/$height,2);//Calculate the original image aspect ratio
$per2=round($w/$h,2);//Calculate the thumbnail aspect ratio

//Calculate the scaling ratio
if($per1>$per2||$per1==$ per2)
{
//The aspect ratio of the original image is greater than or equal to the aspect ratio of the thumbnail, then the width will be given priority
$per=$w/$width;
}
if( $per1<$per2)
{
//The aspect ratio of the original image is smaller than the aspect ratio of the thumbnail, then the height priority is given
$per=$h/$height;
}
$temp_w=intval($width*$per);//Calculate the width of the original image after scaling
$temp_h=intval($height*$per);//Calculate the height of the original image after scaling
$temp_img =imagecreatetruecolor($temp_w,$temp_h);//Create canvas
$im=create($src);
imagecopyresampled($temp_img,$im,0,0,0,0,$temp_w,$temp_h ,$width,$height);
if($per1>$per2)
{
imagejpeg($temp_img,$savepath);
return addBg($savepath,$w,$h, "w");
//Width priority, add the background if the height is insufficient after scaling
}
if($per1==$per2)
{
imagejpeg($ temp_img,$savepath);
return $savepath;
//proportional scaling
}
if($per1<$per2)
{
imagejpeg($temp_img,$savepath );

return addBg($savepath,$w,$h,"h");
//Height priority, if the width is insufficient after scaling, add the background
}
}
/**
* Add background
* @param string $src image path
* @param int $w background image width
* @param int $h background image height
* @param String $first Determines the final position of the image, w width takes precedence h height takes precedence wh: proportional
* @return returns the image with background
* **/
function addBg($src,$w,$h,$fisrt="w")
{
$bg=imagecreatetruecolor($w,$h);
$white = imagecolorallocate($bg,255,255,255);
imagefill($bg,0,0,$white);//Fill the background

//Get the target image information
$info =getImageInfo($src);
$width=$info[0];//Target image width
$height=$info[1];//Target image height
$img=create($ src);
if($fisrt=="wh")
{
//Constant scaling
return $src;
}
else
{
if($fisrt=="w")
{
$x=0;
$y=($h-$height)/2;//Vertically centered
}
if ($fisrt=="h")
{
$x=($w-$width)/2;//Horizontal centering
$y=0;
}
imagecopymerge( $bg,$img,$x,$y,0,0,$width,$height,100);
imagejpeg($bg,$src);
imagedestroy($bg);
imagedestroy ($img);
return $src;
}

}


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444990.htmlTechArticlephp Add watermark function and thumbnail function code for pictures/** * Add watermark for pictures* @param string $desImg target image parameter format is ./images/pic.jpg * @param string $waterImg watermark...
Related labels:
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