Home > database >  Get Image Data in in PHP or Laravel From URL
Get Image Data in in PHP or Laravel From URL

Time:09-29

I want to read file data especially image file but, the image is already uploaded in somewhere or server directory, not from input html. How can I get this? Thank you

ex

$url = 'https://pbs.twimg.com/profile_images/805651234698842112/uPSrYOHT.jpg'

I want to get that file data same as with $_FILES post from input html.

CodePudding user response:

You can try this function:

function download($url, $outputPath)
{
    set_time_limit(0);
    $fp = fopen($outputPath, 'w ');
    $ch = curl_init(str_replace(' ', ' ', $url));
    curl_setopt($ch, CURLOPT_TIMEOUT, 50);
    curl_setopt($ch, CURLOPT_FILE, $fp); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($ch); 
    curl_close($ch);
    fclose($fp);
}

CodePudding user response:

Try following code you might get all the necessary data.

<?php

// Your code here!
$url = "https://pbs.twimg.com/profile_images/805651234698842112/uPSrYOHT.jpg";
$temp = tempnam(sys_get_temp_dir(), 'TMP_');
echo $temp; // output => C:\Windows\Temp\TMP2142.tmp

file_put_contents($temp, file_get_contents($url));

$content = file_get_contents($temp);

$size = getimagesize($temp);
$extension = image_type_to_extension($size[2]);
echo $content;
echo "<br>";
echo print_r($size); // output => Array ( [0] => 120 [1] => 120 [2] => 3 [3] => width="120" height="120" [bits] => 4 [mime] => image/png )

echo "<br>";
echo $extension; // output => .png
  • Related