barendm
(Barendm)
March 16, 2023, 1:04pm
1
I notice that PHP function file_get_contents() is very slow (I experience a timeout of about 30 seconds)
I found a ‘solution’ to use curl instead. So I did, but later I found that imagecreatefromjpeg() was slow aswell.
I guess that beneath the surface it uses file_get_contents()?
So I wonder if someone knows a solve for the main problem here.
I use the PHP docker image php-master\8.2\bullseye\apache
And add GD functionality to it like so:
# setup GD extension
RUN apt-get update && \
apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev && \
docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ && \
docker-php-ext-install gd
Then I have trouble with any PHP script that looks like this:
// Create a blank image and add some text
$im = imagecreatefromjpeg($url);
or like this:
$data = file_get_contents('https://example.com');
After about 30 seconds these functions work like expected.
rimelek
(Ákos Takács)
March 16, 2023, 11:55pm
2
Please, share the output of the following commands
docker version
docker info
Are you sure it is related to Docker?
You mean the curl functions or curl from a shell?
barendm
(Barendm)
March 17, 2023, 1:49pm
4
Turned out that the slowness didn’t have anything to do with Docker, but was probably related to the data source.
Never the less, in the end I used Curl to load the data and the image.
function readData($url) {
$ch = curl_init(); // Initialize cURL session
curl_setopt($ch, CURLOPT_URL, $url); // Set the URL to fetch
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response instead of outputting it
$response = curl_exec($ch); // Execute the request and get the response
curl_close($ch); // Close cURL session
return $response;
}
function readImage($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11');
$res = curl_exec($ch);
$rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch) ;
return imagecreatefromstring($res);
}