迅睿開源框架是一款PHP8高性能·簡單易用的PHP開源開發(fā)框架, 基于MIT開源許可協(xié)議發(fā)布,不限制商業(yè)使用,以多端互聯(lián)為設(shè)計理念, 支持的微信公眾號、小程序、APP客戶端、移動端網(wǎng)站、PC網(wǎng)站等多終端式管理系統(tǒng)。
業(yè)務(wù)經(jīng)理
微信掃描以上二維碼
028-61286886
技術(shù)咨詢
圖片專用字段,如果判斷上傳的圖片必須為白底圖,如何實現(xiàn)?
開源是一種精神,但不是義務(wù),幫忙是情分,不幫也不要抱怨,建議大家多研究代碼、多閱讀代碼、多翻閱社區(qū)歷史問題!
<?php
function isWhiteBackground($imageFile) {
$image = imagecreatefromjpeg($imageFile);
$width = imagesx($image);
$height = imagesy($image);
$whitePixelCount = 0;
// 遍歷圖片中的每個像素
for ($y = 0; $y < $height; $y++) {
for ($x = 0; $x < $width; $x++) {
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
// 判斷當(dāng)前像素是否為白色
if ($r >= 250 && $g >= 250 && $b >= 250) {
$whitePixelCount++;
}
// 計算白色像素的比例
$whiteRatio = $whitePixelCount / ($width * $height);
// 如果白色像素占比過高,則認(rèn)為是白底
return $whiteRatio > 0.95;
// 使用函數(shù)
$imageFile = 'path/to/your/image.jpg';
if (isWhiteBackground($imageFile)) {
echo "圖片為白底";
} else {
echo "圖片不是白底";
?>
這個函數(shù)可以實現(xiàn),但怎么加入到字段判斷里面呢?
回復(fù)@天天向上 大概的代碼怎么寫?
開源是一種精神,但不是義務(wù),幫忙是情分,不幫也不要抱怨,建議大家多研究代碼、多閱讀代碼、多翻閱社區(qū)歷史問題!
<?php
function isWhiteBackground($imageFile) {
$image = imagecreatefromjpeg($imageFile);
$width = imagesx($image);
$height = imagesy($image);
$whitePixelCount = 0;
// 遍歷圖片中的每個像素
for ($y = 0; $y < $height; $y++) {
for ($x = 0; $x < $width; $x++) {
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
// 判斷當(dāng)前像素是否為白色
if ($r >= 250 && $g >= 250 && $b >= 250) {
$whitePixelCount++;
}
}
}
// 計算白色像素的比例
$whiteRatio = $whitePixelCount / ($width * $height);
// 如果白色像素占比過高,則認(rèn)為是白底
return $whiteRatio > 0.95;
}
// 使用函數(shù)
$imageFile = 'path/to/your/image.jpg';
if (isWhiteBackground($imageFile)) {
echo "圖片為白底";
} else {
echo "圖片不是白底";
}
?>
這個函數(shù)可以實現(xiàn),但怎么加入到字段判斷里面呢?
回復(fù)@天天向上 大概的代碼怎么寫?