Skip to content

Instantly share code, notes, and snippets.

@nico3333fr
Last active April 5, 2022 15:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nico3333fr/1d5dc5873aa6a717235ed605b5ac2dc6 to your computer and use it in GitHub Desktop.
Save nico3333fr/1d5dc5873aa6a717235ed605b5ac2dc6 to your computer and use it in GitHub Desktop.
Pixel Art helper script for Hama beads
<?php
// supposed to be a BMP image, as sprites are really small :)
if ( @$_GET['directfile']!='' ){
$img_name = $_GET['directfile'];
}
$resource = imagecreatefrombmp($img_name);
$width = imagesx($resource);
$height = imagesy($resource);
echo $width;
echo '<br>';
echo $height;
$table_pixels = [];
?>
<style>
.grid {
display: grid;
grid-template-columns: repeat(<?php echo $width; ?>, 10px);
grid-template-rows: repeat(<?php echo $height; ?>, 10px);
border: 1px solid black;
}
.grid div {
border: 1px solid black;
}
.grid .plaque_right {
border-right: 1px solid red;
}
.grid .plaque_bottom {
border-bottom: 1px solid red;
}
</style>
<h1>Pixel Art helper for Hama beads</h1>
<img src="<?php echo $img_name ?>" alt="">
<?php
echo '<div class="grid">';
for($y = 0; $y < $height; $y++) {
for($x = 0; $x < $width; $x++) {
// pixel color at (x, y)
$color = imagecolorat($resource, $x, $y);
$colors = imagecolorsforindex($resource, $color);
$table_pixels[$x][$y] = $colors;
// %29 because hama beads supports are 30x30
if ($colors['alpha'] != 0){
echo '<div></div>';
}
else {echo '<div class="'. ( $x%29==0 && $x!=0 ? ' plaque_right' : '' ) .' '. ( $y%29==0 && $y!=0 ? ' plaque_bottom' : '' ) .'" style="background: rgb('.$colors["red"].','.$colors["green"].','.$colors["blue"].')"></div>';}
}
}
echo '</div>';
// debug stuff
// echo '<pre>';
// print_r($table_pixels);
// echo '</pre>';

The MIT License (MIT)

Copyright (c) Nicolas Hoffmann

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment