-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Filipe Brandenburger wrote:
Brion Vibber wrote:
Ideally, you can in fact extract a size from the file. If you can't get it from the file yourself, you should be able to get it from your external program in some way. (Worst case, render to a temporary PNG at "native" size and check *its* size. Probably crappy, but hey.)
Yes, "worst case" is what I am doing now, rendering to a file in /tmp, using PHP's getimagesize() to get its size, and deleting the temporary file. However this is very CPU intensive.
Would there be a way for me to "save" this temporary file somewhere inside the uploads directory, in a way that I could use it later in doTransform?
Well, one thing to consider is that you might never see that same image again. You might be running checks on a file whose upload will be canceled later, or which might not go through a standard upload process at all (say, some sort of batch process).
I mean, in getImageSize() I would write the temporary file to somewhere inside the upload directory (how could I automate the creation of a path for that?) and not remove it after getting its size. Then, in doTransform(), I would check if the requested dimensions match the ones of the temporary file I created, if they do, I would just copy/rename/hardlink the original file to the new one. Would that work? How should I manage creating a path on the upload directory for that? (I'm thinking something such as "math" or "graphviz" under the root of uploads.)
You might create yourself a temp rendering directory, store rendered files by hash, and just check it later if you need it again or at upload time.
Unless rendering is really slow, however, it's probably not worth the effort; rendering twice at upload time is probably not super-painful and it's not likely to be a time-critical code path. I recommend you check how long this takes in practice before investing more effort in optimization tricks.
- -- brion