How can I add a background to my png image? - php

I have a default image that I want my users when they first create their profile to have, and I need to each one of them have a different background color but with the same base shape that will be in a png file, I can do this by creating for each background color create a png but I dont think it might be the best choice. How can I do this?

Use Imagick extension to work on your image. You can set a background color for your transparent png image by simply doing this
shell_exec("convert testimage.png -fill '#CCCCCC00' -opaque none image_wth_bgcolor.png");
This will give a slight gray(#CCCCCC) background to the the image.
See this link.

My suggestion is to create a png file for each colour (which seems to be the only way)
and use PHP to randomly pick one of the images.

Related

How to change the shape of an image using PHP and imagemagick?

I need to manipulate images on the server-side via PHP. I use ImageMagick for that purpose, and there are a lot of great functions, but no documentation...
The issue is the next:
I have two images a png with a transparent background, and a jpg one;
I need to change the JPG image that has the same shape as the visible part of the PNG.
I don't want to crop the image, but to adjust to the PNG's visible part. Is that possible to do?
Example:

PHP ImageMagick making images transparent and drawing them ontop of other images

I am trying to make a program that can export images with another image(that is semi transparent) drawn on top of it. I'd like to be able to control the transparency on a per pixel value for the top image; and do it fairly quickly. Is there something built into ImageMagick to do this? Both of the images will have full opacity when loaded since they are all jpegs.
I guess you could think of it as a kind of watermark, although it would be procedural.
You can use Imagick::setImageOpacity to set the opacity of the watermark first.
Then, use Imagick::compositeImage to apply the watermark image on top of your base.

PHP image resizing and saving as png with transparent background

I'm working on a script for which I need to do the following:
User uploads an image (JPG, PNG, GIF). PHP resizes it, increases the canvas size, and saves the images as a PNG file on the server.
Now I found this tutorial to learn how to do that:
http://www.webxpert.ro/andrei/2009/01/08/thumbnail-generation-with-php-tutorial/
However, this will create a PNG file with a white background. Is it possible to have PHP generate it with a transparent background?
Thanks!
The tutorial breaks it down into basic steps. Just find the step that puts a background colour in there, and put a transparent colour instead. You will need imagecolorallocatealpha and an alpha value of 127.

User name as Watermark

Ok, I searched the internet and stackoverflow but I just can't seem to find an answer for my problem.
I need to watermark images uploaded by users dynamically, but I don't want just text applied on an image. I need a real watermark like this:
The only way I can achieve this effect is by using Photoshop, adding shadow and decreasing the filling to 0%. But if my site is visited by 200 users who upload their images, I just can't make for everyone of them a new PNG file with their user name. That's why I'm looking for a dynamic solution for this problem.
I already found classes how to add a png file as a watermark to images, but like I said before this won't work if my site is visited by a lot of users.
I hope someone knows a way how to solve this and get the same effect on images dynamically.
Thank you very much.
The documentation of the ImageMagick image processing library includes such a transparent watermark example. Even if you would like to use GD instead of ImageMagick, it might give you an idea of how to do it.
You can use imageMagick to do this with PHP. Do some Googling for PHP imagemagick watermarking, this thread may help some:
http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=17067
You essentially want to make a PNG file of your watermark. The PNG will allow for alpha transparency and you can get your drop shadow effect etc.
This will then be applied to your JPG image, and a final watermarked JPG image will be made with your PNG added on top of it.
Should work.
The other answers here are great answers, but I wanted to throw in an alternative.
You can dynamically build scripts for the GIMP to execute, which gives you tons of flexibility. This is way overkill for a simple watermark, but if you needed to do some more complex image processing, it is definitely an option. CoolText.com is an example of a website that does this.
The same approach should work in Photoshop as well. In fact, you could probably instantiate Photoshop's COM interface with PHP.
Again, I don't recommend this for basic watermarking... just if you need more functions than what is provided with ImageMagick/GD.
To the other answers I will add that you should not be generating the image on the fly. If the watermark is by username, generate the watermark file once when the user registers for your site (or changes their username), then use that file as an overlay for the uploaded images. This will save a lot of CPU time.
Use the following command:
magick convert input.jpg ( -size 960x640 xc:none -font microsoft-new-tai-lue -pointsize 90 -fill black -annotate +120+370 Watermark -blur 0x4 -fill none -annotate +125+365 Watermark ) -flatten output.png

Set background color for GD Lib imagerotate and imagecopy

I'm using PHP 5 GD Lib to do some graphic manipulations, and I can't find a way to get around the black background that shows up when you rotate an image or copy an image to another (larger) image.
Assuming that I am working with a JPEG file, which of course is not transparent, how can I rotate the image using GD Lib and end up with a white background / canvas, rather than black background?
This comment is from the PHP documentation and credit goes to "weareexit at yahoo dot co dot uk"
"If you want to place an image on a larger canvas you've previously created with imagecreatetruecolor(), but you don't want the default black background to surround it: use imagefill() AFTER imagecopyresampled()."

Categories