Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Copying images from URL

Options

Comments

  • Registered Users Posts: 2,145 ✭✭✭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 Posts: 6,002 ✭✭✭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