Merging more than 1 image with a base image - php

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

Related

Merge multiple images dynamicallly with 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.

Laravel imagecopy lowers image quality

I am coding a Laravel project where I let users add their own small image to a bigger image (something like milliondollarhomepage.com).
I use the PHP imagecopy(); to add the small image to the bigger image. The problem is: the colors get really poor after merging the two images together. I think the new image only has the colors of the previous image, so no new colors can be added to the image.
My code:
$pixel = GET_INFO_HERE;
$dest = imagecreatefrompng(public_path('storage/pixels.png'));//get big image
$src = imagecreatefrompng(public_path('storage/uploads/' . $pixel->imagename));//get small image to add
imagecopy($dest, $src, $pixel->x_position, $pixel->y_position, 0, 0, $pixel->width, $pixel->height);
ob_start();
imagepng($dest);
$newpixelsimg = ob_get_clean();
Storage::put('public/pixels.png', $newpixelsimg);//save new image
imagedestroy($dest);
imagedestroy($src);
I tried to use imagetruecolortopalette($dest, false, 255); before the imagecopy(); method, but it didn't change anything.
I have been searching for solutions for a long time now, but I couldn't get it to work yet.
Is there anyone who has a solution for this?

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);
?>

Combine two images using PHP GD

I am trying to create an image which will have my image A (uploaded image) and image B (my watermark image). My problem is that I am not getting the proper way to extend the size of image from the bottom which will be created with image A and B.
My code is -
$img_width=imagesx($img);
$img_height=imagesy($img);
$watermark=imagecreatefrompng($watermark);
$watermark_width=imagesx($watermark);
$watermark_height=imagesy($watermark);
$image=imagecreatetruecolor($img_width, $img_height+35);
imagealphablending($image, false);
$dest_x=$img_width-$watermark_width;
$dest_y=$img_height-$watermark_height+20;
imagecopy($img, $watermark, $dest_x, $dest_y, 0, 0,$watermark_width, $watermark_height);
imagesavealpha($img, true);
imagejpeg($img, $config['pdir']."/t/l-".$thepp, 90);
}
What I am getting with this code is -
Results http://www.9gag.in/pdata/t/l-76.jpg
You can see the watermark image is not totally merged with the image I want to create. I want an extended area in the destination image where the watermark will be fitted properly.
When you writes :
$dest_y=$img_height-$watermark_height+20;
I guess you really want :
$dest_y=$img_height-$watermark_height-20;
Y = 0 at the top of your image, so the more bottom you go, the more high is your height.

Combine 2 or more pictures in PHP

Imagine I have 2 pictures, imagea.jpg and imageb.jpg
Image A
Image B
I want to combine these both pictures un just 1 and output them to a file imageab.jpg, just like here
I will do this running cron jobs, so I need to do that on PHP, but I'm getting troubles with previous codes. As additional information, I'm getting the ImageA/B URLs from MySQL and all pictures have the same width and height.
Thanks!
You can use imagecopymerge:
Something like this:
$dest = imagecreatefromgjpg('imagea.jpg');
$src = imagecreatefromjpg('imageb.jpg');
// Copy and merge
imagecopymerge($dest, $src, 10, 10, 0, 0, 100, 47, 75);
// Output and free from memory
header('Content-Type: image/jpeg');
imagejpeg($dest);
imagedestroy($dest);
imagedestroy($src);
Imagick is your friend.
For example Imagick::appendImages.
Take a look at GD and imagemagick, they are plenty of functions that can help you:
http://php.net/manual/en/function.imagecopymerge.php

Categories