I need to convert animated GIF to static in PHP. I mean e.g. use its first frame. Any idea how to do that ?
"Stripping" the GIF of animation can be done by converting it to another format and then back again. PNG is a good candidate for this "other format", since it is non-lossy, unlike JPEG. Using PHPs GD functions, and outputting a PNG instead of a GIF:
header('Content-type: image/png');
imagepng(imagecreatefromgif($file));
This might work (haven't tested) if PHP/GD doesn't support animated GIFs (I don't think it does); and it will output the image in GIF format, unlike the above snippet:
header('Content-type: image/gif');
imagegif(imagecreatefromgif($file));
If that won't work, and output in GIF format is essential, this will:
$img1 = imagecreatefromgif($file);
$size = getimagesize($img1);
$img2 = imagecreatetruecolor($size[0], $size[1]);
imagecopy($img2, $img1, 0, 0, 0, 0, $size[0], $size[1]);
header('Content-type: image/gif');
imagegif($img2);
Take a look at http://php.net/manual/en/function.imagecreatefromgif.php
Check the code snippet from max lloyd.
The best way I could think (not very cute one) is to convert the gif to png/jpeg and then turn it to gif again, :P
try this for converting ;)
http://gallery.menalto.com/node/13206
hope this helps you
yes, you can try the gd library for this
http://php.net/manual/en/book.image.php
take a look into imagejpeg() function
Related
My simple goal was to compress PNG files with the following code:
$image = imagecreatefrompng("test.png");
imagepng($image, "result.png", 9, PNG_ALL_FILTERS);
I've also tried with zero and default compression, with the same results.
Original file is like this:
But the result on some (not all) images was like this:
The only way, I can produce good results is with this additional code:
imagealphablending($image, false);
imagesavealpha($image, true);
Why does this simple image manipulation produce this kind of artefacts? And by saving full alpha information, do I get into troubles with some browsers or unneeded filesize?
Is there any other foolproof solution, which would losslessly compress png files?
I am working on a script which will convert jpg to gif. The problem I am facing is that the output of imagegif is with bad quality and some colors change. ( black )
Here are to samples. One in JPG and the other in GIF.
http://oi62.tinypic.com/atx1j.jpg [JPG]
http://oi62.tinypic.com/oiyscy.jpg [GIF]
As you can see the colors of the GIF image has changed alitle.
I am using the following code
$img = imagecreatefromstring(base64_decode($image));
imagegif($img, "output.gif");
How can I improve the quality of the gif image?
Here's how you could achieve slightly better quality:
<?php
// load image
$image = imagecreatefromstring(file_get_contents('http://oi62.tinypic.com/atx1j.jpg'));
// create a true color image of the same size
$image2 = imagecreatetruecolor(imagesx($image), imagesy($image));
// copy the original gif image on to the true color image
imagecopy($image2, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
// output image
header("Content-Type: image/gif");
imagegif($image2);
imagedestroy($image);
imagedestroy($image2);
?>
Source Image
PHP Generated GIF Image
As you can see, it's slightly better. Very close to orignal. But you are still limited to 256 colors in GIF =[
GIF is inherently worse in quality than JPEG, this is because GIF only has 256 colours available to use, while JPEG has 4,294,967,296 colours...
If you have fewer than 256 colours in your image, GIF is actually higher quality than JPEG, since it doesn't do any compression, while jpeg compresses.
This has nothing to do with PHP: The JPEG format is a true color (or grayscale) format, while the GIF format is a palette format, with max palette size way below true color.
You need to reconsider your conversion process.
I use the following combination of functions
$img_r = imagecreatefrompng($src);
$dst_1 = imagecreate( $targ_w_1, $targ_h_1 );
imagecopyresampled($dst_1,$img_r,0,0,0,0,$targ_w_1,$targ_h_1,$size_test[0],$size_test[1]);
imagepng($dst_1, $final_source_1,9);
Final result comes with very low quality, as I understand imagepng max quality is 9. You cannot write 100 there. But still quality is very bad. maybe I use wrong functions to manipulate with image ? Any suggestions ?
original 220x220 image
resized image to size 120x120
resized image with the same size 220x220
SOLVED
Look in the manual. It has this to say on imagepng()'s $quality parameter:
Compression level: from 0 (no compression) to 9.
So 9 seems to be the worst quality level. Try a lower setting.
As far as I know, there is no such thing as a quality setting in the PNG-format, nor in the underlying c-library. There is compression, but since PNG is a loss-less format, compressing the image does not degenerate the quality.
The compression setting of 9 gives the best compression (=smallest file size).
The issue you run into likely is that your destination image is created with imagecreate(); a paletted image.
You are more likely looking for imagecreatetruecolor()
Problem was with alpha . After I read Jaccos comment I went to google and found this
imagealphablending($dst_1, false);
imagesavealpha($dst_1,true);
$transparent = imagecolorallocatealpha($dst_1, 255, 255, 255, 127);
imagefilledrectangle($dst_1, 0, 0,$targ_w_1, $targ_h_1, $transparent);
This peace of code must be put right after imagecreatetruecolor ad everything will be just fine :)
$im = ImageCreateFromString(file_get_contents($source_file));
ImageFilter($im, IMG_FILTER_GRAYSCALE);
any idea what i could do, to properly grayscale gifs and pngs with transperancy? This snippet actually works good, it transforms jpgs and pngs to grayscale. However gifs are a little bit "buggy" - they don't always work, it depends on the image. Sometimes there are a few pale colors left in them. Moreover this snippet doesn't work with alpha-channels. If i convert a gif or a png with transparancy the transparent parts always get blackened.
Of course im querying the image-type and after "grayscaling" it, i'll set the proper type again.
Have you any ideas?
This code should preserve the alpha, but it's slower than imagefilter:
$im = ImageCreateFromString(file_get_contents($source_file));
$width=imagesx();
$height=imagesy();
for($x=0;$x<$width;$x++)
for($y=0;$y<$height;$y++)
{
$rgb=imagecolorsforindex($im,imagecolorat($im,$x,$y));
$average=ceil(($rgb["red"]+$rgb["green"]+$rgb["blue"])/3);
imagesetpixel($im,$x,$y,imagecolorallocatealpha($im,$average,$average,$average,$rgb['alpha']));
}
If you still have problems try to write this after the image creation (before the $width=..):
imagesavealpha($im,true);
For pngs a simple call to imagesavealpha() solves the problem of black pixels on the alpha channel, complete code:
$im = ImageCreateFromString(file_get_contents($source))
imagefilter($im, IMG_FILTER_GRAYSCALE);
imagesavealpha($im,true);
imagepng( $im, $output );
I want to be able to trim images, many of which are very long vertically...anywhere from 2000 to 4000px, always at 800. So only getting the top part of the image. I then want to output this to a page/report with PHP, without storring the resultant trimmed image.
Is $imagepng->trim the best way to do this?
You'd do something like this:
$srcName = 'source.png';
$info = getimageinfo($srcName);
$src = imagecreatefrompng($srcName);
// Create a new image up to 800px tall
$dest = imagecreate($info[0], min($info[1], 800));
imagecopy($dest, $src, 0, 0, 0, 0, $info[0], min($info[1], 800));
// Output
header('Content-type: image/png');
imagepng($dest);
GD is what imagepng uses and it's the most widely-supported way of doing image manipulation in PHP, so it's a pretty safe bet, especially if you are looking to deploy your code on servers you don't control.
An alternative would be to look at ImageMagick, though I find GD is a little faster in most cases.