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.
Related
I have a straightforward PHP script (php version 7+) that I run to resize images to a smaller dimension around 1200px wide ...
I use imagecreatefromjpeg and imagecopyresampled method and the save new file with imagejpeg # 88% compression.
I and ran over 100 images through it and most images worked out great, except for a few stubborn images that results to BLACK square image.
I tried imagecopyresized and played with different new dimensions but it just didnt work. I even tried outputting the ob stream from these tests at the point of resizing instead of saving to file (no compressions applied) and i can confirm that the moment the image was resized, it is already black square.
On the images that produces black square the result is false.
I did verify mime type IMAGETYPE_JPEG of file, regardless of file extension, before running the code.
Now, I tried running the same files thru imagescale instead
$new_img = imagescale($img, $new_width, $new_height, IMG_SINC );
and that worked.
Now I prefer to use the previous method for better more crispier output so I've been trying to get it to work :(
Why is this happening? I searched stack for similar issues but they are describing a issues that completely didnt work on any images, not my case, where only a select few images had issues.
I am dealing with JPG only, but is it possible that the jpg file is just named with extension ".jpg" but actually a png, bmp, or something else? Will that cause issues?
I'm stuck, and i hope you guys can help.
Thank you
Most images resized nicely
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.
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
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.
I have a small Minecraft server where people can upload their skins. Minecraft skins are small png images. Is it possible to convert this png image to another png image via PHP (e.g. GD library)?
I have made this image to help me explain what I am trying to do:
Yes, it's possible. You'd need multiple imagecopy commands to pull out sections of the skin image and paste it into the proper spots in the "output" image.
Basic order of operations would be:
$input = imagecreatefrompng('skin.png');
$output = imagecreatetruecolor(800, 600); // whatever the dimensions should be.
imagecopy($output, $input, 0,0, 10,20, 50,60);
imagecopy(...);
...
...
The first copy command is saying "take a 50x60 section of the input image, starting at coordinates 10x20, and paste it into the destination image in the top left corner".
The actual sequence/coordinates/sizes will be up to you to figure out.
If you're not doing a 1:1 copy of the image and are doing resizing, then you'll want imagecopyresampled() instead.
Here is the PHP manual for creating images from png :
http://php.net/manual/en/function.imagecreatefrompng.php
Here is a simple tutorial :
http://www.phptutorial.info/?imagecreatefrompng
You can do this with CSS
Here is a tutorial: http://www.w3schools.com/css/css_image_sprites.asp