How to flip image using php in codeigniter?

Member

by dana , in category: PHP Frameworks , 4 months ago

How to flip image using php in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by dalton_moen , 4 months ago

@dana 

To flip an image using PHP in CodeIgniter, you can use the GD library which is a graphics library for image processing.


Here is an example code to flip an image horizontally using CodeIgniter:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public function flipImage($imagePath) {
    // Load the image
    $image = imagecreatefromjpeg($imagePath);
    
    // Get image width and height
    $width = imagesx($image);
    $height = imagesy($image);
    
    // Create a new image to store the flipped image
    $flippedImage = imagecreatetruecolor($width, $height);
    
    // Flip the image horizontally
    for($x = 0; $x < $width; $x++) {
        imagecopy($flippedImage, $image, $width - $x - 1, 0, $x, 0, 1, $height);
    }
    
    // Save the flipped image
    imagejpeg($flippedImage, $imagePath);
    
    // Free up memory
    imagedestroy($image);
    imagedestroy($flippedImage);
}


You can call this function passing the path of the image you want to flip:

1
$this->flipImage('path/to/image.jpg');


This code will flip the image horizontally and save the flipped image back to the same path. You can modify the code to flip the image vertically or both horizontally and vertically as needed.