简体   繁体   中英

How to flip part of an image horizontal with PHP GD

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);

Found here :

//**************************************
// Name: Flip Image with GD
// Description:These are two easy to use functions that will flip an image, either horizontally or vertically. Just create an image, then execute one of these functions on the image resource. The image resource is passed by reference to the functions, so no return values are sent from the functions. Example shown in code.
// By: Ryand833
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1876&lngWId=8//for details.//**************************************

<?php
function flipVertical(&$img) {
 $size_x = imagesx($img);
 $size_y = imagesy($img);
 $temp = imagecreatetruecolor($size_x, $size_y);
 $x = imagecopyresampled($temp, $img, 0, 0, 0, ($size_y-1), $size_x, $size_y, $size_x, 0-$size_y);
 if ($x) {
$img = $temp;
 }
 else {
die("Unable to flip image");
 }
}
function flipHorizontal(&$img) {
 $size_x = imagesx($img);
 $size_y = imagesy($img);
 $temp = imagecreatetruecolor($size_x, $size_y);
 $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");
 }
}
$myimage = imagecreatefromjpeg("psclogo.jpg");
flipHorizontal($myimage);
header("Content-type: image/jpeg");
imagejpeg($myimage);
?>

Using this idea as a starting point, what you are going to do is call imagecopyresample on each slice of the image (as you have above), utilizing the height/width to reverse the slices.

Then you would call imagecopy on each "resampled" slice to copy it into your new, working image.

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.

See the image_flip function in comments at http://php.net/imagecopy

edit here is the exact link: http://cz2.php.net/manual/en/function.imagecopy.php#85992

edit 2 To implement it into your code, just use the line

$flippedOutput = ImageFlip($output,IMAGE_FLIP_HORIZONTAL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM