Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a picture with GD containing other images

Tags:

php

image

gd

I would like to create a picture in PHP with GD composed by different other pictures. For example I have 6 pictures (or more) and I would like to create ONE picture who contain these different pictures.

The Difficulty is that my final picture must have a fixed width and height (304x179), so if the different pictures are too big they must be cut. This is an example from IconFinder :

This picture have 6 images

This picture is composed by 6 images, but the 3rd bird (green) is cutted, and the 4, 5 and 6 are cutted in the bottom. This is what I want, can you give me some help to write this code in PHP ?

Thanks

like image 874
Jensen Avatar asked May 28 '10 23:05

Jensen


People also ask

How to manipulate images in PHP using GD?

The first step is to create an image resource with functions such as imagecreatefrompng() . After that, you can use the function imagecolorat($image, $x, $y) to get the index of the color of a given pixel. The position of the pixel is determined by the second and third parameters in the function.

Which type of pictures can be created by using GD library?

GD is an open source code library for the dynamic creation of images. GD is used for creating PNG, JPEG and GIF images and is commonly used to generate charts, graphics, thumbnails on the fly.

How do I echo an image in PHP?

You cant echo an image using PHP. echo is for strings only. However, you can echo the image source - img src="" Just make sure you put the picture extension at the end of the file you are grabbing.


1 Answers

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

From there, use imagecopy() to copy the smaller images into 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 which I am placing in the canvas such that its top left corner is located at 275, 102 in the canvas, which cuts off the right side.

Edit

I adjusted the code to be more similar to what you're doing.

like image 55
JYelton Avatar answered Oct 07 '22 17:10

JYelton