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

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.

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 imagick paint all non transparent pixels one color

I need to convert all non transparent pixels to one color (e.g. black).
I can only use the methods exposed by the php imagick extension http://php.net/manual/en/class.imagick.php
as exec() is blocked on the server.
what would be the quickest route to achieve this?
In the end I created a black pseudo image and copied the opacity from the original image to the new pseudo image

php imagick change all non-transparent pixels to a certain color

I am wanting to do the following with imagick via php. My ultimate goal is to create an "outline shadow" of an uploaded image. Then make the image itself transparent while keeping the shadow.
Is this how it could work?
User uploads and image with transparent background.
Use imagick to covert all non transparent pixels to a certain color #cccccc for example.
Use imagick to add a drop shadow.
Remove #cccccc pixels while keeping shadow
You can create an outline simply by creating a copy of non-transparent pixels, flood-filling them, fuzzing them a bit and then adding this as background.
There is a comprehensive explanation on how to achieve this and how it works in the ImageMagick forums

How can I add a background to my png image?

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.

Best way to scale an image if it's too small like pinterest does?

How does pinterest rescales their images? Even if you upload a 16x16 icon, it will rescale it but make it look smooth.. Does anyone know how they how their resize mechanism works?
The example you provided looks like they simply set the height of the image to be the height of the container, i.e. in the height attribute of the <img> tag. I wouldn't say it looks smooth at all, plus the aspect ratio is obviously way off.
But if you want to resize images in PHP yourself, there are several image libraries like GD. Take a look at the imagecopyresampled function (there is some example code on that page) or search for some tutorials.

Categories