Php merging an image with its own flipping and rotating view at different positions

I want to combine the image with its own image flipping and rotate at an angle of, say, 60 degrees.

Suppose I have an image of 1000 * 1000 px, then A: the original image is resized to 500 * 500 pixels and the location in the lower right is 1000 * 1000 square squares B: the same image is inverted and rotated -600 degrees and place in the upper left corner 1000 * 1000 square box

everything works fine, but just a question, like 1: some border for rotated images 2: the background for rotation is one darker so that it looks odd.

test case:

sample image (thumbnail reduced, click to open original image):

enter image description here

image output :

enter image description here

Below is my code

$image1=$image2=imagecreatefrompng('a.png');
//filter_opacity( $image1, 25 );

$w=imagesx($image1);
$h=imagesy($image1);


$finala = imagecreatetruecolor($w, $h);
$finalb = imagecreatetruecolor($w, $h);
$finalc = imagecreatetruecolor($w, $h);

$backgroundColora = imagecolorallocate($finala, 250,252,252); // gray
$backgroundColorb = imagecolorallocate($finalb, 250,250,250); // gray
$backgroundColorc = imagecolorallocate($finalc, 250,250,250); // gray

imagefill($finala, 0, 0, $backgroundColora);
imagefill($finalb, 0, 0, $backgroundColorb);
imagefill($finalc, 0, 0, $backgroundColorc);


$percent = 0.583;
$new_width = $w * $percent;
$new_height = $h * $percent;
$wshift = $w/8.5;
$hshift = $h/2.5;


imagecopy($finala, $image1, 0,0,0,0,$w,$h);
imagecopyresampled($finala, $image2,$wshift,$hshift,0,0, $new_width, $new_height, $w, $h);
imagecopy($finalc, $image1, 0,0,0,0,$w,$h);
imagecopyresampled($finalc, $image2,$wshift,$hshift,0,0, $new_width, $new_height, $w, $h);
imagecopyresampled($finalb, $finalc,$w*0.3,$h*0.3,0,0, $w*0.6, $h*0.6, $w, $h);
imageflip($finala, IMG_FLIP_HORIZONTAL );
$finala = imagerotate($finala, -60, imageColorAllocateAlpha($finala, 0, 0, 0, 127));
imagecopyresampled($finalb, $finala,-$w*0.1,-$h*0.1,0,$h*0.20, $new_width, $new_height, $w, $h);
header('Content-Type: image/jpeg');
imagejpeg($finalb);
imagedestroy($finala);
imagedestroy($image2);
imagedestroy($image1);
imagedestroy($final);
+4
source share
2 answers

As you need to rotate and place the same image on the top and bottom, you do not need to transfer the same image twice with $ _GET just once

// Source image
$image = imagecreatefrompng("test.png");
$w=imagesx($image);
$h=imagesy($image);
$finalImage = imagecreatetruecolor($w, $h);

// Bg add
$backgroundColor = imagecolorallocate($finalImage, 252,252,252); // gray
imagefill($finalImage, 0, 0, $backgroundColor);

// New sizes calculation
$percent = 0.5;
$new_width = $w * $percent;
$new_height = $h * $percent;

// First image add
imagecopyresampled($finalImage, $image, $new_width, $new_height, 0, 0,
                   $new_width, $new_height, $w, $h);

// Second image rotate with transparant bg
$transparency = imagecolorallocatealpha( $image,0,0,0,127 );
$rotatedImage = imagerotate( $image, -60, $transparency, 1);
imagealphablending( $rotatedImage, false );
imagesavealpha( $rotatedImage, true );

// Getting new rotated image sizes to avoid cutting border
$rw = imagesx($rotatedImage);
$rh = imagesy($rotatedImage);

// Second rotated image add with bigger space
imagecopyresampled($finalImage, $rotatedImage, 0, 0, 0, 0, 
                   $rw*$percent, $rh*$percent, $rw, $rh);
// Outputing png image 
header( 'Content-Type: image/png' );
imagepng( $finalImage );
0
source

change this line:

$finala = imagerotate($finala, -60, imageColorAllocateAlpha($finala, 0, 0, 0, 127));

to

$finala = imagerotate($finala, -60, imageColorAllocateAlpha($finala, 250, 252, 252, 127));

the black board should disappear!

0
source

Source: https://habr.com/ru/post/1628252/


All Articles