Text to Image - massive usage of Ressources compared to GD

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
strgg
Posts: 16
Joined: 2012-04-30T18:42:58-07:00
Authentication code: 13

Text to Image - massive usage of Ressources compared to GD

Post by strgg »

Hello community

I try to transform text to image with 2 simple lines of code:

Code: Select all

$content =  file_get_contents("longtextclean.txt");
exec('convert -background transparent -fill orange -font Arial -pointsize 12 -interline-spacing -5 -size 3500x3500 caption:"'.$content.'" textimage.png')
You can try yourself by just copy/pasting some words into "longtextclean.txt", i used 6000 characters in mine.

With 6000 characters the generated file gets 140mb in size and to render takes more than 60 seconds. Comparing doing it with GD in half the time this was unexpected.
Considering i intend to make use of 50/k words even by increasing memory_limit & max_execution_time the script will abort.

So my question: What can i do the speed up the process AND reduce the size of the file in mb without losing much quality ?
( I already set the pointsize to make the automatic calculation obsolete, maybe the use of bmp fonts would speed up too, unfortunately i can't make use of that)

Thanks for tips
Have a great day!

Gerome
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Text to Image - massive usage of Ressources compared to

Post by anthony »

Yes, but GD is very limited in just what it can do!!!!!

If you want small use GD,

If you want even smaller (but no text capability), use PbmPlus or NetPbm (same thing)

If you want features use ImageMagick.

These is a lot to be said for small and concise, verses large and feature rich :-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Text to Image - massive usage of Ressources compared to

Post by anthony »

Sorry you are not talking about machine resources, as disk resourses...

In this case IM tries to give you the best result it can. GD the simplest result.

Use png8 if you want to compare to GD, though IM will do a lot more work to 'reduce the colors' that GD does by default.

See IM Examples formats, PNG output formats
http://www.imagemagick.org/Usage/formats/#png_formats
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
strgg
Posts: 16
Joined: 2012-04-30T18:42:58-07:00
Authentication code: 13

Re: Text to Image - massive usage of Ressources compared to

Post by strgg »

Hi Anthony

Thanks for the info. I thought in IM it might be an even faster way by using some options to reduce processing, quality and therefor size. Considering your statement i can assume it is not. right?

I love IM since its very versatile. My project might be unusual (whole sites of text to image) since it is for an art school.

Using IM with the stated code, the resulting image (if not aborted) would be more than 1 GB. If IM is capable to reduce that (equally with quality i guess) i am happy for any hint.
I can make the same image with inkscape for 1mb space. Why?

If i cant solve size and ressource usage (memory which relates heavily on size too) its not even worth thinking about creating smaller pieces to put them together afterwards to overcome script abort.

Thanks for your coming thoughts.

Regards,
Gerome

EDIT:
Thanks for the tip with PNG8, i will try that. Especially as i actually need the text in black and the background transparent only. After that the textimage is taken to mask and cut a same-sized photography ... similar like ascii-style images.
Last edited by strgg on 2012-04-30T19:47:29-07:00, edited 1 time in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Text to Image - massive usage of Ressources compared to

Post by anthony »

In your command IM processes the image using DirectClass (actual arrays of color values)
GD I believe limits itself to PaletteClass (with a limited -presumably grayscale palatte).

PNG8 may actually work quite fast in this case. Try outputting to PNG8:image.png

Note it is also using some higher level font rendering libraries. GD I believe uses much simpler fast font rendering libraries.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
strgg
Posts: 16
Joined: 2012-04-30T18:42:58-07:00
Authentication code: 13

Re: Text to Image - massive usage of Ressources compared to

Post by strgg »

Thanks anthony. I saw your 2nd reply and edited my above. I will try PNG8 and report back about info about ressources used, it might be of interest for others. For the last part; masking and cuting; IM is perfect, hope to do the text also with IM (png8), see soon.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Text to Image - massive usage of Ressources compared to

Post by fmw42 »

I don't know for sure as I don't use GD, but I does it even allow controls such as interline-spacing.
strgg
Posts: 16
Joined: 2012-04-30T18:42:58-07:00
Authentication code: 13

Re: Text to Image - massive usage of Ressources compared to

Post by strgg »

FMW42

Unfortunately no line-spacing in GD. Use can create it with ressource intense php scripting though

Code: Select all

$newY=$y+($i * $size * $spacing);
    imagettftext($image, $size, $angle, $x, $newY, $color, $fontfile,  $lines[$i], $spacing);
Same counts for word-wrapping. It's not fun with GD considering doing it adjustment with PHP (which is easy but ressource intense).
strgg
Posts: 16
Joined: 2012-04-30T18:42:58-07:00
Authentication code: 13

Re: Text to Image - massive usage of Ressources compared to

Post by strgg »

Using PnG's in decreased quality like suggested here http://www.imagemagick.org/Usage/formats/#png_formats

Size and processing time decreases up to 97% and quality too. In some cases it might be appropriate to create a bicolor png (4mb compared to 140 mb) and than add features in postprocessing.

In my case (TEXT) its not usable since all transparency (antialiasing) is gone and text is hardly recognizable.

Unfortunately i have to go with GD for this process eventhough its very limitations. Without looking into code it seems GD gets just the right compromiss between quality and ressource usage / speed. I, naively, assume IM is build non-modular, not letting us use functions spot-on skipping some preprocessing if not needed.

Thank you Anthony for the help and above all for a fantastic module (without that i would not have been able to succeed in my humble project; masking!!)
Thanks also FMW, just discovered your website.

Have a great day!!

Gerome
Post Reply