Last active
April 19, 2021 08:18
-
-
Save TwistedAsylumMC/c3121158200cb1e64873310eee8a3dab to your computer and use it in GitHub Desktop.
Increases the size of each pixel within an image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Validate file input | |
$file = $argv[1] ?? ""; | |
if($file === ""){ | |
echo "Please provide an image" . PHP_EOL; | |
return; | |
} | |
if(substr($file, -4) !== ".png"){ | |
echo "This file is not a image" . PHP_EOL; | |
return; | |
} | |
if(!file_exists($file)){ | |
echo "Image not found" . PHP_EOL; | |
return; | |
} | |
// Validate scale input | |
$scale = (int)$argv[2] ?? 0; | |
if($scale < 2){ | |
echo "Scale must be greater than 1" . PHP_EOL; | |
return; | |
} | |
$image = imagecreatefrompng($file); // Create image from input | |
[$width, $height] = getimagesize($file); // Get input image width & height | |
$newImage = imagecreatetruecolor($width * $scale, $height * $scale); // Create a new image, $scale times bigger than the input image | |
$xOffset = $yOffset = 0; // Define offset variables | |
// Loop through every pixel in the X axis in the input image | |
for($x = 0; $x < $width; ++$x){ | |
// Loop through every pixel in the Y axis in the input image | |
for($y = 0; $y < $height; ++$y){ | |
imagesetpixel($newImage, ($x + $xOffset), ($y + $yOffset), imagecolorat($image, $x, $y)); // Set the original pixel | |
for($i = 0; $i < $scale; ++$i){ | |
imagesetpixel($newImage, ($x + $xOffset), ($y + $yOffset) + $i, imagecolorat($image, $x, $y)); // Set the pixel at +(0, $i) | |
imagesetpixel($newImage, ($x + $xOffset) + $i, ($y + $yOffset), imagecolorat($image, $x, $y)); // Set the pixel at +($i, 0) | |
imagesetpixel($newImage, ($x + $xOffset) + $i, ($y + $yOffset) + $i, imagecolorat($image, $x, $y)); // Set the pixel at +($i, $i) | |
} | |
$yOffset += $scale - 1; // Increase Y offset | |
} | |
$yOffset = 0; // Reset Y offset | |
$xOffset += $scale - 1; // Increase X offset | |
} | |
imagepng($newImage, substr($file, 0, -4) . "-enlarged.png"); // Create the enlarged image | |
// Destroy images after completion | |
imagedestroy($image); | |
imagedestroy($newImage); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lines 42-44 result in a crossed box instead of a fully filled "enlarged pixel" when the scale is greater than 2. (example with scale 8)
