Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot add watermark to GIF images #49

Open
russellseymour opened this issue Sep 17, 2014 · 5 comments
Open

Cannot add watermark to GIF images #49

russellseymour opened this issue Sep 17, 2014 · 5 comments

Comments

@russellseymour
Copy link

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
Copy link
Owner

Sybio commented Sep 17, 2014

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
Copy link
Author

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
Copy link
Owner

Sybio commented Sep 19, 2014

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
Copy link
Author

@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
Copy link
Author

@Sybio,

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

Thanks, Russell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants