Get image size via CURL in PHP

I used this code to get the image size in php and it worked fine for me.

$img = get_headers("http://ultoo.com/img_single.php", 1); $size = $img["Content-Length"]; echo $size; 

But how to get this through CURL? I tried this but it does not work.

 $url = 'http://ultoo.com/img_single.php'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, "Content-Length" ); //curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); $filesize = $result["Content-Length"]; curl_close($ch); echo $filesize; 
+4
source share
3 answers

set curl_setopt($ch, CURLOPT_HTTPHEADER, true ); , then print_r($result) , you will see something like

 HTTP/1.1 200 OK Date: Tue, 04 Jun 2013 03:12:38 GMT Server: Apache/2.2.15 (Red Hat) X-Powered-By: PHP/5.3.3 Set-Cookie: PHPSESSID=rtd17m2uig3liu63ftlobcf195; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Content-Length: 211 Content-Type: image/png 

I don't think Content-Length is the right way to get the image size, because I get a different result between curl and get_header

+1
source

I came across this before, the script that I used can be found here β†’ http://boolean.co.nz/blog/curl-remote-filesize/638/ . Pretty similar to the message above, but a little more straightforward and much less forgiving.

0
source

Maybe this works for you (assuming the page always just returns img/png ). I included a β€œrecording file” to be able to compare the screen output and file size ( png from the page):

 $url = 'http://ultoo.com/img_single.php'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //return the output as a variable curl_setopt($ch, CURLOPT_TIMEOUT, 10); //time out length $data = curl_exec($ch); if (!$data) { echo "<br />cURL error:<br/>\n"; echo "#" . curl_errno($ch) . "<br/>\n"; echo curl_error($ch) . "<br/>\n"; echo "Detailed information:"; var_dump(curl_getinfo($ch)); die(); } curl_close($ch); $handle = fopen("image.png", "w"); fwrite($handle, $data); fclose($handle); $fileSize = strlen($data); echo "fileSize = $fileSize\n"; 
0
source

Source: https://habr.com/ru/post/1484316/


All Articles