i managed to code one function which flip the image in PHP . it use GD library .
flipping done by imagecopyresampled function , this function copy one image or part of image to another image or part of image.
in flipping we copy same image (means source and destionation image is same) but different coordinate.
suppose you image have width = 500 , height= 500 so imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h ) have static argument value like this .
so Algo for horizontal flip ::
imagecopyresampled ($dest_image,$src_image , 0, 0, 0, 499, 500, 500, 500, -500 );
so conclusion is
1. for horizontal flip source height goes minus
2. and source (x, y) become (0 ,image height-1)
and Algo for vertical flip ::
imagecopyresampled ($dest_image,$src_image, 0, 0, 499, 0, 500, 500, -500, 500 );
so conclusion is
1. for vertical flip source width goes minus
2. and source (x, y) become (image width-1 , 0)
so that key part of code is like , its heart of function
$sx = $vert ? ($w - 1) : 0;// if vertical flip than source x become width-1 else 0
$sy = $horz ? ($h - 1) : 0;//If horizontal flip than source y become height-1 else 0
$sw = $vert ? -$w : $w;//if vertical flip than source width goes negative or either leave it positive
$sh = $horz ? -$h : $h;//if horizontal flip than source height goes negative or either leave it positive
thats it .
function flip_image($imagepath, $horz, $vert) {
$image_meta_data = pathinfo($imagepath);
switch ($image_meta_data['extension']) {
case 'jpeg':
case 'jpg':
$image = imagecreatefromjpeg($imagepath);
break;
case 'png':
$image = imagecreatefrompng($imagepath);
break;
case 'gif':
$image = imagecreatefromgif($imagepath);
break;
default:
return false;
break;
}
$w = imagesx($image);
$h = imagesy($image);
$dst = imagecreatetruecolor($w, $h);
if ( $dst ) {
$sx = $vert ? ($w - 1) : 0;
$sy = $horz ? ($h - 1) : 0;
$sw = $vert ? -$w : $w;
$sh = $horz ? -$h : $h;
if ( imagecopyresampled($dst, $image, 0, 0, $sx, $sy, $w, $h, $sw, $sh) ) {
imagedestroy($image);
$image = $dst;
}
switch ( $image_meta_data['extension'] ) {
case 'jpeg':
case 'jpg':
imagejpeg ($image,$imagepath);
break;
case 'png':
imagepng ($image,$imagepath);
break;
case 'gif':
imagegif ($image,$imagepath);
break;
}
}
}
how to use
1. vertical flip
$file=dirname(__FILE__).DIRECTORY_SEPRATOR.'image.jpg';
flip_image($file,0,1);//veritcal flip :: function will replace image with flapped image
2. horizontal flip
$file=dirname(__FILE__).DIRECTORY_SEPRATOR.'image.jpg';
flip_image($file,1,0);//horizontal flip :: function will replace image with flapped image