PHP/GD image mapping/texturing - php

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.

Related

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

How to put a watermark text across the whole image in diagonal position?

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).

Save HTML table as an image

I have a website I am building that takes data (the person's name) from a form and posts it into a customized "note" in a table on the next page. I want the user to then be able to save their "note" as an image with their customized name on it. Is there a way to save a SPECIFIC HTML TABLE as a PNG? Maybe take a screenshot of a specific coordinate on a web page?
If not is there a way to render posted PHP content from the form in an HTML canvas?
Any help would be great. I'm in a little over my head right now.
While you can render HTML using a variety of tools, you can't be sure that those tools will render the HTML the same way your browser renders it.
If your end goal is to generate an image with text on it, then let's solve that problem instead of trying to make the solution you suggested work.
Have a look at PHP's imagettftext() function. It contains an Example #1 which creates a small, simple image from text that can be stored in any variable ... including a form variable.
By using this example, and adding a few other of PHP's GD functions, you can make a decent replica of a table, and make sure it looks exactly the way you want it, rather than the way html2ps or some other tool renders it.
<?php
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = isset($_POST['name']) ? $_POST['name'] : "name";
// Replace path by your own font path
$font = 'arial.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
Here's sample output generated by the above script:
Note that you'll need to provide arial.ttf per the instructions at the link above.
If things don't work, look for errors both on-screen and in your web server's error log FIRST, before following up here. It's possible that PHP's GD module is not installed on your web server. If this is the case, you should check with your server administrator to make sure what you need is available to you.
You can try this JS library on client side.
workable solution
use fpdf to create pdf from html (table)
use imagemagick to convert pdf to png
You can also use Xvfb. It's a linux package. It stands for "X-window Virtual Frame Buffer" (if you were wondering why on earth it had such an esoteric name).
What that will do, is load a page, execute any JavaScript and apply the different stylings from CSS, all in the frame-buffer of the server. Then you can save the image.
Xvfb will probably not be available on most shared-hosting services, however, if that doesn't matter, then it would be a viable solution.
You can use something like html2ps

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

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

Categories