Skip to content

Instantly share code, notes, and snippets.

@mgng
Last active July 8, 2019 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgng/dd58b34e41effbf490994a253277883c to your computer and use it in GitHub Desktop.
Save mgng/dd58b34e41effbf490994a253277883c to your computer and use it in GitHub Desktop.
how to create a wave image by PHP(GD)
<?php
/**
* @param resource $img
* @param int $width
* @param int $height
* @param int $period
* @param int $amplitude
* @return resource
*/
function imageWave($img, $width, $height, $period = 10, $amplitude = 5) {
$p = $period * rand(1, 3);
$k = mt_rand(0, 100);
for ($i = 0; $i<$width; $i++) {
imagecopy($img, $img, $i-1, sin($k+$i/$p) * $amplitude, $i, 0, 1, $height);
}
$k = mt_rand(0,100);
for ($i = 0; $i<$height; $i++) {
imagecopy($img, $img, sin($k+$i/$p) * $amplitude, $i-1, 0, $i, $width, 1);
}
return $img;
}
$img = imagecreatefromjpeg("test.jpg");
$img = imageWave($img, imagesx($img), imagesy($img));
imagejpeg($img, "out.jpg");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment