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);
?>
Related
We are processing nearly 20M existing images (not files), all of which have been converted to the raw PNG data. Each image is always 640x480, and we want them all to be saved as square 640x640 files, with the original image on the top of the larger canvas, leaving the additional lower 160px for descriptive text to be added.
Our PNG data being retrieved is like this:
data:image/png;base64,iVBORw0 ... kJggg==
I've seen lots of posts and examples showing work with resizing, transparencies, colors, etc., but cannot determine where to start creating a new empty 640x640 "canvas" with existing PNG data.
If this is a duplicate question, I'd be happy to delete it if I can get some direction on how to begin. Without knowing the basics to get started, googling things like "image create png canvas" hasn't been helpful. Clearly, image processing is new to me, so apologize if I'm not being clear enough. Thanks in advance.
This should work for you:
$imgData = 'data:image/png;base64,iVBORw0 ... kJggg==';
$img = imagecreatefromstring(base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $imgData)));
$targetImage = imagecreatetruecolor(640, 640);
imagecopyresampled($targetImage, $img, 0, 0, 0, 0, 640, 480, 640, 480);
imagepng($targetImage, $yourPath);
Of course you might want to add some checks, background color etc. but I think you will be ready to start with this.
I have two images (both maps) one is a plain map and one is a map that had pins on it.
Image one
Image Two
I'm trying to subtract them from each other so I'm left with just the pins as a transparent png.
I've had some success with this and have managed to get the pins as their own image, the problem is that the colours aren't quite right (see image 3).
Image Three
I'm using Imagick to do this and my code is below
<?php
// load in the base image into Imagick
$imageOne = new Imagick('images/base-map.png');
$imageTwo = new Imagick('images/pins/location-7.png');
$imageOne->compositeImage($imageTwo, Imagick::COMPOSITE_DIFFERENCE, 0, 0);
$imageOne->paintTransparentImage($imageOne->getImagePixelColor(0, 0), 0, 5000);
header('Content-Type: image/png');
echo $imageOne;
Does anyone know how I can tidy/tweak this to make the colours match the original image?
Thanks!
You subtract from the pins as well, thats why they look different.
I'm not sure how to execute this in Imagick, but what you want to do is:
Subtract map from map with Pins, create a mask (put pin pixels 1, former map pixels 0) then multiply this with the map with pins.
Then you are left with the pins.
This might help:
http://www.imagemagick.org/script/fx.php
I've managed to get this to work using the masking technique mentioned by Piglet and wandering-warrior and with help from this post as well
In case anyone else needs it the code is here:
<?php
$base = new Imagick('images/base-map.png'); // blank map
$mask = $base; // copy of this to create the mask with
$imageTwo = new Imagick('images/pins/location-7.png'); // image with pins on it
// create the mask
$mask->compositeImage($imageTwo, Imagick::COMPOSITE_DIFFERENCE, 0, 0);
$mask->paintTransparentImage($mask->getImagePixelColor(0, 0), 0, 5000);
$mask->compositeImage($mask, Imagick::COMPOSITE_DIFFERENCE, 0, 0);
// remove the mask from the image with pins
$imageTwo->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);
$imageTwo->paintTransparentImage('#000', 0, 5000);
header('Content-Type: image/png');
echo $imageTwo;
The mask looks like so
and the final output like so
Hopefully this will help someone else who needs it!
You'll need to create a mask for this where its transparent for all the pins. More detailed here...
http://www.imagemagick.org/discourse-server/viewtopic.php?t=16279
http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=15584&start=0
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);
?>
I have connected my Google Drive with my website and im getting images in this format: https://googledrive.com/host/{$GoogleID}. What i want to do is to add a text (not image) watermark to all the images im getting from Google Drive. I already tried with:
http://php.net/manual/en/image.examples.merged-watermark.php
http://phpimageworkshop.com/tutorial/1/adding-watermark.html
Both of them dosent work for me or i cant get them to work i dont know why. I will give an example for an image url: https://googledrive.com/host/0B9eVkF94eohMRlBQVENRWE5mc2c
I have also tried the code from this answer, but it dosent work as well. I guess the problem should be that the files i'm getting from Google Drive are not with the file extention and maybe this cause the problem. This is only my guess...
UPDATE:
i managed to show the photo on the website, but how to put the text in diagonal possition across all the photo like this
try to read image with file_get_contents or fopen and create image from string.
$im = imagecreatefromstring(file_get_contents("image url"));
and then use this example: http://php.net/manual/en/image.examples.merged-watermark.php
Thanks to #Durim Jusaj for helping me out with this one and finally i got the answer after a lot of searching and dealing with a lot of problems i will share my knowledge with u guys. So i will post the code and i will explain what is the code doing.
First thing first. Include this function in your file. This function is finding the center of the image. I havent wrote it i found it in the php docs under the comments so maybe it can be done without it or this function can be modified to be better written.
function imagettftext_cr(&$im, $size, $angle, $x, $y, $color, $fontfile, $text)
{
// retrieve boundingbox
$bbox = imagettfbbox($size, $angle, $fontfile, $text);
// calculate deviation
$dx = ($bbox[2]-$bbox[0])/2.0 - ($bbox[2]-$bbox[4])/2.0; // deviation left-right
$dy = ($bbox[3]-$bbox[1])/2.0 + ($bbox[7]-$bbox[1])/2.0; // deviation top-bottom
// new pivotpoint
$px = $x-$dx;
$py = $y-$dy;
return imagettftext($im, $size, $angle, $px, $py, $color, $fontfile, $text);
}
Load the image you want to apply the watermark to. I'm using Google Drive to extract my photos so thats why im using the file_get_contents, if you are the case like mine then use this, BUT otherwise use what is common and if your picture have extension like .jpg or .png you can use imagecreatefromjpeg or imagecreatefrompng. So it should be something like that $im = imagecreatefromjpeg('photo.jpeg'); for example.
$im = imagecreatefromstring(file_get_contents("https://drive.google.com/uc?id=" . $v['GoogleID']));
After that we need to calculate the position of watermark so we want the watermark to be in the center and to auto adjust its size based on the size of the photo. So on the first like we store all the attributes of the image to array. Second and third lines we calculate where is the center of the image (for me dividing it by 2.2 worked great, you can change this if u want to adjust the position. Last line is the size of the watermark according to the size of the image. This is very important as the watermark will be small or very big if u have images with different sizes.
list($width, $height, $type, $attr) = getimagesize("https://drive.google.com/uc?id=" . $v['GoogleID']);
$width = $width / 2.2;
$height = $height / 2.2;
$size = ($width + $height) / 4;
Set the content-type
header('Content-Type: image/png');
Create the image. I dont know why it should be done twice, but im following the php manual.
$im = imagecreatefromstring(file_get_contents("https://drive.google.com/uc?id=" . $v['GoogleID']));
Create some colors. So, if you want your watermark to be transparent (like mine) you have to use imagecolorallocatealpha, otherwise use imagecolorallocate. Last parameter is the transparency. You can google every function name for more info from the php doc.
$black = imagecolorallocatealpha($im, 0, 0, 0, 100);
The text to draw. Simple as that. Write what you want your text to be.
$text = 'nima.bg';
Replace path by your own font path. Put a font in your server so the code can take it (if you dont have already).
$font = 'fonts/ParsekCyrillic.ttf';
Adding all together. Basically you are creating the final image with the water mark with this function. The magic ;)
imagettftext_cr($im, $size, 20, $width, $height, $black, $font, $text);
Now this is the tricky part! If you want to just display the image without saving it to your server you can simply copy and paste this code, but the website will load very slow if you have more then 2-3 images. So, what i suggest you to do it to save the final images to your server and then display it. I know you will have duplicates, but this is the best choice i think.
ob_start();
imagepng($im);
$image = ob_get_contents();
ob_end_clean();
imagedestroy($im);
echo "<img src='data:image/png;base64," . base64_encode($image) . "'>";
}
OR you can save the images to your server and then display them which is much much faster. The best thing you can do is to process all the images create them with the watermark and then display them (so you have them ready to be show when a visitor visit your website).
imagepng($im, 'photo_stamp.png');
imagedestroy($im);
And the final result for me was that.
UPDATE: As of 2017 you can extract the image using this link 'https://drive.google.com/uc?id=(GoogleID)' or you can just use the 'webContentLink' property of the Google_DriveFile Object (it will give you the same link).
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.