我製作的這個函式會比較兩個 ($start-$finish) 圖片,並在「finish」圖片中更改顏色不同的像素
然後在一個圖片中將它們並排顯示
另一個功能是「display」,它會回顯文字
「50% 開啟
50% 關閉」-% 計數 (如果數字小於 1,則會進入一個十進位計數)
或輸入「2」會是
「10023 開啟
3000 關閉」-像素計數
最後一個功能是「color」
您可以在陣列中定義它
$color = array("r" => "244","g" => "122","b" => "100");
為了結束說明,我將顯示我的函式的這個「地圖」
compare($start, $finish[, $color[, $display[, $type]]])
image-url($start) - 基礎圖片 URL
image-url($finish) - 比較圖片 URL
array($color) - 具有鍵 "r"、"g"、"b" 的陣列,r 為紅色 0-255,g 為綠色 0-255,b 為藍色 0-255
bool($display) - 1 或 TRUE 會傳回比較中的文字統計資料
int($type) - 1 或 0 | 1 為 % 結果 | 0 為像素結果
<?
function compare($start, $finish, $color, $display, $type){
$im = ImageCreateFrompng($start);
$im2 = ImageCreateFrompng($finish);
$img['x'] = imagesx($im);
$img['y'] = imagesy($im);
$img2['x'] = imagesx($im2);
$img2['y'] = imagesy($im2);
if(($img['x'] == $img2['x']) && ($img['y'] == $img2['y'])){
//取得並設定圖片高度和寬度
$i = array("width" => $img['x']*2, "height" => $img['y']);
$im3 = imagecreatetruecolor($i['width'], $i['height']);
if($color){
$color = imagecolorallocate($im3, $color['r'], $color['g'], $color['b']);
}else{
$color = imagecolorallocate($im3, 255, 255, 255);
}
for($y = $img['y'];$y > 0; $y--){
for($x = $img['x'];$x > 0; $x--){
if(ImageColorAt($im, $x, $y) == ImageColorAt($im2, $x, $y)){
$on = $on+1;
$rgb = ImageColorAt($im, $x, $y);
Imagesetpixel($im3, $img['x']+$x, $y, $rgb);
}else{
$off = $off+1;
imagesetpixel($im3, $img['x']+$x, $y , $color);
}
}
}
if($display == true){
if(($type == "1") || (!$type)){
$off2 = (round(($off / $on)*10));
if(($off2 == 0) && ($off > 0)){
$off2 = round(($off / $on)*10)*10;
}
$on2 = (100-$off2);
$off2 .="%";
$on2 .="%";
}else{
$off2 = $off;
$on2 = $on;
}
echo $off2 ." 關閉<br>". $on2 ." 開啟";
}else{
imagecopy($im3, $im, 0, 0, 0, 0, $img['x'], $img['y']);
@header("Content-type: image/png");
imagepng($im3);
imagedestroy($im3);
}
imagedestroy($im);
imagedestroy($im2);
return TRUE;
}else{
return False;
}
}
?>