Grandt / PHPePub

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

Error converting markup with svgs #34

Open JonasDoebertin opened 9 years ago

JonasDoebertin commented 9 years ago

Generally, creating an epub from my html markup works just fine and this library is a HUGE time saver. Thank you so much for creating and maintaining it!

There is only problem I'm facing right now though: Whenever my original HTML markup contains an <img> tag linking to a SVG file, adding it to a chapter fails (with EPub::EXTERNAL_REF_ADD enabled, to embed all image files into the epub file). Adding any other image format like PNGs and JPEGs works as expected and if I disable EPub::EXTERNAL_REF_ADD it works, too. I also tried adding other SVG files which still results in the error.

Here's the exact error message I'm facing:

PHP Warning:  imagecreatefromstring(): Data is not in a recognized format in /Users/path/to/project/vendor/grandt/phpepub/src/PHPePub/Core/EPub.php on line 615

This is the markup related to the SVG I'm trying to convert:

<figure>
    <img src="http://example.com/image.svg" alt="My svg image">
</figure>

Using the latest release with composer.

Grandt commented 9 years ago

Please have a look at 4.0.5-beta1 No guarantees, I only just had time to whip that one up.

JonasDoebertin commented 9 years ago

You're so fast! Thanks a lot for your efforts!

I'll have a look this evening.

JonasDoebertin commented 9 years ago

Seems to work beautifully. Thank you so much!

Grandt commented 9 years ago

Can I get you to test one more thing while you are at it? Try to add a very large SVG (2000x2000 or more) The new code should reduce its size to the confines of the $maxImageWidth/$maxImageHeight settings in EPub.

JonasDoebertin commented 9 years ago

Hmmm, that doesn't seem to work. Tried adding a 4000x4000 pixels svg file. The svg file included within the .epub is exactly the same as the original file (except that some attributes on the main <svg> element are reordered).

Original svg:

<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg width="100%" height="100%" viewBox="0 0 4000 4000" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41421;"><g id="Ebene1"><rect x="411.445" y="269.874" width="1021.12" height="1153.57" style="fill:#50df50;"/><ellipse cx="3061.76" cy="1551.06" rx="619.986" ry="1117.49" style="fill:#d2e7e7;"/><path d="M2938.12,1540.04c0,-284.276 -230.796,-515.071 -515.071,-515.071l-1030.14,0c-284.275,0 -515.071,230.795 -515.071,515.071l0,1106.84c0,284.276 230.796,515.071 515.071,515.071l1030.14,0c284.275,0 515.071,-230.795 515.071,-515.071l0,-1106.84Z" style="fill:#ec5656;"/></g></svg>

Svg in epub file:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="0 0 4000 4000" version="1.1" xml:space="preserve" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41421;"><g id="Ebene1"><rect x="411.445" y="269.874" width="1021.12" height="1153.57" style="fill:#50df50;"/><ellipse cx="3061.76" cy="1551.06" rx="619.986" ry="1117.49" style="fill:#d2e7e7;"/><path d="M2938.12,1540.04c0,-284.276 -230.796,-515.071 -515.071,-515.071l-1030.14,0c-284.275,0 -515.071,230.795 -515.071,515.071l0,1106.84c0,284.276 230.796,515.071 515.071,515.071l1030.14,0c284.275,0 515.071,-230.795 515.071,-515.071l0,-1106.84Z" style="fill:#ec5656;"/></g></svg>
Grandt commented 9 years ago

I think I'll change that and leave the SVG's be. Setting their size changes nothing anyway, unlike the bitmap formats where that'll reduce the size of the files as well.

JonasDoebertin commented 9 years ago

Seems like your latest commit broke things.

I guess this line (https://github.com/Grandt/PHPePub/commit/57f3c11d6ced03a5c1e149107f995a92944a9b00#diff-738d6dc186ddd5f8e5366f4bef5652b0R634) shouldn't be commented out, right?

Grandt commented 9 years ago

Agreed, the width and height aren't being set. I'm looking at extracting the SVG metadata now..

JonasDoebertin commented 9 years ago

Nah, I meant the $image = $xml->asXML(); line which was commented out too by accident, i guess.

JonasDoebertin commented 9 years ago

I don't think extracting SVG meta data is necessary. It works fine, even with huge SVGs, right now.

Grandt commented 9 years ago

The reason I commented that $image line out was that it should have been irrelevant. $image was already set.

I have committed another one, as I had practically already finished the rescale part. All I'm doing now is reading the hight/width from the viewbox, and place the rescaled size in the width/height attributes. I builds a valid epub here, though I have to go get a good SVG to test on. If this build fails on your end, I'll comment out the rescaling segment until I crack it right, or feel confident that it'll work and readers will rescale it themselves.

JonasDoebertin commented 9 years ago

Seems to work fine with your latest commit. All SVGs get the correct width/height attributes applied and the ePub displays fine for me.

Grandt commented 9 years ago

Thank you for helping me test this. :)

JonasDoebertin commented 9 years ago

Thank you for maintaining this lib so actively!

Grandt commented 9 years ago

Feel free to let me know if you have any other issues.