Grandt / PHPePub

PHP Classes for dynamically generating EPub files.
http://www.phpclasses.org/package/6115
241 stars 83 forks source link

GIF Images becomes an image with a black background and becomes a jpeg file #20

Closed leikxile closed 10 years ago

leikxile commented 10 years ago

Hi, I came across with the problem of gif images being a jpeg file. I've set isGifImagesEnabled to true. Still Gif images are becoming jpeg files so i've check the code and noticed that the $mime only has png and jpeg conditions so i added the gif condition. Please see code below, the problem is the animation is gone.

`if ($mime == "image/png") { imagealphablending($image_p, false); imagesavealpha($image_p, true);
imagealphablending($image_o, true);

                imagecopyresampled($image_p, $image_o, 0, 0, 0, 0, ($width*$ratio), ($height*$ratio), $width, $height);
                ob_start();
                imagepng($image_p, NULL, 9);
                $image = ob_get_contents();
                ob_end_clean();

                $ext = "png";
            }else if ($mime == "image/gif") {
                imagealphablending($image_p, false);
                imagesavealpha($image_p, true);  
                imagealphablending($image_o, true);

                imagecopyresampled($image_p, $image_o, 0, 0, 0, 0, ($width*$ratio), ($height*$ratio), $width, $height);
                ob_start();
                imagepng($image_p, NULL, 9);
                $image = ob_get_contents();
                ob_end_clean();

                $ext = "gif";
            } else {
                imagecopyresampled($image_p, $image_o, 0, 0, 0, 0, ($width*$ratio), ($height*$ratio), $width, $height);
                ob_start();
                imagejpeg($image_p, NULL, 80);
                $image = ob_get_contents();
                ob_end_clean();

                $mime = "image/jpeg";
                $ext = "jpg";
            }`
Grandt commented 10 years ago

I'll look at this later this evening.

Grandt commented 10 years ago

The problem is that the Gif apparently exceeds the size restriction set by the $maxImageWidth and $maxImageHeight variables.

Resizing an animation is not that easy though.

leikxile commented 10 years ago

Hi Grandt,

I tried using a smaller gif image http://www.afh.com/web/gif89a/sample2.gif But still the animation is gone this is because of the image conversion is to a png but i just made it a gif mimetype see code below:

if ($mime == "image/gif") { imagealphablending($image_p, false); imagesavealpha($image_p, true); imagealphablending($image_o, true); imagecopyresampled($image_p, $imageo, 0, 0, 0, 0, ($width$ratio), ($height_$ratio), $width, $height); ob_start(); imagepng($image_p, NULL, 9); $image = ob_get_contents(); ob_end_clean();

$ext = "gif"; }

this function is for png, I don't know what to do if it is a gif image. I do remember gifs are working in the earlier versions of your epub class so I tried copying the function in the image conversion to the present epub class but this function now converts it to a jpeg file.

On Sun, Jan 12, 2014 at 3:06 AM, Asbjørn Grandt notifications@github.comwrote:

The problem is that the Gif apparently exceeds the size restriction set by the $maxImageWidth and $maxImageHeight variables.

Resizing an animation is not that easy though.

— Reply to this email directly or view it on GitHubhttps://github.com/Grandt/PHPePub/issues/20#issuecomment-32120057 .

Grandt commented 10 years ago

Hi

I have a different solution, but I have to revert some code. I tried to convert/resize the animation, but with very bad results. But the new code correctly handles non animated gifs now. This is the section, without animation support.

        if ($ratio < 1 || empty($mime)) {
            if ($mime == "image/png" || ($this->isGifImagesEnabled

=== FALSE && $mime == "image/gif")) { $image_o = imagecreatefromstring($image); $imagep = imagecreatetruecolor($width$ratio, $height_$ratio);

                imagealphablending($image_p, false);
                imagesavealpha($image_p, true); 
                imagealphablending($image_o, true);

                imagecopyresampled($image_p, $image_o, 0, 0, 0, 0,

($width$ratio), ($height$ratio), $width, $height); ob_start(); imagepng($image_p, NULL, 9); $image = ob_get_contents(); ob_end_clean();

                imagedestroy($image_o);
                imagedestroy($image_p);

                $ext = "png";
            } else if ($this->isGifImagesEnabled !== FALSE && $mime

== "image/gif") { $image_o = imagecreatefromstring($image); $imagep = imagecreatetruecolor($width$ratio, $height_$ratio);

                imagealphablending($image_p, false);
                imagesavealpha($image_p, true); 
                imagealphablending($image_o, true);

                imagecopyresampled($image_p, $image_o, 0, 0, 0, 0,

($width$ratio), ($height$ratio), $width, $height); ob_start(); imagegif($image_p, NULL); $image = ob_get_contents(); ob_end_clean();

                imagedestroy($image_o);
                imagedestroy($image_p);

                $ext = "gif";
              } else {

                $image_o = imagecreatefromstring($image);
                $image_p = imagecreatetruecolor($width*$ratio,

$height*$ratio);

                imagecopyresampled($image_p, $image_o, 0, 0, 0, 0,

($width$ratio), ($height$ratio), $width, $height); ob_start(); imagejpeg($image_p, NULL, 80); $image = ob_get_contents(); ob_end_clean();

                imagedestroy($image_o);
                imagedestroy($image_p);

                $mime = "image/jpeg";
                $ext = "jpg";
            }

The initial check for if the gifs were to be enabled, was misplaced. Now, if they are not enabled, they'll be changed to a png, else resized as a gif. The corrections you made only needed the change from imagepng to imagegif, and the extra check.

On 13-01-2014 08:44, leikxile wrote:

Hi Grandt,

I tried using a smaller gif image http://www.afh.com/web/gif89a/sample2.gif But still the animation is gone this is because of the image conversion is to a png but i just made it a gif mimetype see code below:

if ($mime == "image/gif") { imagealphablending($image_p, false); imagesavealpha($image_p, true); imagealphablending($image_o, true); imagecopyresampled($image_p, $imageo, 0, 0, 0, 0, ($width$ratio), ($height_$ratio), $width, $height); ob_start(); imagepng($image_p, NULL, 9); $image = ob_get_contents(); ob_end_clean();

$ext = "gif"; }

this function is for png, I don't know what to do if it is a gif image. I do remember gifs are working in the earlier versions of your epub class so I tried copying the function in the image conversion to the present epub class but this function now converts it to a jpeg file.

On Sun, Jan 12, 2014 at 3:06 AM, Asbjørn Grandt notifications@github.comwrote:

The problem is that the Gif apparently exceeds the size restriction set by the $maxImageWidth and $maxImageHeight variables.

Resizing an animation is not that easy though.

— Reply to this email directly or view it on GitHubhttps://github.com/Grandt/PHPePub/issues/20#issuecomment-32120057 .

— Reply to this email directly or view it on GitHub https://github.com/Grandt/PHPePub/issues/20#issuecomment-32149982.

leikxile commented 10 years ago

Based on my research recreating gif images will remove the animations hmm... will try to research on this more. btw thanks for this fix.

On Mon, Jan 13, 2014 at 12:08 AM, Asbjørn Grandt notifications@github.comwrote:

Hi

I have a different solution, but I have to revert some code. I tried to convert/resize the animation, but with very bad results. But the new code correctly handles non animated gifs now. This is the section, without animation support.

if ($ratio < 1 || empty($mime)) { if ($mime == "image/png" || ($this->isGifImagesEnabled === FALSE && $mime == "image/gif")) { $image_o = imagecreatefromstring($image); $imagep = imagecreatetruecolor($width$ratio, $height_$ratio);

imagealphablending($image_p, false); imagesavealpha($image_p, true); imagealphablending($image_o, true);

imagecopyresampled($image_p, $imageo, 0, 0, 0, 0, ($width$ratio), ($height_$ratio), $width, $height); ob_start(); imagepng($image_p, NULL, 9); $image = ob_get_contents(); ob_end_clean();

imagedestroy($image_o); imagedestroy($image_p);

$ext = "png"; } else if ($this->isGifImagesEnabled !== FALSE && $mime == "image/gif") { $image_o = imagecreatefromstring($image); $imagep = imagecreatetruecolor($width$ratio, $height_$ratio);

imagealphablending($image_p, false); imagesavealpha($image_p, true); imagealphablending($image_o, true);

imagecopyresampled($image_p, $imageo, 0, 0, 0, 0, ($width$ratio), ($height_$ratio), $width, $height); ob_start(); imagegif($image_p, NULL); $image = ob_get_contents(); ob_end_clean();

imagedestroy($image_o); imagedestroy($image_p);

$ext = "gif"; } else {

$image_o = imagecreatefromstring($image); $imagep = imagecreatetruecolor($width$ratio, $height_$ratio);

imagecopyresampled($image_p, $imageo, 0, 0, 0, 0, ($width$ratio), ($height_$ratio), $width, $height); ob_start(); imagejpeg($image_p, NULL, 80); $image = ob_get_contents(); ob_end_clean();

imagedestroy($image_o); imagedestroy($image_p);

$mime = "image/jpeg"; $ext = "jpg"; }

The initial check for if the gifs were to be enabled, was misplaced. Now, if they are not enabled, they'll be changed to a png, else resized as a gif. The corrections you made only needed the change from imagepng to imagegif, and the extra check.

On 13-01-2014 08:44, leikxile wrote:

Hi Grandt,

I tried using a smaller gif image http://www.afh.com/web/gif89a/sample2.gif But still the animation is gone this is because of the image conversion is to a png but i just made it a gif mimetype see code below:

if ($mime == "image/gif") { imagealphablending($image_p, false); imagesavealpha($image_p, true); imagealphablending($image_o, true); imagecopyresampled($image_p, $imageo, 0, 0, 0, 0, ($width$ratio), ($height_$ratio), $width, $height); ob_start(); imagepng($image_p, NULL, 9); $image = ob_get_contents(); ob_end_clean();

$ext = "gif"; }

this function is for png, I don't know what to do if it is a gif image. I do remember gifs are working in the earlier versions of your epub class so I tried copying the function in the image conversion to the present epub class but this function now converts it to a jpeg file.

On Sun, Jan 12, 2014 at 3:06 AM, Asbjørn Grandt notifications@github.comwrote:

The problem is that the Gif apparently exceeds the size restriction set by the $maxImageWidth and $maxImageHeight variables.

Resizing an animation is not that easy though.

— Reply to this email directly or view it on GitHub<https://github.com/Grandt/PHPePub/issues/20#issuecomment-32120057

.

— Reply to this email directly or view it on GitHub https://github.com/Grandt/PHPePub/issues/20#issuecomment-32149982.

— Reply to this email directly or view it on GitHubhttps://github.com/Grandt/PHPePub/issues/20#issuecomment-32150833 .

Grandt commented 10 years ago

The problem with animated Gifs is that you have to decompose the animation, and then resize each frame individually, then recompose it again. Adding to the complexity is the "optimized" animated gifs, where a frame may not discard the previous, and just "paint" over part of it, in those you have to recalculate the offset as well when you recompose it.

leikxile commented 10 years ago

Hmm.. sorry I do not have that kind of knowledge to do that lol, though if all else fails, can I just download the file if it is image using file_put_contents? so it will not recreate the gif file?

On Mon, Jan 13, 2014 at 10:02 PM, Asbjørn Grandt notifications@github.comwrote:

The problem with animated Gifs is that you have to decompose the animation, and then resize each frame individually, then recompose it again. Adding to the complexity is the "optimized" animated gifs, where a frame may not discard the previous, and just "paint" over part of it, in those you have to recalculate the offset as well when you recompose it.

— Reply to this email directly or view it on GitHubhttps://github.com/Grandt/PHPePub/issues/20#issuecomment-32240904 .

Grandt commented 10 years ago

I didn't know either, until Sunday :)

I'll have to think on this though, as while adding the file directly is possible, if the link in the HTML isn't relative, the image may not be reachable in the ePub.

But does it still enter the resize part of the code, when the gif is small enough?

What happens if you set $book->maxImageWidth and $book->maxImageHeight to something larger than the gif size? They defaults to 768 and 1024 respectively.

On 14-01-2014 07:40, leikxile wrote:

Hmm.. sorry I do not have that kind of knowledge to do that lol, though if all else fails, can I just download the file if it is image using file_put_contents? so it will not recreate the gif file?

On Mon, Jan 13, 2014 at 10:02 PM, Asbjørn Grandt notifications@github.comwrote:

The problem with animated Gifs is that you have to decompose the animation, and then resize each frame individually, then recompose it again. Adding to the complexity is the "optimized" animated gifs, where a frame may not discard the previous, and just "paint" over part of it, in those you have to recalculate the offset as well when you recompose it.

— Reply to this email directly or view it on GitHubhttps://github.com/Grandt/PHPePub/issues/20#issuecomment-32240904 .

— Reply to this email directly or view it on GitHub https://github.com/Grandt/PHPePub/issues/20#issuecomment-32242112.

leikxile commented 10 years ago

Adding an image link in an ePub is not advisable since the device may sometimes go offline right? So the class should download the file right into the book. This could be an exception for the gif files since they are very hard to recreate.

I tried an image with a resolution of 480x300 but it failed, tried increasing $book->maxImageWidth and $book->maxImageHeight also but no luck.

On Mon, Jan 13, 2014 at 10:50 PM, Asbjørn Grandt notifications@github.comwrote:

I didn't know either, until Sunday :)

I'll have to think on this though, as while adding the file directly is possible, if the link in the HTML isn't relative, the image may not be reachable in the ePub.

But does it still enter the resize part of the code, when the gif is small enough?

What happens if you set $book->maxImageWidth and $book->maxImageHeight to something larger than the gif size? They defaults to 768 and 1024 respectively.

On 14-01-2014 07:40, leikxile wrote:

Hmm.. sorry I do not have that kind of knowledge to do that lol, though if all else fails, can I just download the file if it is image using file_put_contents? so it will not recreate the gif file?

On Mon, Jan 13, 2014 at 10:02 PM, Asbjørn Grandt notifications@github.comwrote:

The problem with animated Gifs is that you have to decompose the animation, and then resize each frame individually, then recompose it again. Adding to the complexity is the "optimized" animated gifs, where a frame may not discard the previous, and just "paint" over part of it, in those you have to recalculate the offset as well when you recompose it.

— Reply to this email directly or view it on GitHub<https://github.com/Grandt/PHPePub/issues/20#issuecomment-32240904

.

— Reply to this email directly or view it on GitHub https://github.com/Grandt/PHPePub/issues/20#issuecomment-32242112.

— Reply to this email directly or view it on GitHubhttps://github.com/Grandt/PHPePub/issues/20#issuecomment-32242464 .

Grandt commented 10 years ago

I'll have to look into this, something else is wrong then. At that size the GIF should not even be attempted to be processed, it should just be added.

leikxile commented 10 years ago

Alright, thank you moral support here :)

On Mon, Jan 13, 2014 at 11:15 PM, Asbjørn Grandt notifications@github.comwrote:

I'll have to look into this, something else is wrong then. At that size the GIF should not even be attempted to be processed, it should just be added.

— Reply to this email directly or view it on GitHubhttps://github.com/Grandt/PHPePub/issues/20#issuecomment-32243325 .

Grandt commented 10 years ago

Please note that some ePub readers will not play the animation. For instance Sony Reader for PC doesn't.

Grandt commented 10 years ago

I'll look at it some more tonight, when I get back from work. Until then, have a look at the new 3.21 branch. It seems to work without trying to resize the animated gif.

leikxile commented 10 years ago

Alright thank you!

On Mon, Jan 13, 2014 at 11:28 PM, Asbjørn Grandt notifications@github.comwrote:

I'll look at it some more tonight, when I get back from work. Until then, have a look at the new 3.21 branch. It seems to work without trying to resize the animated gif.

— Reply to this email directly or view it on GitHubhttps://github.com/Grandt/PHPePub/issues/20#issuecomment-32243821 .

leikxile commented 10 years ago

Forgot to say that the file is from an external source.

On Mon, Jan 13, 2014 at 11:31 PM, Kiel Lago kiellago@gmail.com wrote:

Alright thank you!

On Mon, Jan 13, 2014 at 11:28 PM, Asbjørn Grandt <notifications@github.com

wrote:

I'll look at it some more tonight, when I get back from work. Until then, have a look at the new 3.21 branch. It seems to work without trying to resize the animated gif.

— Reply to this email directly or view it on GitHubhttps://github.com/Grandt/PHPePub/issues/20#issuecomment-32243821 .

Grandt commented 10 years ago

Try to uncomment the echo just before the if ($ratio <1... line in 3.21. It will of course fail the script but i'd like to know whai it prints for the gif. Remember to enable error reporting as seen in the example scripts.

leikxile commented 10 years ago

Here is the output

$source: http://localhost/public_html/includes/lib_uploader/uploads/2014-01-15/1/52d5d498298db/elem-52d5d503d2d91.gif $ratio.: 1 $mime..: image/gif $width.: 480 $height: 360

On Tue, Jan 14, 2014 at 12:15 AM, Asbjørn Grandt notifications@github.comwrote:

Try to uncomment the echo just before the if ($ratio <1... line in 3.21. It will of course fail the script but i'd like to know whai it prints for the gif. Remember to enable error reporting as seen in the example scripts.

— Reply to this email directly or view it on GitHubhttps://github.com/Grandt/PHPePub/issues/20#issuecomment-32245819 .

Grandt commented 10 years ago

Then it shouldn't even have entered that resizing code. The line

    if ($ratio < 1 || empty($mime)) {

should stop it right there. If you unzip the epub file, and look at the gif there, is it still broken?

leikxile commented 10 years ago

Yup that is what i've been thinking of.. I even tried to comment the resizing part. So it downloaded the file but the animation is gone. So this means that the problem is not in the resizing part anymore right?

On Tue, Jan 14, 2014 at 1:32 AM, Asbjørn Grandt notifications@github.comwrote:

Then it shouldn't even have entered that resizing code. The line

if ($ratio < 1 || empty($mime)) {

should stop it right there.

— Reply to this email directly or view it on GitHubhttps://github.com/Grandt/PHPePub/issues/20#issuecomment-32249988 .

Grandt commented 10 years ago

Is the gif file in the epub file itself changed? Rename the epub to .zip and unpack it, the file should be in OEBPS/images/localhost/public_html/includes/lib_uploader/uploads/2014-01-15/1/52d5d498298db/elem-52d5d503d2d91.gif

As I said, some readers don't play the animation.

leikxile commented 10 years ago

Yup i've checked the file it is a gif image but tried opening it in photo viewer, the animation is gone, transparent yes, but is just a flat image.

On Tue, Jan 14, 2014 at 2:16 AM, Asbjørn Grandt notifications@github.comwrote:

Is the gif file in the epub file itself changed? Rename the epub to .zip and unpack it, the file should be in

OEBPS/images/localhost/public_html/includes/lib_uploader/uploads/2014-01-15/1/52d5d498298db/elem-52d5d503d2d91.gif

As I said, some readers don't play the animation.

— Reply to this email directly or view it on GitHubhttps://github.com/Grandt/PHPePub/issues/20#issuecomment-32252758 .

Grandt commented 10 years ago

Hi

Can I get you to send me the generated ePub file (or just a simple test file containing that gif from the link you used, as well as the original gif?

Please use the address php@grandt.com

Cheers

Grandt commented 10 years ago

Hi

EPub.Example3_2.php in the 3.21 branch loads the animated gif correctly.

leikxile commented 10 years ago

issue has been fixed thank you!