Flip an Image horizontally

Mar 12, 2012   //   by phpfreelancer.biz   //   Blog, PHP Tutorial  //  No Comments

I need to flip an image so that a character faces in the right direction. This needs to be done “on the fly’ as they say.

The issue I am having is that with Gif images, I seem to lose the transparency. (The background goes white)

Check my solution here

$size_x = imagesx($img);
$size_y = imagesy($img);

$temp = imagecreatetruecolor($size_x, $size_y);

imagecolortransparent($temp, imagecolorallocate($temp, 0, 0, 0));
imagealphablending($temp, false);
imagesavealpha($temp, true);
$x = imagecopyresampled($temp, $img, 0, 0, ($size_x-1), 0, $size_x, $size_y, 0-$size_x, $size_y);
if ($x) {
    $img = $temp;
else {
    die("Unable to flip image");

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

Leave a comment

Share This Post

RSS Wordpress News