PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 10, 2022

[FIXED] How to flip part of an image horizontal with PHP GD

 October 10, 2022     gd, image, php     No comments   

Issue

I am creating an image with parts from another image. I use imagecopy to but the "parts" togheter. Some parts need to be flipped horizontal.

For example this part:

imagecopy($output, $input, 0,8, 44,20, 4,12);

How do i flip this part horizontal??

I have tried this:

imagecopy($output, $input, (0 - 1),8, 44,20, 4,12);

But it does not work.

I have searched for ages, but cant find anything that works

My script looks like this:

$input = imagecreatefrompng($image_file);
$output = imagecreatetruecolor(50, 40);

imagecopy($output, $input, 4,8, 20,20, 8,14);
imagecopy($output, $input, 12,8, 44,20, 4,14);
imagecopy($output, $input, 4,20, 4,20, 4,14);
imagecopy($output, $input, 8,28, 4,20, 4,14);
imagecopy($output, $input, 32,8, 52,20, 4,14);
imagecopy($output, $input, 24,20, 12,20, 4,14);
imagecopy($output, $input, 28,28, 12,20, 4,14); 
imagecopy($output, $input, 4,0, 40,8, 8,9);
imagecopy($output, $input, 24,0, 56,8, 8,9);


header('Content-Type: image/png');

imagepng($output);

imagedestroy($output);
imagedestroy($input);

Solution

I want to share the answer some years later... :)

Change:

imagecopy($output, $input, 8, 20, 4, 20, 4, 12)

To:

imagecopyresampled($output, $input, 8, 20, (8 - 1), 20, 4, 12, 0 - 4, 12);

Which will flip the part of the image horizontal.



Answered By - 2by
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing