Imagecreatefromstring and mime types

Greetings,

I’m having problems with PHP’s getimagesize() function on remote images. Usually this used to work in a fraction of a second, but I noticed on some remote images it can take 200 seconds or more. I need to use this function to get the image size and the MIME type. Once I have the MIME type, I can run something like imagecreatefromjpeg on JPG files only, or imagecreatefrompng for PNG files only, etc…

I studied up and learned about using cURL and getting an image using: imagecreatefromstring() in order to download the files faster , but I still have questions about this function.

If I use CURL to download a GIF or PNG file, I understand that imagecreatefromstring turn it into GD’s internal image format… Does this mean the resulting image can be saved in any format (GIF to JPG, or PNG to JPG), without errors or corruption or quality problems?

Thanks
Kind regards

Yes, that’s right.

I tried this function on a PNG file which had a transparent background and saved it as JPG. The JPG file ended up with a black background. How do I fix this issue and other similar issues with GIF files?

Thanks
Kind regards

JPEG doesn’t support transparency.

Save to format which supports alpha-channel (transparency) - PNG or GIF.

That will depend on the responsiveness of the server that hosts the images, presumably.

So the speed of retrieving the remote image doesn’t depend on if I use cURL or getimagesize()?

Correct

Though using curl you have control over timeout and what not.

I have cURL set up in a “do while” loop with a timeout ( CURLOPT_TIMEOUT ) set at 20 seconds. So if the image doesn’t load in 20 seconds, the “do while” loop will repeat or retry to get the image.

Do you think 20 seconds is a good timeout amount? What should I do if the image is large and takes more than 20 seconds to load?

That all depends on the context which this is running. Is this some type of cron/background process that loads several hundred images or something?

Yes, this is a type of cronjob. Sellers import URLs to 3rd-party external images on our site. Our cronjob takes about 50 items at a time, which could have up to a maximum of 600 images (usually it’s only 50 images).

These images come from virtually anywhere, wherever the seller hosts their images. We attempt to copy and resize each image and save it to our own server.

No offence, but cronjobs aren’t really a good solution for this kind of problem. Sure it works at first, but you run into scaling and/or locking problems quite fast.
What I would suggest instead is a work queue like beanstalkd, where you can send messages to of jobs that need to be done, and workers (other php scripts) can pick those up in the background and process them.
You could then for example put a job in the queue that image XYZ must be downloaded, which will be then be done by a worker when it picks up that job (it’s in a queue, so the worker might be busy doing something else). After it’s been downloaded that job itself can put a new job in the queue that the new image, XYZ must be resized and saved. The cool thing about this is that you’re not limited to 1 worker per type, so you could have multiple workers all downloading images at the same time, and multiple workers all processing and saving images at the same time, allowing you to go a lot faster and process a lot more than any cron setup could get you.

Food for thought :slight_smile:

1 Like

Thanks for the beanstalkd tip @ScallioXTX . I guess my site isn’t large enough for that yet. I don’t actually need the images copied and resized immediately, so the cronjob takes care of it fine for now. Although I will keep this in mind in case my site needs it someday.

I was able to build a really nice script using cURL with 30 second timeouts/retry along with imagecreatefromstring() to convert ALL images to JPG format to make it simple. I was also able to figure out how to turn the transparent background into a default white background in regards to PNG/GIF images.

With the image transparency format issue fixed, is there anything else I need to keep in mind in regards to image quality while converting PNG/GIF images to JPG? Any potential problems when converting images to JPG?

Thanks
Kind regards

Apart from the loss of transparency, nope :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.