引言
一、PHP图片处理基础
1.1 PHP支持的图片格式
1.2 图片处理函数库
PHP提供了多个图像处理函数库,如GD库、ImageMagick等。GD库是PHP自带的图像处理库,功能强大且易于使用。
二、GD库基本操作
2.1 创建图像资源
$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);
2.2 设置画布颜色
$background_color = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $background_color);
2.3 输出图像
header('Content-Type: image/png');
imagepng($image);
2.4 释放图像资源
imagedestroy($image);
三、图片处理技巧
3.1 图片缩放
$source_image = imagecreatefromjpeg('example.jpg');
$width = 100;
$height = 100;
$target_image = imagecreatetruecolor($width, $height);
imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $width, $height, imagesx($source_image), imagesy($source_image));
imagepng($target_image);
imagedestroy($source_image);
imagedestroy($target_image);
3.2 图片裁剪
$source_image = imagecreatefromjpeg('example.jpg');
$width = 100;
$height = 100;
$x = 50;
$y = 50;
$target_image = imagecreatetruecolor($width, $height);
imagecopyresampled($target_image, $source_image, 0, 0, $x, $y, $width, $height, $width, $height);
imagepng($target_image);
imagedestroy($source_image);
imagedestroy($target_image);
3.3 图片旋转
$source_image = imagecreatefromjpeg('example.jpg');
$width = 100;
$height = 100;
$angle = 90;
$target_image = imagerotate($source_image, $angle, 0);
imagepng($target_image);
imagedestroy($source_image);
imagedestroy($target_image);
3.4 图片添加文字
$source_image = imagecreatefromjpeg('example.jpg');
$font_file = 'arial.ttf';
$font_size = 20;
$font_color = imagecolorallocate($source_image, 0, 0, 0);
$text = 'Hello, World!';
$angle = 0;
$position = imagettftext($source_image, $font_size, $angle, 10, 30, $font_color, $font_file, $text);
imagepng($source_image);
imagedestroy($source_image);
四、实例解析
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// 检查文件是否是真实的图片
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// 检查文件是否已经存在
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// 检查文件大小
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// 检查文件类型
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// 检查是否上传成功
if ($uploadOk == 1) {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
// 创建缩略图
$source_image = imagecreatefromjpeg($target_file);
$width = 100;
$height = 100;
$target_image = imagecreatetruecolor($width, $height);
imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $width, $height, imagesx($source_image), imagesy($source_image));
imagepng($target_image, $target_dir . "thumbnail_" . basename($_FILES["fileToUpload"]["name"]));
imagedestroy($source_image);
imagedestroy($target_image);
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}