Home Tutorial Php Phpgd PHP GD Punch

 
 

Share on Google+Share on Google+
PHP GD Punch
Posted on: November 7, 2009 at 12:00 AM
Advertisement
This example shows how to display punch in php gd.

<?php

$image = imagecreatefromjpeg("images.jpg");

$displacement = displacement($image);

$image = imagedisplace($image,$displacement);

header("Content-type: image/jpeg");

imagejpeg($image);

imagedestroy($image);

function displacement($i) {

$width = imagesx($i);

$height = imagesy($i);

for ($y=0;$y<$height;$y++) {

for ($x=0;$x<$width;$x++) {

$pos_x = $x - ($width/2);

$pos_y = $y - ($height/2);

$theta = atan2(($pos_y),($pos_x));

$radius = sqrt($pos_x*$pos_x + $pos_y*$pos_y);

$newRadius = $radius * $radius/(max(($width/2), ($height/2)));

$newX = ($width/2) + ($newRadius * cos($theta));

if ($newX > 0 && $newX < $width) {

$dis_x = $newX;

} else {

$dis_x = 0;

}

$newY = ($height/2) + ($newRadius * sin($theta));

if ($newY > 0 && $newY < $height) {

$dis_y = $newY;

} else {

$dis_y = 0;

}

$dis_x = ($dis_x < 0) ? $dis_x + $width : $dis_x;

$dis_x = ($dis_x >= $width) ? $dis_x - $width : $dis_x;

$dis_y = ($dis_y < 0) ? $dis_y + $height : $dis_y;

$dis_y = ($dis_y >= $height) ? $dis_y - $height : $dis_y;

$displacement['x'][$x][$y] = $dis_x;

$displacement['y'][$x][$y] = $dis_y;

}

}

return $displacement;

}

function imagedisplace($i,$displacement) {

$width = imagesx($i);

$height = imagesy($i);

$temp = imagecreatetruecolor($width,$height);

imagecopy($temp,$i,0,0,0,0,$width,$height);

for ($y=0;$y<$height;$y++) {

for ($x=0;$x<$width;$x++) {

$rgb = imagecolorat($temp,$displacement['x'][$x][$y],$displacement['y'][$x][$y]);

$a = ($rgb >> 24) & 0xFF;

$r = ($rgb >> 16) & 0xFF;

$g = ($rgb >> 8) & 0xFF;

$b = $rgb & 0xFF;

$col = imagecolorallocatealpha($i,$r,$g,$b,$a);

imagesetpixel($i,$x,$y,$col);

}

}

imagedestroy($temp);

return $i;

}

?>

After running the program you will get the following output

Advertisement

Related Tags for PHP GD Punch:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 7, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com