Sybio / ImageWorkshop

ImageWorkshop is a PHP5.3+ library that helps you to manage images based on GD library
http://phpimageworkshop.com/
Other
861 stars 189 forks source link

Cannot add watermark to GIF images #49

Open russellseymour opened 9 years ago

russellseymour commented 9 years ago

Hello,

I have been going through the tutorial about adding watermarks and I seem to have found a problem.

When I attempt to add a watermark to a GIF image I just the text layer with the text on it and no image below that layer. At first I debugged my code but could not find a problem so then I tested with a JPG image and it worked.

So it appears that adding a text layer to a GIF image does not work. Is there something I am missing? If i do not attempt to add the text layer and just save the image to a different filename then I can see the GIF image properly so it only when the layer is added to I get this issue.

Thanks, Russell

Sybio commented 9 years ago

Hi @russellseymour, a GIF is a compilation of multiple images.

GD doesn't manage GIF (in fact GD only use the first image of a GIF), but I created a class (https://github.com/Sybio/GifFrameExtractor) to recover all images of a GIF that you can watermarked separately and another class (https://github.com/Sybio/GifCreator) to rebuild the GIF with watermarked frames.

Please note it can rarely display graphic bug depending on the GIF encoding...

Find the tutorial here : http://phpimageworkshop.com/tutorial/5/manage-animated-gif-with-imageworkshop.html

russellseymour commented 9 years ago

Hello @Sybio,

Thanks very much for this.

I have now refactored by code to use these two classes when working with GIF files. However the class throws an error when I attempt to extract the frames from the GIF as it states that it is not animated.

This is the case, there is only one image in the GIF file, but I had assumed that there would be at least one frame in there for it to work with.

So although I have got a better understanding of how it should work I am still not able to add a watermark to the gif image.

Thanks, Russell

Sybio commented 9 years ago

Hi @russellseymour

If this is a one frame GIF, it should work without the 2 additionnal libs. This is perhaps a bug...

Can I have a look to your original code and can you share me images you're using ?

russellseymour commented 9 years ago

@Sybio,

Yep no problem, and thanks very much. The code that I am using is as follows:


# Load the libraries
require 'vendor/autoload.php';

# Add the lirabries to use
 use PHPImageWorkshop\ImageWorkshop;
use PHPImageWorkshop\Core\ImageWorkshopLayer;

function getAngle($width, $height) {

    # workout the hypotenuse
    $hypotenuse = sqrt(pow($width, 2) + pow($height, 2));

    # determine the angle at the bottom right
    $br_angle = acos($width / $hypotenuse) + 180 / pi();

    # return the angle to the calling function
    return round(90 - $br_angle);
}

# set the path for the orignal image
$path = sprintf("%s/original/1.010371.gif", __DIR__);
printf("Original Image Path:\t%s\n", $path);

# Set the path for the font file
$font_path = sprintf("%s/font/arial.ttf", __DIR__);
printf("Font path:\t%s\n", $font_path);

# Set the text for the watermark
$watermark_text = "Russell";
printf("Watermark Text:\t%s\n", $watermark_text);

# Determine the path for saving the modified image
$save_path = sprintf("%s/generated/%s", __DIR__, basename($path));
printf("Modified image path:\t%s\n", $save_path);

# Initialise the layers for the image
# get the image layer, which is the original image
$original_layer = ImageWorkshop::initFromPath($path);

# Determine the angle of the text
$angle = getAngle($original_layer -> getWidth(), $original_layer -> getHeight());
printf("Angle of text:\t%s\n\n", $angle);

# Build the text layer which will be the watermark
printf("Creating watermark layer\n");
$watermark_layer = ImageWorkshop::initTextLayer($watermark_text, $font_path, 22, '000000', $angle);

# set the opacity of the text
printf("Setting opacity\n");
$watermark_layer -> opacity(60);

# Add the watermark layer to the original layer
printf("Adding watermark to image\n");
$original_layer -> addLayer(1, $watermark_layer, 0, 0, 'MM');

# Save the modified image
printf("Saving image\n");
$original_layer -> save(dirname($save_path), basename($save_path));

The image I am working with is:

1 010371

If I run the above code with the image above, I get the following result:

1 010371

As you can see the original image is not there. I have played around the layer number settings but it has not made a difference. Hopefully you can help me find the error.

thanks again, Russell

russellseymour commented 9 years ago

@Sybio,

Have you had a chance to look at this at all?

Thanks, Russell