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


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

语法

bool imageistruecolor(resource $image)

参数

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

返回值

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

示例 1

<?php
   // Create an image instance with a true-color image using
   //imagecreatefrompng() function.
   $img = imagecreatefrompng('C:\xampp\htdocs\Images\img44.png');

   // Checked if the image is true-color
   $istruecolor = imageistruecolor($img);

   // Show the output image to the browser
   if($istruecolor) {
      echo "The given input image is true-color";
   }
?>

输出

// 输入 RGB 图像

// 结果输出

The given input image is true-color.

示例 2

<?php
   // Create an image instance with a true-color image using
   //imagecreatefrompng() function.
   $img = imagecreatefrompng('C:\xampp\htdocs\Gray.png');
   
   // Checked if the image is true-color
   $istruecolor = imageistruecolor($img);
 
    // Show the output image to the browser
   if($istruecolor) {
      echo "The given input image is not a true-color";
   }
?>

输出

// 输入灰度图像。

// 输出

The given input image is not a true-color image.

更新于: 2021年8月9日

169 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.