PHP gd2 upload image and text watermark and image watermark and scale thumbnail and implementation code

  • 2020-03-31 20:46:15
  • OfStack


<?php 
//Upload a list of file types
$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, BYTE
$path_im = "prod_img/"; //Generate a large image to save the folder path
$path_sim = "prod_simg/"; //Thumbnail save folder path
$watermark = 1; //Whether to add watermark (1 for watermark, other for no watermark);
$watertype = 1; //Watermark type (1 for text,2 for 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, digital small transparent high
$imclearly = 100; //Picture clarity 0-100, the larger the number, the clearer, the larger the file size
$simclearly = 75; //Thumbnail clarity 0-100, the larger the number, the clearer the file size
$smallmark = 1; //Whether to generate thumbnails (1 is plus generation, other is not);
$dst_sw = 80; //I'm going to define the width and height of the thumbnail and I'm going to scale it equally, so I'm just going to compare the width
?> 
<form enctype="multipart/form-data" method="post" name="upform"> 
 Upload a file : 
<input name="upfile" type="file"> 
<input type="submit" value=" upload "><br> 
 The type of file allowed to be uploaded is :<?=implode(',',$uptypes)?> 
</form> 
<?php 
if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
if (!is_uploaded_file($_FILES["upfile"][tmp_name])) 
//Is there a file
{ 
echo " The picture doesn't 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 to exceed  ".$max_file_size." KB!"; 
exit; 
} 
if(!in_array($file["type"],$uptypes)) 
//Check file types
{ 
echo " File type mismatch !".$file["type"]; 
exit; 
} 
if(!file_exists($path_im)) 
//Check if the upload directory exists, it does not exist
{ 
mkdir($path_im); 
} 
if(!file_exists($path_sim)) 
//Check that the thumbnail directory exists and is not created
{ 
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 + filename, currently named above pass 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 "<font color=red> Uploaded successfully </font><br> The file name : <font color=blue>".$all_path."</font><br>"; 
echo " Width: ".$src_w."px "; 
echo " Length: ".$src_h."px "; 
echo "<br>  Size: ".$file["size"]." bytes"; 
switch($src_type)//Determines the source image file type
{ 
case 1://gif 
$src_im = imagecreatefromgif($all_path);//Gets 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 types "); 
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 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: // add Watermark string
imagestring($dst_im,5,5,$src_h-20,$waterstring,$orange);//Text watermark, font 5 point color orange, located in the lower left corner of the background image
break; 
case 2: //Watermarked picture
$lim_size = getimagesize($waterimg); //Get watermark image size, information
switch($lim_size[2]) //Determine the watermark image file type
{ 
case 1://gif 
$src_lim = imagecreatefromgif($waterimg); //Obtain 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 types "); 
exit; 
} 
$src_lw = ($src_w-$lim_size[0])/2; //The watermark is located at the width in the middle 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 opacity $waterclearly
imagedestroy($src_lim); 
break; 
} 
switch($src_type) 
{ 
case 1: 
imagegif($dst_im,$all_path,$imclearly);//Generate GIF files, picture clarity 0-100
break; 
case 2: 
imagejpeg($dst_im,$all_path,$imclearly);//Generate JPG file, picture clarity 0-100
break; 
case 3: 
imagepng($dst_im,$all_path,$imclearly);//Generate PNG file, picture clarity 0-100
break; 
//case 6: 
//imagewbmp($dst_im,$all_path); 
break; 
} 
//Release the 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 drawing size <= thumbnail size
{ 
$dst_sim = imagecreatetruecolor($src_w,$src_h); //New true color thumbnail 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 drawing size> Thumbnail size
{ 
$dst_sh = $dst_sw/$src_w*$src_h; 
$dst_sim = imagecreatetruecolor($dst_sw,$dst_sh); //New thumbnail true color bitmap (scale down the size of the original)
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 files, picture clarity 0-100
break; 
case 2: 
imagejpeg($dst_sim,$sall_path,$simclearly);//Generate JPG file, picture clarity 0-100
break; 
case 3: 
imagepng($dst_sim,$sall_path,$simclearly);//Generate PNG file, picture clarity 0-100
break; 
//case 6: 
//imagewbmp($dst_sim,$sall_path); 
break; 
} 
//Release the cache
imagedestroy($dst_sim); 
} 
//Release the cache
imagedestroy($src_im); 
} 
?>

PHP equal proportion generates thumbnail function 2
 
function reSizeImg($imgSrc, $resize_width, $resize_height, $isCut=false) { 
//Type of picture
$type = substr ( strrchr ( $imgSrc, "." ), 1 ); 
//Initialization 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 ); 
//To generate the image
//The scale of the changed image
$resize_ratio = ($resize_width) / ($resize_height); 
//The scale of the actual picture
$ratio = ($width) / ($height); 
if (($isCut) == 1) //The cutting figure
{ 
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) //The width of the 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 // Don't The cutting figure
{ 
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 ); 
} 

Related articles: