而不是一個樣本,我拿了10個樣本,並平均顏色。你可以嘗試更多的樣品。
<?php
$im = imagecreatefrompng('images/green.png');
$width = imagesx($im);
$height = imagesy($im);
$hpart = round($width/5); // divide image in five columns
$vpart = round($height/2); // divide image in two rows
$r = 0;
$g = 0;
$b = 0;
$rarr = array();
$garr = array();
$barr = array();
for($i=0; $i<5; $i++) {
$sampleh = round($hpart/2) + ($hpart*$i);
$samplev = round($vpart/2);
$rgb = imagecolorat($im, $sampleh,$samplev);
$r += ($rgb >> 16) & 0xFF;
$g += ($rgb >> 8) & 0xFF;
$b += $rgb & 0xFF;
$rarr[] = ($rgb >> 16) & 0xFF;
$garr[] = ($rgb >> 8) & 0xFF;
$barr[] = $rgb & 0xFF;
}
for($i=0; $i<5; $i++) {
$sampleh = round($hpart/2) + ($hpart*$i);
$samplev = round($vpart/2) + $vpart;
$rgb = imagecolorat($im, $sampleh,$samplev);
$r += ($rgb >> 16) & 0xFF;
$g += ($rgb >> 8) & 0xFF;
$b += $rgb & 0xFF;
$rarr[] = ($rgb >> 16) & 0xFF;
$garr[] = ($rgb >> 8) & 0xFF;
$barr[] = $rgb & 0xFF;
}
$r = round($r/10);
$g = round($g/10);
$b = round($b/10);
// echo "$r , $g, $b <br/>\n";
// $rgb = imagecolorat($im, 4,4);
// $r = ($rgb >> 16) & 0xFF;
// $g = ($rgb >> 8) & 0xFF;
// $b = $rgb & 0xFF;
// echo "$r , $g, $b <br/>\n";
echo "<div> <img src='images/green.png' border=1 /></div><br/>\n";
echo '<div style="font-size:60px;color:rgb(' .$r .','.$g.','.$b.')">Text in color of the Image</div>';
$text = "Text in color of the Image";
$coloredText = "";
$j = 0;
for($i=0; $i<strlen($text); $i++) {
$coloredText .= '<span style="color:rgb(' .$rarr[$j] .','.$garr[$j].','.$barr[$j].')">'.$text[$i].'</span>';
$j++;
if($j === 10) {
$j=0;
}
}
echo "<div style='font-size:60px;'>$coloredText </div>\n";
?>
你想在紅色圖像上寫紅色?這將如何有用? –