在 PHP 中调整图像大小

Muhammad Abubakar 2024年2月15日
  1. 在调整大小之前加载图像
  2. PHP 中的 imagecopyresized()
  3. PHP 中的 imagecopyresampled()
  4. PHP 中的 imagescale()
在 PHP 中调整图像大小

在本教程文章中,我们将讨论在 PHP 中调整图像大小。

在调整大小之前加载图像

在调整图像大小之前,我们必须首先将其作为脚本中的图像资源加载。这与使用 file_get_contents() 之类的函数来获取图像文件的内容不同。要加载文件,我们需要使用 imagecreatefromjpeg()imagecreatefrompng()imagecreatefromgif() 等函数。根据我们将调整大小的图像类型,我们将相应地使用不同的函数。

PHP 中的 getimagesize()

加载图像后,我们使用 getimagesize() 来计算输入图像的宽度、高度和类型。此函数返回一个项目列表,其中图像的宽度和高度分别存储在索引 0 和 1 处,IMAGETYPE_XXX 常量存储在索引 2 处。我们将使用返回的这个常量的值来找出什么要使用的图像类型以及要使用的功能。

<?php
function load_image($filename, $type) {
    $new = 'new34.jpeg';
    if( $type == IMAGETYPE_JPEG ) {
        $image = imagecreatefromjpeg($filename);
        echo "here is jpeg output:";
        imagejpeg($image, $new);

    }
    elseif( $type == IMAGETYPE_PNG ) {
        $image = imagecreatefrompng($filename);
        echo "here is png output:";
        imagepng($image,$new);
    }
    elseif( $type == IMAGETYPE_GIF ) {
        $image = imagecreatefromgif($filename);
        echo "here is gif output:";
        imagejpeg($image, $new);
    }
    return $new;
}

$filename = "panda.jpeg";
list($width, $height,$type) = getimagesize($filename);
echo "Width:" , $width,"\n";
echo "Height:", $height,"\n";
echo "Type:", $type, "\n";

$old_image = load_image($filename, $type);
?>

输入图像:

php 调整图像大小 - 原始图像

输出:

Width:225
Height:225
Type:2
here is jpeg output:

输出图像:

php 调整图片大小

两者是相同的图像,因为我们在本节中只加载图像并计算原始图像的大小。

PHP 中的 imagecopyresized()

imagecopyresized() 从位置 (src_x,src_y) 处的 src_image 获取宽度为 src_w 和高度为 src_h 的长方形区域,并将其放置在位置 (dst_x,dst_y) 处的目标图像的矩形区域中 .它是 PHP 中的内置函数。

<?php
// File and new size
$filename = 'panda.jpeg';
$percent = 0.5;

// Content type
header('Content-Type: image/jpeg');

// Get new sizes
list($width, $height, $type) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
$type = $list[2];
echo $type;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb,'new.jpeg');
?>

输入图像:

php 调整图像大小 - 原始图像

输出图像:

php 调整图片大小

如上所述,imagecopyresize() 函数接受十个参数,如下所列。

参数 解释
$dst_image 目标图像资源
$src_image 源图像资源
$dst_x 目标点的 x 坐标
$dst_y 目标点的 y 坐标
$src_x 源点的 x 坐标
$src_y 源点的 y 坐标
$dst_w 目的地宽度
$dst_h 目的地高度
$src_w 源宽度
$src_h 源高度

它在成功时返回布尔值 TRUE,在失败时返回 FALSE。

PHP 中的 imagecopyresampled()

imagecopyresampled() 将一幅图像的长方形部分复制到另一个不同的、无缝插值的像素值,以减少图片的尺寸,保持高度的清晰度。

它的功能类似于 imagecopyresized() 功能,除了调整图像大小外,还具有对图像进行采样的额外好处。

<?php
// The file
$filename = 'panda.jpeg';
$percent = 2;

// Content type
header('Content-Type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, 'resampled1.jpeg', 100);
?>

输入图像:

php 调整图像大小 - 原始图像

输出图像:

php 调整图片大小

函数 imagecopyresampled() 接受十个不同的参数。

参数 解释
$image_p 它确定目标图像。
$image 它确定源图像。
$x_dest 它确定目标图像的 x 坐标。
$y_dest 它确定目标图像的 y 坐标。
$x_src 它确定源图像的 x 坐标。
$y_src 它确定源图像的 y 坐标。
$wid_dest 它确定新图像的宽度。
$hei_dest 它确定新图像的高度。
$wid_src 它确定旧图像的宽度。
$wid_src 它确定旧图像的高度。
注意
我们也可以按比例进行重采样,这可以通过提供 dest_image 的宽度和高度而不是百分比来实现。

PHP 中的 imagescale()

你指定大小而不是定义最新图像的宽度或高度。如果你希望新图像大小是第一张图像的一半,请将大小设置为 0.8。这是在保留比例的同时按给定因子缩放图片的示例代码。

我们将图像的原始宽度和高度与 scale image() 函数中的指定比例相乘。

这是它的例子:

<?php
    
function load_image($filename, $type) {

        if( $type == IMAGETYPE_JPEG ) {
            $image = imagecreatefromjpeg($filename);

        }
        elseif( $type == IMAGETYPE_PNG ) {
            $image = imagecreatefrompng($filename);
           
        }
        elseif( $type == IMAGETYPE_GIF ) {
            $image = imagecreatefromgif($filename);
            
        }
        
        return $image;
    }
function scale_image($scale, $image, $width, $height) {
    $new_width = $width * $scale;
    $new_height = $height * $scale;
    return resize_image($new_width, $new_height, $image, $width, $height);
    
}
function resize_image($new_width, $new_height, $image, $width, $height) {
    $image12 = 'hello.jpeg';
    $new_imag = imagecreatetruecolor($new_width, $new_height);
    imagecopyresampled($new_imag, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    
    return imagejpeg($new_imag, $image12);
    }


$filename = "panda.jpeg";
list($width, $height, $type) = getimagesize($filename);
$old_image = load_image($filename, $type);
$image_scaled = scale_image(0.8, $old_image, $width, $height);
?>

输入图像:

php 调整图像大小 - 原始图像

输出图像:

php 调整图片大小

除此之外,PHP 有一个内置功能,允许你将图像缩放到指定的宽度和高度。

请参阅示例代码。

<?php
    
$out_image = "sca.jpeg";
$image_name ="panda.jpeg";
     
// Load image file
$image = imagecreatefromjpeg($image_name);
  
// Use imagescale() function to scale the image
$img = imagescale( $image, 500, 400 );
  

header("Content-type: image/jpeg");
imagejpeg($img,$out_image);
  
?>

输入图像:

php 调整图像大小 - 原始图像

输出_图像:

php 调整图片大小

相关文章 - PHP Image