Home > Backend Development > PHP Tutorial > PHP gd2 uploads pictures/text watermarks/picture watermarks/proportional thumbnails/implementation code_PHP tutorial

PHP gd2 uploads pictures/text watermarks/picture watermarks/proportional thumbnails/implementation code_PHP tutorial

WBOY
Release: 2016-07-21 15:38:25
Original
892 people have browsed it

Copy code The code is as follows:

//Upload file type list
$uptypes=array(
'image/jpg',
'image/jpeg',
'image /png',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png'
);
$ max_file_size = 200000; //Upload file size limit, unit BYTE
$path_im = "prod_img/"; //Generate large image saving folder path
$path_sim = "prod_simg/"; //Thumbnail image saving file Clip path
$watermark = 1; //Whether to add watermark (1 means adding watermark, others means not adding watermark);
$watertype = 1; //Watermark type (1 means text, 2 means picture)
$waterstring = "[url=http://www.jy17.com/]http://www.jy17.com/[/url]"; //Watermark string
$waterimg = "water. png"; //Watermark image file path
$waterclearly = 100; //Watermark transparency 0-100, smaller numbers are more transparent
$imclearly = 100; //Picture clarity 0-100, the larger the number, the clearer it is , the larger the file size
$simclearly = 75; //Thumbnail resolution 0-100, the larger the number, the clearer the file size, the larger the file size
$smallmark = 1; //Whether to generate a thumbnail (1 is plus Generate, others are not);
$dst_sw = 80; //Define the thumbnail width. I use equal proportion scaling for the height, so just compare the width.
?>

Upload file:



The file types allowed to be uploaded are:

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
//Does the file exist?
{
echo "The picture does not exist!";
exit;
}
$file = $_FILES["upfile"];
if($ max_file_size < $file["size"])
//Check file size
{ $max_file_size = $max_file_size/1000;
echo "The file is too large, exceeding ".$max_file_size." KB!" ;
exit;
}
if(!in_array($file["type"],$uptypes))
//Check file type
{
echo "File type does not match !".$file["type"];
exit;
}
if(!file_exists($path_im))
//Check whether the upload directory exists, if not create
{
mkdir($path_im);
}
if(!file_exists($path_sim))
//Check whether the thumbnail directory exists, if not create it
{
mkdir($ path_sim);
}
$filename = $file["tmp_name"];
$im_size = getimagesize($filename);
$src_w = $im_size[0];
$src_h = $im_size[1];
$src_type = $im_size[2];
$pinfo = pathinfo($file["name"]);
$filetype = $pinfo['extension'];
$all_path = $path_im.time().".".$filetype; //Path + file name, currently named after upload time
if (file_exists($all_path))
{
echo "The file with the same name already exists";
exit;
}
if(!move_uploaded_file ($filename,$all_path))
{
echo "Error moving file";
exit;
}
$pinfo = pathinfo($all_path);
$fname = $pinfo[basename];
echo "has been successfully uploaded
File name: ".$all_path."
";
echo "Width: ".$src_w."px ";
echo "Length:".$src_h."px ";
echo "
Size: ".$file["size"]." bytes";
switch($src_type)//Judgement source Image file type
{
case 1://gif
$src_im = imagecreatefromgif($all_path);//Get the image from the source image file
break;
case 2://jpg
$src_im = imagecreatefromjpeg($all_path);
break;
case 3://png
$src_im = imagecreatefrompng($all_path);
break;
//case 6 :
//$src_im=imagecreatefromwbmp($all_path);
//break;
default:
die("Unsupported file type");
exit;
}
if($watermark == 1)
{
//$iinfo = getimagesize($all_path,$iinfo);
$dst_im = imagecreatetruecolor($src_w,$src_h);
//Create a true color bitmap of the same size based on the original image size
$white = imagecolorallocate($dst_im,255,255,255);//White
//Fill the new image with the background color
$black = imagecolorallocate ($dst_im,0,0,0);//Black
$red = imagecolorallocate($dst_im,255,0,0);//Red
$orange = imagecolorallocate($dst_im,255,85, 0);//Orange
imagefill($dst_im,0,0,$white);
imagecopymerge($dst_im,$src_im,0,0,0,0,$src_w,$src_h,100); //The original image is written into the new true color bitmap
//imagefilledrectangle($dst_im,1,$src_h-15,80,$src_h,$white);
switch($watertype)
{
case 1: //Watermark string
imagestring($dst_im,5,5,$src_h-20,$waterstring,$orange);//Text watermark, font No. 5, color orange, located in the background The lower left corner of the picture
break;
case 2: //Watermark image
$lim_size = getimagesize($waterimg); //Get the watermark image size, information
switch($lim_size[2]) //Determine the watermark image file type
{
case 1://gif
$src_lim = imagecreatefromgif($waterimg); //Get the watermark image
break;
case 2:// jpg
$src_lim = imagecreatefromjpeg($waterimg);
break;
case 3://png
$src_lim = imagecreatefrompng($waterimg);
break;
//case 6:
//$src_im=imagecreatefromwbmp($waterimg);
//break;
default:
die("Unsupported file type");
exit;
}
$src_lw = ($src_w-$lim_size[0])/2; //The watermark is located at the width positioning in the center of the background image
$src_lh = ($src_h-$lim_size[1])/2; / /height positioning
imagecopymerge($dst_im,$src_lim,$src_lw,$src_lh,0,0,$lim_size[0],$lim_size[1],$waterclearly);// Merge two images and set watermark transparency $waterclearly
imagedestroy($src_lim);
break;
}
switch($src_type)
{
case 1:
imagegif($dst_im,$all_path,$ imclearly);//Generate gif files, image clarity 0-100
break;
case 2:
imagejpeg($dst_im,$all_path,$imclearly);//Generate jpg files, image clarity 0-100
break;
case 3:
imagepng($dst_im,$all_path,$imclearly);//Generate png file, image definition 0-100
break;
/ /case 6:
//imagewbmp($dst_im,$all_path);
break;
}
//Release cache
imagedestroy($dst_im);
}
if($smallmark == 1)
{
$sall_path = $path_sim.time().".".$filetype;
if (file_exists($sall_path))
{
echo "The file with the same name already exists";
exit;
}
if($src_w <= $dst_sw) // Original image size<= Thumbnail size
{
$ dst_sim = imagecreatetruecolor($src_w,$src_h); //Create a new thumbnail true color bitmap
imagecopymerge($dst_sim,$src_im,0,0,0,0,$src_w,$src_h,100); // The original image is written into the new true color bitmap
}
if($src_w > $dst_sw) // Original image size > Thumbnail size
{
$dst_sh = $dst_sw/ $src_w*$src_h;
$dst_sim = imagecreatetruecolor($dst_sw,$dst_sh); //Create a new thumbnail true color bitmap (reducing the original image size proportionally)
imagecopyresampled($dst_sim,$src_im,0 ,0,0,0,$dst_sw,$dst_sh,$src_w,$src_h); //The original image is written into the new true color bitmap
}
switch($src_type)
{
case 1:
imagegif($dst_sim,$sall_path,$simclearly);//Generate gif file, image resolution 0-100
break;
case 2:
imagejpeg($dst_sim ,$sall_path,$simclearly);//Generate jpg file, image resolution 0-100
break;
case 3:
imagepng($dst_sim,$sall_path,$simclearly);//Generate png File, image resolution 0-100
break;
//case 6:
//imagewbmp($dst_sim,$sall_path);
break;
}
//release Cache
imagedestroy($dst_sim);
}
//Release cache
imagedestroy($src_im);
}
?>

php Function 2 to generate thumbnails in equal proportions
Copy code The code is as follows:

function reSizeImg($imgSrc, $resize_width, $resize_height, $isCut=false) {
//Type of image
$type = substr ( strrchr ( $imgSrc, "." ), 1 );
//Initialize image
if ($type == "jpg") {
$im = imagecreatefromjpeg ( $imgSrc );
}
if ($type == "gif") {
$im = imagecreatefromgif ( $imgSrc );
}
if ($type == "png") {
$im = imagecreatefrompng ( $imgSrc );
}
//Target image address
$full_length = strlen ( $imgSrc );
$type_length = strlen ( $type );
$name_length = $full_length - $type_length;
$ name = substr ( $imgSrc, 0, $name_length - 1 );
$dstimg = $name . "_s." . $type;
$width = imagesx ( $im );
$height = imagesy ( $im );
//Generate image
//Changed image ratio
$resize_ratio = ($resize_width) / ($resize_height);
//Actual image The ratio of
$ratio = ($width) / ($height);
if (($isCut) == 1) //Cut image
{
if ($ratio >= $ resize_ratio) //High priority
{
$newimg = imagecreatetruecolor ( $resize_width, $resize_height );
imagecopyresampled ( $newimg, $im, 0, 0, 0, 0, $resize_width, $resize_height, (($height) * $resize_ratio), $height );
ImageJpeg ( $newimg, $dstimg );
}
if ($ratio < $resize_ratio) //Width priority
{
$newimg = imagecreatetruecolor ( $resize_width, $resize_height );
imagecopyresampled ( $newimg, $im, 0, 0, 0, 0, $resize_width, $resize_height, $width, (($width) / $ resize_ratio) );
ImageJpeg ( $newimg, $dstimg );
}
} else //Do not crop the image
{
if ($ratio >= $resize_ratio) {
$newimg = imagecreatetruecolor ( $resize_width, ($resize_width) / $ratio );
imagecopyresampled ( $newimg, $im, 0, 0, 0, 0, $resize_width, ($resize_width) / $ratio, $width , $height );
ImageJpeg ( $newimg, $dstimg );
}
if ($ratio < $resize_ratio) {
$newimg = imagecreatetruecolor ( ($resize_height) * $ratio, $resize_height );
imagecopyresampled ( $newimg, $im, 0, 0, 0, 0, ($resize_height) * $ratio, $resize_height, $width, $height );
ImageJpeg ( $newimg, $ dstimg );
}
}
ImageDestroy ( $im );
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321752.htmlTechArticleCopy the code as follows: ?php //Upload file type list $uptypes=array( 'image/jpg', 'image/jpeg', 'image/png', 'image/pjpeg', 'image/gif', 'image/bmp', 'image/x-png' ); $max_fi...
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