引言

一、图片上传

1.1 创建表单

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="image" />
    <input type="submit" value="上传" />
</form>

1.2 PHP处理上传

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["image"]["name"]);
    $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

    // 检查文件是否是图片
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["image"]["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["image"]["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 == 0) {
        echo "Sorry, your file was not uploaded.";
    // 如果一切顺利,尝试上传文件
    } else {
        if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
            echo "The file ". htmlspecialchars( basename( $_FILES["image"]["name"])). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
}
?>

二、图片缩放

2.1 使用GD库

<?php
$image = imagecreatefromjpeg("uploads/image.jpg");
$width = 100;
$height = 100;
$thumb = imagecreatetruecolor($width, $height);
imagecopyresized($thumb, $image, 0, 0, 0, 0, $width, $height, imagesx($image), imagesy($image));
imagejpeg($thumb, "uploads/thumbnail.jpg");
imagedestroy($image);
imagedestroy($thumb);
?>

2.2 使用ImageMagick

如果你的服务器支持ImageMagick,可以使用它提供的PHP扩展进行图像处理。

<?php
$im = new Imagick("uploads/image.jpg");
$im->resizeimage(100, 100, Imagick::FILTER_LANCZOS, 1);
$im->writeImage("uploads/thumbnail.jpg");
$im->clear();
$im->destroy();
?>

三、图片裁剪

3.1 使用GD库

<?php
$image = imagecreatefromjpeg("uploads/image.jpg");
$width = 100;
$height = 100;
$x = 50;
$y = 50;
$crop = imagecrop($image, ['x' => $x, 'y' => $y, 'width' => $width, 'height' => $height]);
imagejpeg($crop, "uploads/cropped.jpg");
imagedestroy($image);
imagedestroy($crop);
?>

3.2 使用ImageMagick

<?php
$im = new Imagick("uploads/image.jpg");
$im->cropimage(50, 50, 100, 100);
$im->writeImage("uploads/cropped.jpg");
$im->clear();
$im->destroy();
?>

四、图片格式转换

4.1 使用GD库

<?php
$image = imagecreatefromjpeg("uploads/image.jpg");
imagepng($image, "uploads/image.png");
imagedestroy($image);
?>

4.2 使用ImageMagick

<?php
$im = new Imagick("uploads/image.jpg");
$im->setImageFormat("png");
$im->writeImage("uploads/image.png");
$im->clear();
$im->destroy();
?>

五、总结