Create an image using GD containing other images

I would like to create a picture in PHP with GD made up of other pictures. For example, I have 6 (or more) pictures, and I would like to create ONE picture that contains these different pictures.

The difficulty is that my final image should have a fixed width and height (304x179), so if the various images are too large, they need to be cropped. This is an example from IconFinder:

This picture have 6 images

This picture consists of 6 images, but the 3rd bird (green) is cut out, and 4, 5 and 6 are cut out below. This is what I want, can you help me write this code in PHP?

thanks

+6
source share
2 answers

Create your main image and consider it your "canvas."

From there, use imagecopy () to copy smaller images to the canvas image.

See this for example:

<?php header('Content-Type: image/jpg'); $canvas = imagecreatetruecolor(304, 179); $icon1 = imagecreatefromjpeg('icon.jpg'); $icon2 = imagecreatefromjpeg('icon2.jpg'); // ... add more source images as needed imagecopy($canvas, $icon1, 275, 102, 0, 0, 100, 100); imagecopy($canvas, $icon2, 0, 120, 0, 0, 100, 100); // ... copy additional source images to the canvas as needed imagejpeg($canvas); ?> 

In my example, icon.jpg is a 100x100 image that I put on the canvas, so its upper left corner is at 275, 102 in the canvas, which cuts off the right side.

Edit

I adjusted the code to be more like what you are doing.

+13
source

Here is not a single tested modified spinet from one of my scripts, hope this can be useful:

  header('Content-type: image/png'); $image = array() //Populate this array with the image paths //Create the Letters Image Objects foreach($image as $a){ $image['obj'][] = imageCreateFromPNG($a); }unset($a); $canvasW = 300; $canvasH = 300; //Create Canvas $photoImage = imagecreatetruecolor($canvasW,$canvasH); imagesavealpha($photoImage, true); $trans_color = imagecolorallocatealpha($photoImage, 0, 0, 0, 127); imagefill($photoImage, 0, 0, $trans_color); //Merge Images $Offset_y = 0; $images_by_row = 3; $images_rows_height = 100; // height of each image row $counter = 0; foreach($image['obj'] as $a){ $counter++; $width = ceil(imagesx($a)); $height = ceil(imagesy($a)); if(!isset($offset)){ $offset = 1; } imageComposeAlpha($photoImage, $a, $offset, $Offset_y,$width,$height); if($offset >= 1){ $offset = $offset + $width; } //Check if new row next time if($counter >= $images_by_row){ if($images_by_row%$counter){ $offset_y += $images_rows_height; } } }unset($a); imagepng($photoImage); 
0
source

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


All Articles