您的位置 首页 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 操作文件)

一:readfile() 函数

实例一:

<?php

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.文件指针

实例二:

<?php

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

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

fclose($myfile);

?>

(五) fgets (1)读取一行数据

1.文件指针

实例三:

<?php

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

echo fgets($myfile);

fclose($myfile);

?>

实例四: feof(1) 检测文件是否到了结尾

<?php

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

// 输出单行直到 end-of-file

while(!feof($myfile)) {

echo fgets($myfile) . “<br>”;

}

fclose($myfile);

?>

(六) fgetc(1)读取一个字符

(七)fwrite()写入文件中

实例五:

<?php

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

$txt = “Bill Gates\n”;

fwrite($myfile, $txt);

fclose($myfile);

?>

文章来源:智云一二三科技

文章标题:「php」php中图片处理和文件操作的方法小结(附代码)

文章地址:https://www.zhihuclub.com/80539.shtml

关于作者: 智云科技

热门文章

网站地图