Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Copying images from URL

Comments

  • Registered Users, Registered Users 2 Posts: 2,157 ✭✭✭dazberry


    While it has no extension, if you make the request in chrome you can see the response header content type is image/webp. Subsequently right clicking on the image in chrome and selecting save as - wants to save the image as a webp file.

    There does seem to be support in php to convert from webp to jpg:

    https://stackoverflow.com/questions/31132343/how-to-convert-webp-image-format-to-normal-php

    hth



  • Registered Users, Registered Users 2 Posts: 7,140 ✭✭✭Talisman


    If you use the browser to check the type, you need to be mindful of the fact that the browser specifies a preference for content types in the headers when it sends a request. By default modern browsers prefer the webp image format so that is the format you are likely to get by opening the link in a browser.

    To identify the content type, you should check the 'Content-Type' in the headers for the request's response.

    <?php
        $media_url ='https://media.daft.ie/eyJidWNrZXQiOiJtZWRpYW1hc3Rlci1zM2V1IiwiZWRpdHMiOnsicmVzaXplIjp7ImZpdCI6ImNvdmVyIiwid2lkdGgiOjE0NDAsImhlaWdodCI6OTYwfX0sIm91dHB1dEZvcm1hdCI6ImpwZWciLCJrZXkiOiI3L2YvN2Y2MTRjOGY1MWJlMTA2ZmY4MjAzOGY1ZmM2ZDFjODguanBnIn0=?signature=a4a022137faf3f2e1e4a999399f66e89778dae1aedbbce893ba7224bd134a976';
        $headers = get_headers($media_url, 1);
        print_r($headers);
    ?>
    

    In this instance, the 'Content-Type' header has a value of 'image/jpeg' so the response should be a JPEG image.

    Array
    (
       [0] => HTTP/1.1 200 OK
       [Content-Type] => image/jpeg
       [Content-Length] => 155311
       [Connection] => close
       [Date] => Tue, 21 Mar 2023 17:37:51 GMT
       [x-amzn-RequestId] => e197cbd2-f7bc-4d0f-a9d1-1cde8c08abde
       [Last-Modified] => Mon, 20 Mar 2023 14:43:28 GMT
       [Access-Control-Allow-Headers] => Content-Type, Authorization
       [x-amz-apigw-id] => CJG5XGPVDoEFX7g=
       [Cache-Control] => max-age=31536000,public
       [Access-Control-Allow-Methods] => GET
       [X-Amzn-Trace-Id] => Root=1-6419eb6e-7dfcaf7a6443a75a122bcf1e
       [Access-Control-Allow-Credentials] => true
       [X-Cache] => Hit from cloudfront
       [Via] => 1.1 e20527248be1eebaced63108ab7e73d6.cloudfront.net (CloudFront)
       [X-Amz-Cf-Pop] => DUB56-P1
       [X-Amz-Cf-Id] => 2XOUtidXkk87K8qmC80SxQUn72ekQtmq4LPvMSobXnQMJRrj5O0PjQ==
       [Age] => 590136
    )
    

    You could use curl to save the image.

    curl 'https://media.daft.ie/eyJidWNrZXQiOiJtZWRpYW1hc3Rlci1zM2V1IiwiZWRpdHMiOnsicmVzaXplIjp7ImZpdCI6ImNvdmVyIiwid2lkdGgiOjE0NDAsImhlaWdodCI6OTYwfX0sIm91dHB1dEZvcm1hdCI6ImpwZWciLCJrZXkiOiI3L2YvN2Y2MTRjOGY1MWJlMTA2ZmY4MjAzOGY1ZmM2ZDFjODguanBnIn0=?signature=a4a022137faf3f2e1e4a999399f66e89778dae1aedbbce893ba7224bd134a976' --output filename.jpg
    

    Here's a PHP script to download the image using curl with a provided filename.

    <?php
    
    function save_binary_to_file($binary_url, $save_file){
      $ch = curl_init($binary_url);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
      $raw = curl_exec($ch);
      curl_close ($ch);
      if (file_exists($save_file)) {
          unlink($save_file);
      }
      $fp = fopen($save_file, 'x');
      fwrite($fp, $raw);
      fclose($fp);
    }
    
    $media_url = 'https://media.daft.ie/eyJidWNrZXQiOiJtZWRpYW1hc3Rlci1zM2V1IiwiZWRpdHMiOnsicmVzaXplIjp7ImZpdCI6ImNvdmVyIiwid2lkdGgiOjE0NDAsImhlaWdodCI6OTYwfX0sIm91dHB1dEZvcm1hdCI6ImpwZWciLCJrZXkiOiI3L2YvN2Y2MTRjOGY1MWJlMTA2ZmY4MjAzOGY1ZmM2ZDFjODguanBnIn0=?signature=a4a022137faf3f2e1e4a999399f66e89778dae1aedbbce893ba7224bd134a976';
    
    $fname = 'downloaded-by-script.jpg';
    
    save_binary_to_file($media_url, $fname);
    
    ?>
    


Advertisement