如何使用 PHP 中的 imageistruecolor() 函数确保图像为真彩色图像?

phpserver side programmingprogramming

imageistruecolor() 是 PHP 中的内置函数,用于检查给定图像是否为真彩色图像。在真彩色图像中,每个像素由 RGB(红色、绿色和蓝色)颜色值指定。

语法

bool imageistruecolor(resource $image)

参数

imageistruecolor() 接受单个参数 $image。它保存图像。

返回值

imageistruecolor() 如果给定图像是真彩色,则返回 True,否则,如果图像不是真彩色,则返回 False。

示例 1

<?php
   // 使用 imagecreatefrompng() 函数创建具有真彩色图像的图像实例。
   $img = imagecreatefrompng('C:\xampp\htdocs\Images\img44.png');

   // 检查图像是否为真彩色
   $istruecolor = imageistruecolor($img);

   // 将输出图像显示到浏览器
   if($istruecolor) {
      echo "给定的输入图像是真彩色";
   }
?>

输出

// 输入 RGB 图像

// 结果输出

给定的输入图像是真彩色的。

示例 2

<?php
   // 使用
   //imagecreatefrompng() 函数创建一个具有真彩色图像的图像实例。
   $img = imagecreatefrompng('C:\xampp\htdocs\Gray.png');
   
   // 检查图像是否为真彩色
   $istruecolor = imageistruecolor($img);
 
    // 将输出图像显示给浏览器
   if($istruecolor) {
      echo "给定的输入图像不是真彩色";
   }
?>

输出

// 输入灰度图像。

// 输出

给定的输入图像不是真彩色图像。

相关文章