


PHP gd2 uploads pictures/text watermarks/picture watermarks/proportional thumbnails/implementation code_PHP tutorial
//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.
?>
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
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 );
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
