「php」php中圖片處理和文件操作的方法小結(附代碼)

本篇文章給大家帶來的內容是關於php中圖片處理和文件操作的方法小結(附代碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

「php」php中圖片處理和文件操作的方法小結(附代碼)

第一部分:圖片處理

第一:圖片縮放

圖片等比例縮放、沒處理透明色

代碼如下:

function thumn($background, $width, $height, $newfile) {

list($s_w, $s_h)=getimagesize($background);//獲取原圖片高度、寬度

if ($width && ($s_w < $s_h)) {

$width = ($height / $s_h) * $s_w;

} else {

$height = ($width / $s_w) * $s_h;

}

$new=imagecreatetruecolor($width, $height);

$img=imagecreatefromjpeg($background);

imagecopyresampled($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h);

imagejpeg($new, $newfile);

imagedestroy($new);

imagedestroy($img);

}

thumn("images/hee.jpg", 200, 200, "./images/hee3.jpg");

第二:圖片加水印

圖片添加文字水印

function mark_text($background, $text, $x, $y){

$back=imagecreatefromjpeg($background);

$color=imagecolorallocate($back, 0, 255, 0);

imagettftext($back, 20, 0, $x, $y, $color, "simkai.ttf", $text);

imagejpeg($back, "./images/hee7.jpg");

imagedestroy($back);

}

mark_text("./images/hee.jpg", "細說PHP", 150, 250);

第二部分:可變變量

1、可變變量

2、可變函數

$a="function";

$a teststr()

{

return "adfasd";

}

$b="teststr";

echo $b();

3、可變類

$a="b";

$$a="c";

echo $b;

「php」php中圖片處理和文件操作的方法小結(附代碼)

第三部分:文件操作(PHP 操作文件)

一:readfile() 函數

實例一:

echo readfile("webdictionary.txt");

?>

二:fopen() ;打開文件

(一). fopen(1,2);

1.文件名

2.打開模式

模式 描述

r 打開文件為只讀。文件指針在文件的開頭開始。

w 打開文件為只寫。刪除文件的內容或創建一個新的文件,如果它不存在。文件指針在文件的開頭開始。

a 打開文件為只寫。文件中的現有數據會被保留。文件指針在文件結尾開始。創建新的文件,如果文件不存在。

x 創建新文件為只寫。返回 FALSE 和錯誤,如果文件已存在。

r+ 打開文件為讀/寫、文件指針在文件開頭開始。

w+ 打開文件為讀/寫。刪除文件內容或創建新文件,如果它不存在。文件指針在文件開頭開始。

a+ 打開文件為讀/寫。文件中已有的數據會被保留。文件指針在文件結尾開始。創建新文件,如果它不存在。

x+ 創建新文件為讀/寫。返回 FALSE 和錯誤,如果文件已存在。

die

exit

(二).fread()讀取文件

fread(1,2)

1.文件的指針

2.讀取文件的大小

(三). filesize() 獲取文件大小

filesize(1);

1.文件名

(四).fclose(1)關閉文件指針

fclose(1)

1.文件指針

實例二:

$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");

echo fread($myfile,filesize("webdictionary.txt"));

fclose($myfile);

?>

「php」php中圖片處理和文件操作的方法小結(附代碼)

(五) fgets(1)讀取一行數據

1.文件指針

實例三:

$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");

echo fgets($myfile);

fclose($myfile);

?>

實例四: feof(1) 檢測文件是否到了結尾

$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");

// 輸出單行直到 end-of-file

while(!feof($myfile)) {

echo fgets($myfile) . "
";

}

fclose($myfile);

?>

(六) fgetc(1)讀取一個字符

(七)fwrite()寫入文件中

實例五:

$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");

$txt = "Bill Gates\n";

fwrite($myfile, $txt);

fclose($myfile);

?>


分享到:


相關文章: