本文目錄一覽:
php的介紹及Php有什麼優勢?
介紹:
PHP 獨特的語法混合了 C、Java、Perl 以及 PHP 自創新的語法。 PHP安裝它可以比 CGI或者Perl更快速的執行動態網頁。用PHP做出的動態頁面與其他的編程語言相比,PHP是將程序嵌入到HTML文檔中去執行,執行效率比完全生成HTML標記的CGI要高許多;PHP還可以執行編譯後代碼,編譯可以達到加密和優化代碼運行,使代碼運行更快。PHP具有非常強大的功能,所有的CGI的功能PHP都能實現,而且支持幾乎所有流行的數據庫以及操作系統。最重要的是PHP可以用C、C++進行程序的擴展!
優勢:
1、開放的源代碼: 所有的PHP源代碼事實上都可以得到。 2、PHP是免費的。 和其它技術相比,PHP本身免費。 3、php的快捷性 程序開發快,運行快,技術本身學習快。嵌入於HTML:因為PHP可以被嵌入於HTML語言,它相對於其他語言,編輯簡單,實用性強,更適合初學者。 4、跨平台性強: 由於PHP是運行在服務器端的腳本,可以運行在UNIX、LINUX、WINDOWS下。 5、效率高: PHP消耗相當少的系統資源。 6、圖像處理: 用PHP動態創建圖像 7、面向對象: 在php4,php5 中,面向對象方面都有了很大的改進,現在php完全可以用來開發大型商業程序。 8、專業專註: PHP支持腳本語言為主,同為類C語言。
PHP網站上傳圖片自動壓縮,怎麼編程啊,求指
這裡會使用到三個文件:
connect.php:連接數據庫
test_upload.php:執行SQL語句
upload_img.php:上傳圖片並壓縮
三個文件代碼如下:
連接數據庫:connect.php
?php
$db_host = ”;
$db_user = ”;
$db_psw = ”;
$db_name = ”;
$db_port = ”;
$sqlconn=new mysqli($db_host,$db_user,$db_psw,$db_name);
$q=”set names utf8;”;
$result=$sqlconn-query($q);
if (mysqli_connect_errno()) {
printf(“Connect failed: %s\n”, mysqli_connect_error());
exit();
}
?
當然使用一些封裝的數據庫類也是可以的。
執行SQL語句:test_upload.php
?php
require (“connect.php”);
require (“upload_img.php”);
$real_img=$uploadfile;
$small_img=$uploadfile_resize;
$insert_sql = “insert into img (real_img,small_img) values (?,?)”;
$result = $sqlconn – prepare($insert_sql);
$result – bind_param(“ss”, $real_img,$small_img);
$result – execute();
?
上傳圖片並壓縮:upload_img.php
?php
//設置文件保存目錄
$uploaddir = “upfiles/”;
//設置允許上傳文件的類型
$type=array(“jpg”,”gif”,”bmp”,”jpeg”,”png”);
//獲取文件後綴名函數
function fileext($filename)
{
return substr(strrchr($filename, ‘.’), 1);
}
//生成隨機文件名函數
function random($length)
{
$hash = ‘CR-‘;
$chars = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz’;
$max = strlen($chars) – 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i $length; $i++)
{
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
$a=strtolower(fileext($_FILES[‘filename’][‘name’]));
//判斷文件類型
if(!in_array(strtolower(fileext($_FILES[‘filename’][‘name’])),$type))
{
$text=implode(“,”,$type);
$ret_code=3;//文件類型錯誤
$page_result=$text;
$retArray = array(‘ret_code’ = $ret_code,’page_result’=$page_result);
$retJson = json_encode($retArray);
echo $retJson;
return;
}
//生成目標文件的文件名
else
{
$filename=explode(“.”,$_FILES[‘filename’][‘name’]);
do
{
$filename[0]=random(10); //設置隨機數長度
$name=implode(“.”,$filename);
//$name1=$name.”.Mcncc”;
$uploadfile=$uploaddir.$name;
}
while(file_exists($uploadfile));
if (move_uploaded_file($_FILES[‘filename’][‘tmp_name’],$uploadfile))
{
if(is_uploaded_file($_FILES[‘filename’][‘tmp_name’]))
{
$ret_code=1;//上傳失敗
}
else
{//上傳成功
$ret_code=0;
}
}
$retArray = array(‘ret_code’ = $ret_code);
$retJson = json_encode($retArray);
echo $retJson;
}
//壓縮圖片
$uploaddir_resize=”upfiles_resize/”;
$uploadfile_resize=$uploaddir_resize.$name;
//$pic_width_max=120;
//$pic_height_max=90;
//以上與下面段注釋可以聯合使用,可以使圖片根據計算出來的比例壓縮
$file_type=$_FILES[“filename”][‘type’];
function ResizeImage($uploadfile,$maxwidth,$maxheight,$name)
{
//取得當前圖片大小
$width = imagesx($uploadfile);
$height = imagesy($uploadfile);
$i=0.5;
//生成縮略圖的大小
if(($width $maxwidth) || ($height $maxheight))
{
/*
$widthratio = $maxwidth/$width;
$heightratio = $maxheight/$height;
if($widthratio $heightratio)
{
$ratio = $widthratio;
}
else
{
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
*/
$newwidth = $width * $i;
$newheight = $height * $i;
if(function_exists(“imagecopyresampled”))
{
$uploaddir_resize = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
else
{
$uploaddir_resize = imagecreate($newwidth, $newheight);
imagecopyresized($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($uploaddir_resize,$name);
ImageDestroy ($uploaddir_resize);
}
else
{
ImageJpeg ($uploadfile,$name);
}
}
if($_FILES[“filename”][‘size’])
{
if($file_type == “image/pjpeg”||$file_type == “image/jpg”|$file_type == “image/jpeg”)
{
//$im = imagecreatefromjpeg($_FILES[$upload_input_name][‘tmp_name’]);
$im = imagecreatefromjpeg($uploadfile);
}
elseif($file_type == “image/x-png”)
{
//$im = imagecreatefrompng($_FILES[$upload_input_name][‘tmp_name’]);
$im = imagecreatefromjpeg($uploadfile);
}
elseif($file_type == “image/gif”)
{
//$im = imagecreatefromgif($_FILES[$upload_input_name][‘tmp_name’]);
$im = imagecreatefromjpeg($uploadfile);
}
else//默認jpg
{
$im = imagecreatefromjpeg($uploadfile);
}
if($im)
{
ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize);
ImageDestroy ($im);
}
}
?
請按照現實情況更改connect.php,test_upload.php中對應的信息。
望採納,謝謝。
為什麼有那麼多人選擇PHP編程語言
開放源代碼
所有的PHP源代碼事實上都可以得到。
免費性
和其它技術相比,PHP本身免費且是開源代碼。
快捷性
程序開發快,運行快,技術本身學習快。嵌入於HTML:因為PHP可以被嵌入於HTML語言,它相對於其他語言。編輯簡單,實用性強,更適合初學者。
跨平台性強
由於PHP是運行在服務器端的腳本,可以運行在UNIX、LINUX、WINDOWS、Mac OS、Android等平台
效率高
PHP消耗相當少的系統資源。
圖像處理
用PHP動態創建圖像,PHP圖像處理默認使用GD2。且也可以配置為使用image magick進行圖像處理。
面向對象
在php4,php5 中,面向對象方面都有了很大的改進,php完全可以用來開發大型商業程序。
專業專註
PHP支持腳本語言為主,同為類C語言。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/238228.html