How do I rotate an animated gif using PHP? - php

Is there any code out there that will allow me to rotate an animated gif 180 degrees while still maintaining the animation?
I tried using the standard imagerotate function (changing the appropriate things from jpeg to gif), but it still only outputs the first frame.
I also found https://stackoverflow.com/a/9356895/462158, but I would like to find something other than ImageMagick if at all possible.
Thanks!

You can either use ImageMagick rotateImage, as suggested in the comments
OR
Extract frames from the gif
Process individual frames the way you like (imagerotate in your case..)
Reconstruct animation
To extract frames from gif image (and reconstruct the animation), you can use gifsicle for example.
The first way is simpler, so first try that, but in case it does not fit the need somehow, processing manually is always an option

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:

How to resize gif with standard php functions without losing the animation

I have a picture uploading PHP script, which handles png, jpg and jpeg images well. The problem is, that since I am using the combo of imagealphablending, imagesavealpha, imagecreatetruecolor and imagecopyresampled to resize the picture, the gif animation is lost. I am using imagecreatefromgif to load the gif into memory.
I know one can use ImageMagick to achieve the desired result, but I wonder whether there is a solution with a combo of standard php functions to resize the gif without losing the animation. I there such a solution, or should I start to use a library?
It is possible to resize an animated GIF using GD (what you call "standard PHP").
You need to split the image into individual frames, resize them and then put everything back together.
You can see a more detailed explanation here.
However, I would really suggest you to use imagick, since it's easy to install and much easier to use for advanced tasks like this.
EDIT This is the relevant part from the answer I linked above:
If you don't have ImageMagick access, you should be able to use a
combination of the following steps to resize an animated gif (assuming
you have GD access):
Detect if the image is an animated gif: https://stackoverflow.com/questions/280658/can-i-detect-animated-gifs-using-php-and-gd
(top answer)
Split the animated gif into individual frames: [http://www.phpclasses.org/package/3234-PHP-Split-GIF-animations-into-multiple-images.html][2]
Resize the individual frames: [http://www.akemapa.com/2008/07/10/php-gd-resize-transparent-image-png-gif/][3]
Recomposite the frames into an animated gif again: [http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html][4]

Detecting animated GIF on the fly using PHP/CodeIgniter

Is there any way to check if an image URL returns an animated or not GIF?
I need to save image URLs and then show the images on my site, so I would like to allow GIF extension, but I need to avoid animated GIFs. Is it possible to detect that on the fly with out downloading the image?
Check out this comment to the documentation for function imagecreatefromgif (on php.net). You still need to first download the image file even if you are just going to link to it later.
I think, ImageMagick really will be your best chance:
identify -format %n posible_animation.gif
Please check the forum post How to identify animated image format for more information.

Resizing animated GIF with GIFDecoder.class.php will cause transparency problems

I need to resize animated GIF in PHP without ImageMagick. I am using the latest GIFDecoder and GIFEncoder classes from PHPclasses. When I extract each frame from the GIF, I get blotches of transparent areas on all the frames but the first one, even though the GIF is not transparent at all. Even putting them back together does not work. I have tried other files but still have the same problem.
The original
How it turned out
Individual frames
If there are other solutions to resizing animated GIF please tell me, too. Thanks!
I would recomment using ImageMagick. It's way faster and more powerfull than any custom coded PHP class.
Please take a look at this thread. The top answer even offers an alternative solution, if you have no access to ImageMagick on your server.

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