jpgマップの各ポイントを反映するマップポイントを使用してデータベーステーブルをシードしようとしています。コードは小さな3x7画像を使用したテストですが、アプリケーションははるかに大きなマップを対象としており、マップポイント情報が各ポイントに関連付けられてテーブルに保存されます。map_terrainフィールドは、特定の座標の色の16進値を表します。しかし、うまくいかないのは関連するx、y値です。
最終的に、テーブルの各行はmap_point_x = 7およびmap_point_y = 3になります。map_terrainフィールドには正しい値があります。
これはどのように迷っていますか?
$image_filespec = '../test.jpg';
$map_id = 1;
include_once '../includes/functions.php';
function heximagecolorat($image, $x, $y, $sharp_prefix = TRUE) {
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
If ($sharp_prefix == TRUE) {
$hex = "#";
} else {
$hex = "";
}
$hex .= str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($g), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($b), 2, "0", STR_PAD_LEFT);
return $hex; // returns the hex value including the number sign (#)
}
if ($image_filespec == 'nnn.jpg' ){
echo 'you need to edit teh php file to make this work and alter $image_filespec as appropriate';
return;
}
//$result = query_db('SELECT * FROM users');
//
//while($row = $result->fetch()){
// echo $row['uID'] . " - " . $row['uUsername'] . "<br/>";
//}
$image = imagecreatefromjpeg($image_filespec); // imagecreatefromjpeg/png/
$width = imagesx($image);
$height = imagesy($image);
$colors = array();
for ($y = 0; $y < $height; $y++) {
for ($x = 0; $x < $width; $x++) {
$colors[] = heximagecolorat($image, $x, $y, $sharp_prefix=FALSE) ;
}
}
// connect and insert arrya to database $row in this context is talking about table row as opposed to row from the original image
$dbh = db_connect();
$query = "INSERT INTO map_points ( map_id, map_point_x, map_point_y, map_terrain ) VALUES "; //Prequery
$qPart = array_fill(0, count($colors), "( ?, ?, ?, ?)");
$query .= implode(",",$qPart);
$stmt = $dbh -> prepare($query);
$i = 1;
$point=0;
for ($y = 0; $y < $height; $y++) {
for ($x = 0; $x < $width; $x++) { //bind the values one by one
$stmt -> bindParam($i++, $map_id);
$stmt -> bindParam($i++, $x);
$stmt -> bindParam($i++, $y);
$stmt -> bindParam($i++, $colors[$point]);
$point += 1;
}
}
$temp=$stmt;
$stmt -> execute(); //execute
私はこの問題は、の使用であると信じてPDOStatement::bindParam()
、このバインド変数の値(のような:$x
、$y
、...)クエリ実行の瞬間に。これはすなわち、ループ後の最終的な値になります3
し、7
あなたの例では。
PDOStatement::bindValue()
代わりに使用してください。
さらに、行の数と同じ数のタプルを使用する代わりに、ポイントごとにステートメントを実行すると、コードの読み取りと保守がより簡単になる場合があります。つまり、INSERT INTO map_points(map_id, map_point_x, map_point_y, map_terrain) VALUES (?, ?, ?, ?)
1つではなく複数を使用しますINSERT INTO map_points(map_id, map_point_x, map_point_y, map_terrain) VALUES (?, ?, ?, ?), (?, ?, ?, ?), ...
。複数のタプルの方がパフォーマンスは優れていますが、INSERTステートメントをトランザクションでラップすることで同様のパフォーマンスを実現できます。
$dbh = db_connect();
$stmt = $dbh->prepare('INSERT INTO map_points (map_id, map_point_x, map_point_y, map_terrain) VALUES (?, ?, ?, ?)');
$y = 0;
$x = 0;
$stmt->bindValue(1, $map_id);
$stmt->bindParam(2, $x);
$stmt->bindParam(3, $y);
$dbh->beginTransaction();
for (; $y < $height; $y++) {
for (; $x < $width; $x++) {
$stmt->bindValue(4, heximagecolorat($image, $x, $y, $sharp_prefix=FALSE));
$stmt->execute();
}
}
$dbh->commit();
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加