PHP Convert JPEG or PDF to PNG with transparency - php

Im trying to convert a PDF or JPG file into a png with specific colors being transparent.
For example. I have a file with white background and black text and would like to convert the white area to be transparent on the png
I have tried using imagemagick :
exec('convert -density 144 ' . $infile.' -transparent "#FFFFFF" ' .$png_file);
and this seems to work at times and not other times for some reason
also the edges of the black come out kind of jagged. Does anyone know of a way to make the edges smoother. or any other function in php to use to create the transparency.
Also the quality needs to be perfect as well as sometimes when the dimensions of the file are small the output png is very low res and i need to keep it at the same dimensions so i cant really use the -density function

You can overcome the jagged edges using the fuzz factor option -fuzz, but as the usage manual explains, the results may or may not fit your needs.
Background removal is also discussed at length in the usage manual, describing several techniques besides using the -transparent option.
Without more information, I don't know why your conversion code works sometimes and not at others, but I don't think you need the -density option for JPG conversion. That might be the problem.

Related

Save PNG as PNG-8 with Floyd-Steinberg 2 dithering like GIMP in PHP

In GIMP I can save an image for Web with these properties:
PNG-8
256 colors palette
Dither: Floyd-Steinberg / Floyd-Steinberg 2 / positioned
With these options I can reduce upto about 70% of the size of the image file without evident quality loss.
How can I get the same result with PHP?
The resulting image must preserve transparency.
I know almost nothing about image compression, encoding, palette, etc.
Edit: I posted a new question with a PHP script to do that, so I'll close this question or I'll answer it.
PHP wrong result for imagetruecolortopalette with PNG with transparency

PHP ImageMagick png to eps conversion with transparent background

I have a png image with transparent background and i tried to convert that to an eps file using convert command of imagemagick,and got eps image with white background.
I want eps image with transparent background.I have a php application for designing images for tshirt printing,So image transparency is must.
Also image quality of output eps image is pretty bad and image size is really big comparing to input size.
I had tried command
convert test2.png -resize 1024x1024 -density 500 -units pixelsperinch -quality 100 -compress none -alpha set -flatten -background none -depth 20 test2_eps.eps
Iam using system function of php to execute this command.
Is there any option to maintain transparency of input image? Is it white by default? How can we improve output eps image quality?
PostScript (and therefore EPS) doesn't have a concept of transparency (with some exceptions), so you can't readily convert an image with an alpha channel to an EPS.
If the 'background' is 100% transparent it is possible to create a similar effect in PostScript using masked images (that's 2 images, where the second is a mask that determines where the 1st image draws). That's a rarely used feature, and requires a level 3 PostScript interpreter. Most EPS export functionality doesn't go above level 2.
As to whether ImageMagick can create such an EPS, I have no clue, but I wouldn't be at all surprised to find it cannot. You may have to use something like Photoshop.
Regarding the image size; PNG is a compressed format, and you have specified
-compress none
So its hardly surprising that the output is larger!
Its essentially impossible to comment on your quality problem, partly because you haven't really said exactly what it is that you see as a problem. 'Quality' is rather vague, perhaps you could post an example.
If your EPS has a thumbnail preview do not be fooled into thinking this is what the EPS contains, its a low resolution bitmap preview only and its there for the benefit of EPS consumers that can't interpret the PostScript content. So that they can preview the result when you place the EPS in the final document.

Bad CMYK to RGB conversion with GD

I am doing the standard image optimise / resize technique with PHP and GD, basically:
imagecreatefromjpeg()
imagecreatetruecolor()
imagecopyresampled()
imagejpeg()
However people are complaining that there CYMK logos are coming up different colours after being uploaded. I know this is because GD is doing a conversion to RGB but i am wondering if there is a way to improve this?
As these are often peoples logos, they rightly get protective over the colours used and want them to be right.
I know there are some good tools to convert single colours online but i want something that can be added to my class file that converts all the CMYK colours in an image to a more accurate version of the CMYK equivalent.
As a quick example, a pic uploaded without conversion and then selected with a colour picker extension in chrome gives me this HEX code: #992A78. Having run it through the above scripts, it converts it to this one: #9000F6;
Anyone got any advice for me? Thanks
GD doesn't support color management at all, which explains the color shift, so there is probably no feasible workaround there. You might want to do some quick tests using ImageMagick, which seems to support various color profiles.

PHP GD Image Watermark Quality

I'am using PHP GD to copy an image (watermark) on another image.
Unfortunately the quality of the watermark is terrible.
I'm using quality 100% as the attribute, but it doesn't help.
Have you guys know any good way to increase the quality?
Regards.
A 90% quality should give you the exact same results and decrease the file size by half (on JPEG, OFC).
Try using ImageCopyResampled() instead of ImageCopyResize(), other than that I don't think you can do anything else with GD alone, maybe Imagick has some other tricks.
Are you using any transparency, or is it just a solid square. What exactly looks bad about the quality? The edges, the whole thing?
I like to use a 24bit transparent PNG for the watermark, and imagemagick to do the overlay, you get a lot of control over the final product that way.
Lots of possibilities for watermark with imagemagick:
http://www.imagemagick.org/Usage/annotating/

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

Categories