Merge multiple images dynamicallly with PHP - php

I want to merge several image from a folder, into one image.
I've managed to do it with two images, but I don't like this method, because what if my folder has over 10 pictures?
$image1 = imagecreatefrompng('image1.png');
$image2 = imagecreatefrompng('image2.png');
imagealphablending($image1, true);
imagesavealpha($image1, true);
imagecopy($image1, $image2, 0, 0, 0, 0, 100, 100);
imagepng($image1, 'image3.png');
My question how can I access my folder and get all of my images without having to type $image1, $image2...over and over? Also can I do it in a Class?
Thanks everyone!
PS: I'm new to PHP but you can still be technical, the objective is to learn so I don't mind.

Related

Load transparent png into gd php but don't make it's background transparent

I'm trying to create images from dynamic text data in GD and put a logo in the top corner. The background color of the image will change based on the data passed in so I can't just save the logo as an image with no alpha channel.
I create the image, fill it with the dynamic background color using imagefill(), then add the text using imagettftext() and then load my logo in. I'm having a problem getting the logo into the image without it keeping its background color of 'transparent'. So I would like it to have the dynamic background color behind it that is set with imagefill(). However, it keeps the transparency background it was loaded in with and so writes this part of the png as transparent. I tried calling imagefill() on the logo after it had been loaded in (using the same rgb that sets the background of the destination image) but this didn't do anything.
Below is my code:
$background = $_GET['background'];
$data1 = $_GET['data1'];
$data2 = $_GET['data2'];
$r = $_GET['r'];
$g = $_GET['g'];
$b = $_GET['b'];
$png_image = imagecreate(400, 200);
$gd_text_color = imagecolorallocate($png_image, 255, 255, 255);
$gd_background_color = imagecolorallocate($png_image, $r, $g, $b);
imagefill($png_image, 0, 0, $gd_background);
$text1 = "test test $data1";
$text2 = "test test again $data2";
$font = 'Lato-regular.ttf';
imagettftext($png_image, 18, 0, 20, 20, $gd_text_color, $font, $text1);
imagettftext($png_image, 18, 0, 20, 50, $gd_text_color, $font, $text2);
//trying to get this logo and place it in the corner.
$logo = imagecreatefrompng("images/logo.png");
imagecopy($png_image, $logo, 10, 10, 0, 0, 100, 30);
header('Content-type: image/png');
imagepng($png_image, $filename);
imagedestroy($png_image);
Here's the output of that code: http://i.imgur.com/n25h9Js.png
And here's what the image looks like when loaded into a program that accepts an alpha channel: http://i.imgur.com/3OIRupN.png
Does anyone know how I'd achieve what I'm attempting?
Thanks for your time.
EDIT
To try and explain what I want here's another image. The top image is what I currently get, and the bottom image is what I want. I'm simply trying to load in a transparent PNG that can sit ontop of different colored backgrounds. However I either get it like how it is shown here (transparent background) or as a black background (because I guess the alpha channel isn't being looked at?). Hope this helps. Image:
Edit 2
As per the comment below, I changed it from imagecreate() to imagecreatetruecolor() and now it works fine! I would love an explanation why this solved it if anyone has the time but for now, thank you all who commented or even spent your time looking at this question.
I suspect your imagecreate() may be causing you problems as it creates a palettised image which doesn't have the flexibility or breadth of expression of a true-colour image.
I suggest you replace it with imagecreatetruecolor().

Uploading and Merging images in PHP

So I'm looking to create an internal tool where our employees can upload a picture of themselves, which automatically merges it with a white border at the bottom (company logo border) and their name on top of it. This way the offices can easily print the pictures for employee boards
So what I need is:
- merge border, picture and text into one image.
- Upload function with crop tool.
What I found is:
- PHP Image Magician (http://phpimagemagician.jarrodoberto.com/)
This basically has all functions I need available so naturally I got excited but I ran across one thing:
In the 14.1_upload.php file it refers to the following:
require_once('image_lib/upload_class.php');
the image_lib/upload_cass.php file doesnt come with the download from the website.
Is there something I'm missing or would you guys recommend not to use PHP Image Magician at all?
I'm looking to make it a very basic and simple tool but functional.
Thanks a bunch in advance
Uploading Image is a very easy process, you can use W3School reference to understand it well http://www.w3schools.com/php/php_file_upload.asp
About Image merge you can use below code:
<?php
$dest = imagecreatefrompng('vinyl.png');
$src = imagecreatefromjpeg('cover2.jpg');
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagecopymerge($dest, $src, 10, 9, 0, 0, 181, 180, 100); //have to play with these numbers for it to work for you, etc.
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
?>

Merging more than 1 image with a base image

I use this code to merge two images. Can someone please help me to merge 2 source images with 1 base image. For eg. I have these images base.jpg, image1.jpg, image2.jpg. I want to merge image1.jpg and image2.jpg on base.jpg. I'm using this code to merge two images.
$dest = imagecreatefromjpeg('base.jpg');
$src = imagecreatefromjpeg('$image1.jpg');
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagecopymerge($dest, $src, 229, 285, 0, 0, 70, 70, 100);
Imagejpeg($dest, '$new.jpg', 100);
imagedestroy($dest);
imagedestroy($src);
Basically we will be putting many different images all together on one big ‘canvas’ image. If you need to do that for some reason, read on to find out how.
Combining multiple images using PHP

Superimpose Images with PHP

I am searching for a way to overlay an image on an existing image.
e.g:
+
I have found a great example over here: PNG overlay using one single Image element.
but I have two problems with these.
First of all, I don't want the dimensions to be equal to each other. e.g (215*215 on 215*215). This is because my users would have the ability to choose where they want to put their image. (Top, left, bottom, top-right) so 8 directions.
The second problem is that in that example, only 2 images are allowed to overlay. My users (again) will have the ability to put multiple images on top of it.
I have a little knowledge of Javascript and PHP, so it would be great if you guys (and girls) could help me out.
Sincerely,
You can do this using GD library. There is function to "merge" images called imagecopymerge.
Here is a very simple example how to merge images:
<?php
header('Content-Type: image/jpeg');
$bg = imagecreatefromjpeg('background.jpg');
$img = imagecreatefromjpeg('image.jpg');
imagecopymerge($bg, $img, 0, 0, 0, 0, imagesx($bg), imagesy($bg), 75);
imagejpeg($bg, null, 100);
?>

PHP/GD image mapping/texturing

I'm trying to create a produkt configurator similar to: http://winmap.active-online.de/kler/pol/index.php3?room_name=200D
I'd like to put texture or color on diffrent image elements and put it together into one result image.
I'm trying to usa like this:
header('Content-Type: image/jpeg');
$poszycie = imagecreatefrompng('poszycie.png');
$tekstura = imagecreatefrompng('tekstura.png');
//imagefilter($poszycie,IMG_FILTER_EMBOSS);
//imagepng($tekstura);
//$bcg = imagecreatefromjpeg('las.jpg');
//$img = imagecreatefromjpeg('zdjecie.jpg');
//imagecopymerge($poszycie, $tekstura, 0, 0, 0, 0, imagesx($poszycie), imagesy($poszycie), 75);
//imagepng($poszycie, null, 100);
imagecopymerge($poszycie, $tekstura, 0, 0, 0, 0, imagesx($poszycie), imagesy($poszycie), 50);
imagepng($poszycie);
but result is :http://saveur.pl/testgd/configurator.png
Thanks for any help.
I think you'll be better off with ImageMagick. For example you could use these tricks http://www.imagemagick.org/discourse-server/viewtopic.php?f=2&t=14513&start=15 and I'm sure a web search for imagemagick texture image area would reveal more ideas.
Anyway you'll have to take apart the image and use the alpha channel.
I think my answer to this question can help. The problem deals with alpha channel. Besides, you should send this header (png instead of jpeg):
header('Content-Type: image/png');
since you are calling
imagepng($poszycie);
which may be another cause of trouble.

Categories