一个简单的使用php上传图片文件生然后再生成一张等比例的缩略图效果,有需要学习的朋友可参考参考。
代码如下 | 复制代码 |
function _UPLOADPIC($upfile, $maxsize, $updir, $newname = 'date') { if ($newname == 'date') $newname = date ( "Ymdhis" ); //使用日期做文件名 $name = $upfile ["name"]; $type = $upfile ["type"]; $size = $upfile ["size"]; $tmp_name = $upfile ["tmp_name"]; switch ($type) { case 'image/pjpeg' : case 'image/jpeg' : $extend = ".jpg"; break; case 'image/gif' : $extend = ".gif"; break; case 'image/png' : $extend = ".png"; break; } if (emptyempty ( $extend )) { echo ( "警告!只能上传图片类型:GIF JPG PNG" ); exit (); } if ($size > $maxsize) { $maxpr = $maxsize / 1000; echo ( "警告!上传图片大小不能超过" . $maxpr . "K!" ); exit (); } if (move_uploaded_file ( $tmp_name, $updir . $newname . $extend )) { return $updir . $newname . $extend; } }
function show_pic_scal($width, $height, $picpath) { $imginfo = GetImageSize ( $picpath ); $imgw = $imginfo [0]; $imgh = $imginfo [1];
$ra = number_format ( ($imgw / $imgh), 1 ); //宽高比 $ra2 = number_format ( ($imgh / $imgw), 1 ); //高宽比
if ($imgw > $width or $imgh > $height) { if ($imgw > $imgh) { $newWidth = $width; $newHeight = round ( $newWidth / $ra );
} elseif ($imgw $newHeight = $height; $newWidth = round ( $newHeight / $ra2 ); } else { $newWidth = $width; $newHeight = round ( $newWidth / $ra ); } } else { $newHeight = $imgh; $newWidth = $imgw; } $newsize [0] = $newWidth; $newsize [1] = $newHeight;
return $newsize; }
function getImageInfo($src) { return getimagesize($src); } /** * 创建图片,返回资源类型 * @param string $src 图片路径 * @return resource $im 返回资源类型 * **/ 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; } /** * 缩略图主函数 * @param string $src 图片路径 * @param int $w 缩略图宽度 * @param int $h 缩略图高度 * @return mixed 返回缩略图路径 * **/
function resize($src,$w,$h) { $temp=pathinfo($src); $name=$temp["basename"];//文件名 $dir=$temp["dirname"];//文件所在的文件夹 $extension=$temp["extension"];//文件扩展名 $savepath="{$dir}/{$name}";//缩略图保存路径,新的文件名为*.thumb.jpg
//获取图片的基本信息 $info=getImageInfo($src); $width=$info[0];//获取图片宽度 $height=$info[1];//获取图片高度 $per1=round($width/$height,2);//计算原图长宽比 $per2=round($w/$h,2);//计算缩略图长宽比
//计算缩放比例 if($per1>$per2||$per1==$per2) { //原图长宽比大于或者等于缩略图长宽比,则按照宽度优先 $per=$w/$width; } if($per1 { //原图长宽比小于缩略图长宽比,则按照高度优先 $per=$h/$height; } $temp_w=intval($width*$per);//计算原图缩放后的宽度 $temp_h=intval($height*$per);//计算原图缩放后的高度 $temp_img=imagecreatetruecolor($temp_w,$temp_h);//创建画布 $im=create($src); imagecopyresampled($temp_img,$im,0,0,0,0,$temp_w,$temp_h,$width,$height); if($per1>$per2) { imagejpeg($temp_img,$savepath, 100); imagedestroy($im); return addBg($savepath,$w,$h,"w"); //宽度优先,在缩放之后高度不足的情况下补上背景 } if($per1==$per2) { imagejpeg($temp_img,$savepath, 100); imagedestroy($im); return $savepath; //等比缩放 } if($per1 { imagejpeg($temp_img,$savepath, 100); imagedestroy($im); return addBg($savepath,$w,$h,"h"); //高度优先,在缩放之后宽度不足的情况下补上背景 } } /** * 添加背景 * @param string $src 图片路径 * @param int $w 背景图像宽度 * @param int $h 背景图像高度 * @param String $first 决定图像最终位置的,w 宽度优先 h 高度优先 wh:等比 * @return 返回加上背景的图片 * **/ function addBg($src,$w,$h,$fisrt="w") { $bg=imagecreatetruecolor($w,$h); $white = imagecolorallocate($bg,255,255,255); imagefill($bg,0,0,$white);//填充背景
//获取目标图片信息 $info=getImageInfo($src); $width=$info[0];//目标图片宽度 $height=$info[1];//目标图片高度 $img=create($src); if($fisrt=="wh") { //等比缩放 return $src; } else { if($fisrt=="w") { $x=0; $y=($h-$height)/2;//垂直居中 } if($fisrt=="h") { $x=($w-$width)/2;//水平居中 $y=0; } imagecopymerge($bg,$img,$x,$y,0,0,$width,$height,100); imagejpeg($bg,$src,100); imagedestroy($bg); imagedestroy($img); return $src; } 使用方法: $filename=(_UPLOADPIC($_FILES["upload"],$maxsize,$updir,$newname='date')); |

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版