No transparency with Imagick polaroid effect - php

I use the below code to create polaroid effect, but its really distracted without any transparent effect around.
<?php
/* Create the object */
$image = new Imagick('wood.png');
/* Set the opacity */
$image->polaroidImage(new ImagickDraw(), 25);
/* output the image */
header('Content-type: image/png');
echo $image;
?>
I get result like http://photoapp.biz/polaroid/test.php
Orginal image is http://photoapp.biz/polaroid/wood.png
What will be the problem? This occurs in almost all 10 images I've tried.
Example:

Correct me if I'm wrong but you are really asking about anti aliasing, aren't you?
http://www.imagemagick.org/Usage/antialiasing/

Transparency is set with Imagick::setImageOpacity
$image->setImageOpacity(0.7);
Besides maybe you need to work with another image format:
That said, some web browsers however do NOT display transparent "PNG"
images correctly (most notably Microsoft Internet Explorer v6, though
IE v7 does). Because of this I generally prefer to use JPEG and GIF
image formats, and only use PNG when generating images with
semi-transparent pixels, or requiring a exact colors for later
examples.
Source: http://www.imagemagick.org/Usage/#PNG
EDIT 1
try
$image->setBackgroundColor(new ImagickPixel('transparent'));

Try this function http://php.net/manual/en/function.imageantialias.php.
What it does is:
Just be aware that IMAGIC can be compiled in many different ways and you might have different effects across different environments.

Related

How to generate images from PDF files in php?

I am using Imagick to product images from massive pdf files. I also want those images with RGB or sRGB color mode so Internet Explorer can display the images correctly.
I have tried
$im = new imagick($fileName.'[0]');
//$im->setImageColorspace(Imagick::COLORSPACE_SRGB); //try this already
// $im->setImageColorSpace(1); //try this already
$im->setResolution(300,300);
$im->setImageFormat('jpeg');
$im->writeImage($imageFile);
$im->clear();
$im->destroy();
I did get images but the color is way off with setImageColorspace and setImageColorSpace methods. (ex: color is inverted.)
If I comment out those methods, the images look right but some of them are not RGB mode and create problems in Internet Explorer.
I really need the RGB color mode on the images. Are there anyways to do it? Thanks so much!
You seem to encounter a problem with CMYK pdfs. Have you tried converting them to PNG? PNG -contrary to jpeg - only encodes RGB so the images will in any case be in the correct colorspace.
You might also want to have a look at ghostscript (the engine behind imagemagicks PDF conversion) and it's --UseCIE switch.
I wrote a php-wrapper to ghostscript which you can find at github that you might find usefull when you want to use ghostscript.

PHP Imagick - convert image to greyscale (very bad result)

I was doing some image editing with PHP, since GD provides less functionalities, I switched to Imagick.
One of the processes is to greyscale images. Everything went fine (locally on Windows 7, Imagick 2.2.1-dev 6.5.8-7 Q16) till I uploaded the script to my web hosting server (Linux, Imagick 3.0.1, 6.2.8, 2010-10-20, Q16).
I'v tried to change the quality, but it didn't improve anything.
$img->setImageCompression(imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(100);
Here is the results from GD, Imagick and Photoshop
I believe something's wrong with version 3.0.1. Can someone please confirm that?
Q1: Is there an alternative way to convert an image to greyscale with Imagick?
Q2: Is it possible to convert a GD resource to Imagick? So I can use imagefilter($img, IMG_FILTER_GRAYSCALE); to get the correct result and then output with Imagick.
ps: For Q2, you might suggest me to just use GD to process the image. But the problem is that imagejpeg() cannot save images with resolution preserved. and that is actually the reason I switched to Imagick.
This is my preferred way to make a B&W photo in php/imagick: $im = $im->fxImage('intensity');
That applies a function to the image, where intensity is equal to 0.299*red+0.587*green+0.114*blue.
That formula is based on how our eyes are more sensitive to different colours, and as such the difference between that and a "flat" grayscale image really is night and day.
More details here:
http://php.net/manual/en/imagick.fximage.php
http://www.imagemagick.org/script/fx.php
function ImagickToGD($imagick){
$tmpfile = tmpfile();
$imagick->writeImage($tmpfile);
return imagecreatefromstring(file_get_contents($tmpfile));
}
Note that this function does not do any cleanup (except the temp file, which PHP cleans automatically).
So, for example, your code should look like:
$img = new Imagick();
// ...
$gd = ImagickToGD($img);
unset($img); // destroy imagick
imagefilter($gd, IMG_FILTER_GRAYSCALE);
imagejpeg($gd, $target_name, 100);
imagedestroy($gd);
Also, I did not understand the part about "preserving resolution". There is nothing in these operations relating to resolution. My guess is you meant compression? If you want full quality (ie, no compression) simply use 100 as compression value (as I did).
This results in maintaining the existing quality, since opening an image of 70% quality and saving it back with 70% quality actually decreases the final quality by 49% (70% of 70%).
function GDToImagickTo($gd){
$tmpfile = tmpfile();
imagepng($tmpfile); // Png is our best image deal:
// lossless compression, transparency etc..
$imagick = new Imagick()
$imagick->readImage($tmpfile);
return $imagick;
}
Refer this website and check out the image Magick operators found here www.rubblewebs.co.uk/imagemagick/
Also go with www.fmwconcepts.com/imagemagick/ you will find some examples out here...
You can use the image class what you prefer and then use the method readImageBlob to send it to the imagick http://www.php.net/manual/en/imagick.readimageblob.php

Favicon to PNG in PHP

I need a PHP script to convert favicons to PNGs while keeping their original dimensions.
I know Google has it's secret icon converter - http://www.google.com/s2/favicons?domain=http://facebook.com/ but this converts favicons to 16x16 even if they they were originally larger. So basically I need this, minus the shrinking effect.
I've also seen this - http://www.controlstyle.com/articles/programming/text/php-favicon/ but I couldn't get it to work after hours of messing around with it.
Basically I am trying to automatically grab the icon for a link that will be as large as possible - automatically 48x48 png based on a URL would be the perfect scenario, but I don't know of any humanly possible way to do this given that no websites happen to keep a 48x48 icon in a publicly accessible spot.
Does anybody know of a script/service or have a suggestion? Thanks!
So I ended up using a class called FloIcon that could convert BMPs to ICO. I should note that it's always important to check the file type of an icon and not assume that .ico means bmp because some sites (like Facebook) were actually PNG).
#goker.cebeci Your service looks awesome! The main thing is that I needed my icons to be the maximum size when possible, so I just wrote my own script.
Here is a function to convert from bmp(ico) to png
http://us3.php.net/manual/en/function.imagecreate.php#53879
Download the ico to your server (file_get_contents or other methods) usually is favicon.ico at the base url, or scrape the html code for the <link rel="shortcut icon" href="ico_url_here" type="image/x-icon" /> element and extract the href
use the function from the link above to convert to the png
use the GD functions to open and resize
$image = imagecreatefrompng($filename);
$resized_image = imagecreatetruecolor($NewWidth, $NewHeight);
imagecopyresampled($resized_image, $image, 0, 0, 0, 0, $NewWidth, $NewHeight, $OriginalWidth, $OriginalHeight);
4 Save the file (imagepng or similar)
I used Imagemagick on my favicon to PNG convertor web service project.
convert "favicon.ico" -thumbnail 16x16 -alpha on -background none -flatten "favicon.png"
Some websites' favicons have scene and their sizes are bigger than 16x16 pixels
eg: http://blogger.com/favicon.ico
http://www.google.com/s2/favicons?domain=http://facebook.com/ does not work properly. So, I developed a web service for this.
If you want to try my web service you can go this way
http://geticon.org/of/http://facebook.com/ or this way
http://geticon.org/of/facebook.com
Code at http://www.controlstyle.com/articles/programming/text/php-favicon/ has small error:
You need to change $entry['swBitCount'] to $entry['wBitCount']. When I have made that changing all work right
imagecopyresized - the docs contains the example as well
The above require compiled with option --with-gd
I assume you did not aware of imagick extension as well
etc:
all possible image processing extensions/functions
Im using here: http://plugins.trac.wordpress.org/browser/wp-favicons/trunk/plugins/filters/convert_to_png.php a lib from here: http://www.tom-reitz.com/2009/02/09/ico-images-in-facebook-profile-boxes/
(I did not want to save the ico's to disk first)
The only problem with the lib is that it sometimes fails on the XOR e.g. on this favicon: http://www.slatch.com/
So that is something I need to fix in it but furthermore it worked great for thousands of icons.

How to compress images in CodeIgniter, yet do not change their dimensions?

I have a site where users can upload images. I process these images directly and resize them into 5 additional formats using the CodeIgniter Image Manipulation class. I do this quite efficiently as follow:
I always resize from the previous format, instead of from the original
I resize using an image quality of 90% which about halves the file size of jpegs
The above way of doing things I implemented after advise I got from another question I asked. My test case is a 1.6MB JPEG in RGB mode with a high resolution of 3872 x 2592. For that image, which is kind of borderline case, the resize process in total takes about 2 secs, which is acceptable to me.
Now, only one challenge remains. I want the original file to be compressed using that 90% quality but without resizing it. The idea being that that file too will take half the file size. I figured I could simply resize it to its' current dimensions, but that doesn't seem to do anything to the file or its size. Here's my code, somewhat simplified:
$sourceimage = "test.jpg";
$resize_settings['image_library'] = 'gd2';
$resize_settings['source_image'] = $sourceimage;
$resize_settings['maintain_ratio'] = false;
$resize_settings['quality'] = '90%';
$this->load->library('image_lib', $resize_settings);
$resize_settings['width'] = $imagefile['width'];
$resize_settings['height'] = $imagefile['height'];
$resize_settings['new_image'] = $filename;
$this->image_lib->initialize($resize_settings);
$this->image_lib->resize();
The above code works fine for all formats except the original. I tried debugging into the CI class to see why nothing happens and I noticed that the script detects that the dimensions did not change. Next, it simply makes a copy of that file without processing it at all. I commented that piece of code to force it to resize but now still nothing happens.
Does anybody know how to compress an image (any image, not just jpegs) to 90% using the CI class without changing the dimensions?
I guess you could do something like this:
$original_size = getimagesize('/path/to/original.jpg');
And then set the following options like this:
$resize_settings['width'] = $original_size[0];
$resize_settings['height'] = $original_size[1];
Ok, so that doesn't work due to CI trying to be smart, the way I see it you've three possible options:
Rotate the Image by 360ยบ
Watermark the Image (with a 1x1 Transparent Image)
Do It Yourself
The DIY approach is really simple, I know you don't want to use "custom" functions but take a look:
ImageJPEG(ImageCreateFromString(file_get_contents('/path/to/original.jpg')), '/where/to/save/optimized.jpg', 90);
As you can see, it's even more simpler than using CI.
PS: The snippet above can open any type of image (GIF, PNG and JPEG) and it always saves the image as JPEG with 90% of quality, I believe this is what you're trying to archive.

How do I set partial transparancy in GD with PHP?

I am attempting to make a dynamic image with PHP, and I can't find out how to set partial transparency. It is very easy to make things either solid or fully transparent, but I have been unable to do this.
Assuming you're creating your image with the imagecreatetruecolor function and that you're outputting the image as a PNG file....
You need to call the following methods to specify that the image must be generated as a 24-bit PNG before calling imagepng:
imagesavealpha($im, true);
Note that Internet Explorer 6 doesn't support 24-bit PNG files without the use of a CSS filter applied to the img tag.
Check out the documentation for imagecolorallicatealpha() There they create a transparent color circle.

Categories