


Php upload, generate thumbnails, generate text watermarks and image watermarks_PHP tutorial
/*
'-##########################################-|
'| 程序设计:(绿竹居) |
'| MSN:[email]lzj_zhangjun@hotmail.com[/email] |
'| Email:cszjun@gmail.com |
'| 结合自己以前做的上传和生成缩略和改编一个
'| 老前辈的生成图片水印做成这个 |
'| 上传+生成缩略图+生成文字水印和图片水印 |
'| 可以直接引用 |
'-##########################################-|
*/
//审明图片文件夹
$bigfolder="img";
$smallfolder="smallimg";
//审明文件夹名称 以年月日来建议文件夹
$fdate=date("Ymd");
//echo $fdate;
//审明文件名称。以年月日时分秒命名
$fname=date("YmdHis");
$bigname="0724e_com_".$fname;
$smallname="0724e_com_".$fname."_s";
//echo $fname."
".$bigname."
".$smallname;
//确定大小文件夹的名称和路经
$bigaddrname=$bigfolder."/".$fdate."/".$bigname;
$smalladdrname=$smallfolder."/".$fdate."/".$smallname;
//审明小图片的高度和宽度
$RESIZEWIDTH=180;
$RESIZEHEIGHT=150;
//审明水印的文字或图片的地址及字体的地址
$imgaddr="img.jpg"; //图片水印
$fontname="www.0724e.com"; //文字水印
//充许上传的文件扩展名
$exit_name=array(".jpg",".gif",".png");
if (isset($_POST['Submit'])){
//上传部分----------------------------
//定议上传名称和上传错误
$upfile=$_FILES['image']['name'];
$uperror=$_FILES['image']['error'];
//最简表单验证
switch ($uperror) {
case 1:
die("
break;
case 2:
die("
break;
case 3:
die("
break;
case 4:
die("
break;
}
//检测扩展是否是充许上传的文件类型
//取得文件扩展名
$exname=strrchr($upfile,".");
//判断取得扩展名是否在要求的扩展名内
if(!in_array($exname,$exit_name))
die("
//检测存放图片的目录和子目录是否存在,如果不存在则建目录和子目录,并给目录最大权限777 对LINUX或unix对WINDOWS没必要
//大图
if (!file_exists($bigfolder)){
mkdir($bigfolder,0777);
mkdir($bigfolder."/".$fdate,0777);
}else{
if (!file_exists($bigfolder."/".$fdate)){
mkdir($bigfolder."/".$fdate,0777);
}
}
//缩略图
if (!file_exists($smallfolder)){
mkdir($smallfolder,0777);
mkdir($smallfolder."/".$fdate,0777);
}else{
if (!file_exists($smallfolder."/".$fdate)){
mkdir($smallfolder."/".$fdate,0777);
}
}
//创建目录结束
//得到临时上传的文件
$upfiletmp=$_FILES['image']['tmp_name'];
//判断临时文件是否存在
if ($uperror==6)
die("
//Watermark part============================================
//Start of watermark function============
/**$groundImage Background image, that is, the image that needs to be watermarked, currently only supports GIF, JPG, and PNG formats;
* $waterPos Watermark position, there are 10 states, 0 is a random position;
* 1 means the top is on the left, 2 means the top is in the center, 3 means the top is on the right;
* 4 means the middle is on the left, 5 means the middle is on the middle, 6 means the middle is on the right;
* 7 means the bottom is on the left, 8 means the bottom is in the middle, 9 means the bottom is on the right;
* $waterImage Image watermark, that is, the image used as a watermark, currently only supports GIF, JPG, and PNG formats;
* $waterText Text watermark, that is, text is used as a watermark. It supports ASCII code and does not support Chinese;
* $textFont Text size, value is 1, 2, 3, 4 or 5, default is 5;
* $textColor Text color, the value is a hexadecimal color value, the default is #FF0000 (red);
**/
function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="", $textFont=5,$textColor="#FF0000")
{
$isWaterImage = FALSE;
$formatMsg = "This file format is not supported yet. Please use image processing software to convert the image to GIF, JPG, or PNG format.";
//Read watermark file
If(!empty($waterImage) && file_exists($waterImage)) {
$isWaterImage = TRUE;
$water_info = getimagesize($waterImage);
$water_w only
$water_h
switch($water_info[2]) { //Get the format of the watermark image
case 2:$water_im = imagecreatefromjpeg($waterImage);break;
case 3:$water_im = imagecreatefrompng($waterImage);break;
default:die($formatMsg);
}
}
//Read background image
$ground_info = getimagesize($groundImage);
$ground_w only
$ GROUND_H = $ GROUND_INFO [1]; // Give the background picture high
switch($ground_info[2]) { //Get the format of the background image
case 1:$ground_im = imagecreatefromgif($groundImage);break;
case 3:$ground_im = imagecreatefrompng($groundImage);break;
default:die($formatMsg);
}
} else {
die("The picture that needs to be watermarked does not exist!");
}
//Watermark position
If($isWaterImage) { //Image watermark
$w = $water_w;
$h = $water_h;
$label = "Picture";
} else { //Text watermark
$temp = imagettfbbox(ceil($textFont*2.5),0,"./cour.ttf",$waterText);//Get the range of text using TrueType font
$w = $temp[2] - $temp[6];
$h = $temp[3] - $temp[7];
unset($temp);
$label = "Text area";
}
If( ($ground_w
echo "The length or width of the image that needs to be watermarked is smaller than the watermark ".$label.", so the watermark cannot be generated!";
return;
}
switch($waterPos) {
case 0://random
$posX = rand(0,($ground_w - $w));
$posY = rand(0,($ground_h - $h));
break;
case 1://1 means the top is on the left
$posX = 0;
$posY = 0;
break;
case 2://2 is top-centered
$posX = ($ground_w - $w) / 2;
$posY = 0;
break;
case 3://3 is the top right
$posX = $ground_w - $w;
$posY = 0;
break;
case 4://4 is the center on the left
$posX = 0;
$posY = ($ground_h - $h) / 2;
break;
case 5://5 is centered in the middle
$posX = ($ground_w - $w) / 2;
$posY = ($ground_h - $h) / 2;
break;
case 6://6 is the center on the right
$posX = $ground_w - $w;
$posY = ($ground_h - $h) / 2;
break;
case 7://7 means the bottom is on the left
$posX = 0;
$posY = $ground_h - $h;
break;
case 8://8 is the bottom center
$posX = ($ground_w - $w) / 2;
$posY = $ground_h - $h;
break;
case 9://9 means the bottom is on the right
$posX = $ground_w - $w;
$posY = $ground_h - $h;
break;
default://random
$posX = rand(0,($ground_w - $w));
$posY = rand(0,($ground_h - $h));
break;
}
//设定图像的混色模式
imagealphablending($ground_im, true);
if($isWaterImage) { //图片水印
imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件
} else {//文字水印
if( !empty($textColor) && (strlen($textColor)==7) ) {
$R = hexdec(substr($textColor,1,2));
$G = hexdec(substr($textColor,3,2));
$B = hexdec(substr($textColor,5));
} else {
die("水印文字颜色格式不正确!");
}
imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
}
//生成水印后的图片
@unlink($groundImage);
switch($ground_info[2]) {//取得背景图片的格式
case 1:imagegif($ground_im,$groundImage);break;
case 2:imagejpeg($ground_im,$groundImage);break;
case 3:imagepng($ground_im,$groundImage);break;
default:die($errorMsg);
}
//释放内存
if(isset($water_info)) unset($water_info);
if(isset($water_im)) imagedestroy($water_im);
unset($ground_info);
imagedestroy($ground_im);
}
//水印函数完
//存在则移动完在上传
$goodupfile=@move_uploaded_file($upfiletmp,$bigaddrname.$exname);
if (!$goodupfile){
die("
}else{
//Text watermark
ImageWaterMark($bigaddrname.$exname,5,"",$fontname,5,"#FF0000");
//Picture watermark
//imageWaterMark($bigaddrname.$exname,5,$imgaddr);
//End watermark part====================================
//缩略图部分------------------------------------------------------------
//判断缩略图大小函数-----
function ResizeImage($im,$maxwidth,$maxheight,$name){
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$smalladdrname.$name.".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$smalladdrname.$name.".jpg");
}
}
//生成部分
if($_FILES['image']['size']){
if($_FILES['image']['type'] == "image/pjpeg"){
$im = imagecreatefromjpeg($bigaddrname.$exname);
}elseif($_FILES['image']['type'] == "image/x-png"){
$im = imagecreatefrompng($bigaddrname.$exname);
}elseif($_FILES['image']['type'] == "image/gif"){
$im = imagecreatefromgif($bigaddrname.$exname);
}
if($im){
if(file_exists($smalladdrname.".jpg")){
unlink($smalladdrname.".jpg");
}
ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$smalladdrname);
ImageDestroy ($im);
}
}
echo "
}
//End of thumbnail------------------------------------------------ --------
}
?>

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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