简体   繁体   中英

PHP GD image with 2 files

Here is what im trying to do. Be advise im fairly new to GD2

I want to make an image out of 2 images this way;

A background rectangle filled with images no 1

After that i want to draw a polygon over it filled with another image.

What i have right now is the rectangle and the image in background.

I can draw the polygon but i cant figure out how to fill it with another image. it is filled in blue right now and i would like to fill it with another image.

Heres my code

$values = array(
            40,  50,  // Point 1 (x, y)
            20,  240, // Point 2 (x, y)
            60,  60,  // Point 3 (x, y)
            240, 20,  // Point 4 (x, y)
            50,  40,  // Point 5 (x, y)
            10,  10   // Point 6 (x, y)
        );

$image2 = imagecreatefromjpeg('test2.jpg');
$image = imagecreatefromjpeg('test.jpg');

$bg   = imagecreatefromjpeg('test.jpg');

$fill = imagecolorallocate($image, 0, 0, 255);

// fill the background
imagefilledrectangle($image, 0, 0, 249, 249, $bg);

// draw a polygon
imagefilledpolygon($image, $values, 6, $fill);

// flush image
header('Content-type: image/jpg');
imagepng($image);
imagedestroy($image);

as you can see imagepng() render only $image how do i get it to render $image and $image2

Thanks all

You need to overlay the second image on top of the first.

$file1 = 'test.jpg';
$file2 = 'test2.jpg';

// First image
$image = imagecreatefromjpeg($file1);

// Second image (the overlay)
$overlay = imagecreatefromjpeg($file2);

// We need to know the width and height of the overlay
list($width, $height, $type, $attr) = getimagesize($file2);

// Apply the overlay
imagecopy($image, $overlay, 0, 0, 0, 0, $width, $height);
imagedestroy($overlay);

// Output the results
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);

I suggest you torn imagealphablending off for Image2, draw an inverse of your polygon on Image2 with a color with alpha value: 0. Turn imagealphablending on. And then you can copy Image2 over Image1 (the background).

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