-
-
Save unickorn/50010890db5095ab4d1d9f3877ea5642 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($xi = 0; $xi < $scale; ++$xi){ | |
for($yi = 0; $yi < $scale; ++$yi){ | |
imagesetpixel($newImage, ($x + $xOffset) + $xi, ($y + $yOffset) + $yi, imagecolorat($image, $x, $y)); // Set the pixel at +($xi, $yi) | |
} | |
} | |
$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