1. 准备工作

pecl install gd

安装完成后,需要确保在php.ini文件中启用了GD库:

extension=gd

2. 获取图片尺寸

function get_image_size($img_path) {
    $image_info = getimagesize($img_path);
    return [
        'width' => $image_info[0],
        'height' => $image_info[1],
        'type' => $image_info[2],
        'attr' => $image_info[3],
        'bits' => $image_info[bits],
        'channels' => $image_info[channels],
        'mime' => $image_info['mime']
    ];
}

3. 调整图片尺寸

3.1 等比缩放

function resize_image($img_path, $max_width, $max_height) {
    $image_info = get_image_size($img_path);
    $width = $image_info['width'];
    $height = $image_info['height'];

    $ratio = min($max_width / $width, $max_height / $height);
    $new_width = $width * $ratio;
    $new_height = $height * $ratio;

    $image = imagecreatetruecolor($new_width, $new_height);
    switch ($image_info['type']) {
        case IMAGETYPE_JPEG:
            $source_image = imagecreatefromjpeg($img_path);
            break;
        case IMAGETYPE_PNG:
            $source_image = imagecreatefrompng($img_path);
            break;
        case IMAGETYPE_GIF:
            $source_image = imagecreatefromgif($img_path);
            break;
        default:
            return false;
    }

    imagecopyresampled($image, $source_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    switch ($image_info['type']) {
        case IMAGETYPE_JPEG:
            imagejpeg($image, 'new_image.jpg');
            break;
        case IMAGETYPE_PNG:
            imagepng($image, 'new_image.png');
            break;
        case IMAGETYPE_GIF:
            imagegif($image, 'new_image.gif');
            break;
    }

    imagedestroy($source_image);
    imagedestroy($image);
}

3.2 固定宽度

function resize_image_by_width($img_path, $new_width) {
    $image_info = get_image_size($img_path);
    $width = $image_info['width'];
    $height = $image_info['height'];

    $ratio = $new_width / $width;
    $new_height = $height * $ratio;

    $image = imagecreatetruecolor($new_width, $new_height);
    switch ($image_info['type']) {
        case IMAGETYPE_JPEG:
            $source_image = imagecreatefromjpeg($img_path);
            break;
        case IMAGETYPE_PNG:
            $source_image = imagecreatefrompng($img_path);
            break;
        case IMAGETYPE_GIF:
            $source_image = imagecreatefromgif($img_path);
            break;
        default:
            return false;
    }

    imagecopyresampled($image, $source_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    switch ($image_info['type']) {
        case IMAGETYPE_JPEG:
            imagejpeg($image, 'new_image.jpg');
            break;
        case IMAGETYPE_PNG:
            imagepng($image, 'new_image.png');
            break;
        case IMAGETYPE_GIF:
            imagegif($image, 'new_image.gif');
            break;
    }

    imagedestroy($source_image);
    imagedestroy($image);
}

3.3 固定高度

function resize_image_by_height($img_path, $new_height) {
    $image_info = get_image_size($img_path);
    $width = $image_info['width'];
    $height = $image_info['height'];

    $ratio = $new_height / $height;
    $new_width = $width * $ratio;

    $image = imagecreatetruecolor($new_width, $new_height);
    switch ($image_info['type']) {
        case IMAGETYPE_JPEG:
            $source_image = imagecreatefromjpeg($img_path);
            break;
        case IMAGETYPE_PNG:
            $source_image = imagecreatefrompng($img_path);
            break;
        case IMAGETYPE_GIF:
            $source_image = imagecreatefromgif($img_path);
            break;
        default:
            return false;
    }

    imagecopyresampled($image, $source_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    switch ($image_info['type']) {
        case IMAGETYPE_JPEG:
            imagejpeg($image, 'new_image.jpg');
            break;
        case IMAGETYPE_PNG:
            imagepng($image, 'new_image.png');
            break;
        case IMAGETYPE_GIF:
            imagegif($image, 'new_image.gif');
            break;
    }

    imagedestroy($source_image);
    imagedestroy($image);
}

4. 总结