引言
第一章:PHP图片处理概述
1.1 PHP图片处理简介
1.2 PHP图片处理函数
imagecreate():创建一个空白的图像资源。imagecreatefromjpeg():从JPEG文件创建一个图像资源。imagecreatefromgif():从GIF文件创建一个图像资源。imagecreatefrompng():从PNG文件创建一个图像资源。imagecolorallocate():分配颜色。imagefill():填充颜色。imagejpeg():输出JPEG图像。imagegif():输出GIF图像。imagepng():输出PNG图像。
第二章:PHP图片路径处理入门
2.1 图片路径的基本概念
- 相对路径:相对于当前工作目录的路径。
- 绝对路径:从根目录开始的路径。
2.2 读取图片路径
$imagePath = 'path/to/image.jpg';
$imageContent = file_get_contents($imagePath);
2.3 获取图片信息
list($width, $height, $type, $attr) = getimagesize($imagePath);
第三章:PHP图片路径处理进阶
3.1 图片路径修改
$sourceImage = imagecreatefromjpeg('path/to/source.jpg');
$destinationImage = imagecreatefromjpeg('path/to/destination.jpg');
imagecopy($destinationImage, $sourceImage, 0, 0, 0, 0, $width, $height);
imagejpeg($destinationImage, 'path/to/output.jpg');
3.2 图片路径动态生成
使用uniqid()函数生成唯一的文件名。
$uniqueFileName = uniqid() . '.jpg';
3.3 图片路径存储
$imagePath = 'path/to/image.jpg';
// 存储到数据库
// $stmt = $pdo->prepare("INSERT INTO images (path) VALUES (:path)");
// $stmt->bindParam(':path', $imagePath);
// $stmt->execute();
// 存储到文件
file_put_contents('path/to/store', $imagePath);
第四章:PHP图片路径处理高效实战
4.1 图片上传
if (isset($_FILES['image'])) {
$imagePath = 'path/to/save/' . $_FILES['image']['name'];
move_uploaded_file($_FILES['image']['tmp_name'], $imagePath);
}
4.2 图片缩放
$sourceImage = imagecreatefromjpeg('path/to/source.jpg');
$width = 100;
$height = 100;
$destinationImage = imagecreatetruecolor($width, $height);
imagecopyresampled($destinationImage, $sourceImage, 0, 0, 0, 0, $width, $height, $width, $height);
imagejpeg($destinationImage, 'path/to/output.jpg');
4.3 图片裁剪
$sourceImage = imagecreatefromjpeg('path/to/source.jpg');
$width = 100;
$height = 100;
$x = 50;
$y = 50;
$destinationImage = imagecreatetruecolor($width, $height);
imagecopy($destinationImage, $sourceImage, 0, 0, $x, $y, $width, $height);
imagejpeg($destinationImage, 'path/to/output.jpg');