Asked 7 years ago
24 Dec 2016
Views 1621
web-api

web-api posted

flip image in php by GD

How to do image flip at php porgramming by GD library

original image


how to do horizontal and vertical image flip at php . use GD library or any other library
is that possible without even GD library usage ?
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

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
good explanation and function but it should support both flip also - shyam  
Dec 24 '16 03:41
shyam

shyam
answered Nov 30 '-1 00:00

if you using php 5.5 or latest than imageflip will do image flip in one line of code.

Flip image vertically

$im = imagecreatefromjpeg($file);
imageflip($file, IMG_FLIP_VERTICAL);


// Flip image horizontally


$im = imagecreatefromjpeg($file);
imageflip($im, IMG_FLIP_HORIZONTAL);



// Flip image horizontally / vertically both


$im = imagecreatefromjpeg($file);
imageflip($im, IMG_BOTH);
Mahesh Radadiya

Mahesh Radadiya
answered Nov 30 '-1 00:00

sorry its not in GD but i use ImageMagick :: image Processing Lib


function flipImage($imagePath) {
    $imagick = new Imagick(realpath($imagePath));
    $imagick->flipImage();
    header("Content-Type: image/jpg");
    echo $imagick->getImageBlob();
}


Imagick::flipimage -- Creates vertical flip function
Imagick::flopImage — Creates a horizontal flip

Post Answer