How can I save this image?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    How can I save this image?

    Hi,

    I am using snapcasa.com to provide me with an image.

    They encourage you to cache the images on your own server so I am trying to do that.

    When I use this code to display the image it works fine:

    Code:
    <img src=\"http://snapcasa.com/get.aspx?code=9360&size=l&url=$Db_url\" width='300px' height='200px' alt='Thumbnail for $title_sht'>
    Now, I want to save this image locally so I tried this:

    Code:
    $image_data = file_get_contents("http://snapcasa.com/get.aspx?code=9360&size=l&url=$Db_url");
    file_put_contents($image_file, $image_data);
    When I do the above I don't get the real image returned but only their
    default "Unregister ed Domain" image.

    This maybe an anti-hammer reaction because of the quick succession of my image requests.

    So since I already have the image displayed using the <img src=
    tag, is there a way to save this image locally ?

    If not, maybe I should use the file_get_conten ts() function to get the image data in the first place ?

    Would appreciate any input on this :)



    .
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    When you use the file_get_conten ts function, are you displaying the image in the <img> tag using the remote URL, or the local URL?

    It should be something like:
    [code=php]$url = "http://snapcasa.com/get.aspx?code=9 360&size=l&url= $Db_url";
    $image_data = file_get_conten ts($url);
    file_put_conten ts($image_file, $image_data);
    echo "<img src='{$image_fi le}' width='300px' height='200px' alt='Thumbnail for {$title_sht}'>" ;[/code]

    And be sure that the image URL is in fact what it should be. (Just print it before using it, to see if it is all OK.)

    Comment

    • jeddiki
      Contributor
      • Jan 2009
      • 290

      #3
      Hi Atli,

      Thanks for your reply.

      The way you did it is more logical as it requires only one call.

      But for some reason the snapcasa.com site still does not accept
      it.

      I think their delivery script checks that my script or <img> tag
      is coming from a website domain that I have registered with them.

      I have registered my domain and IP address with them.

      Maybe when I use the file_get_conten ts($url) method, it is anonymous and
      therefore only gets back their "Unregister ed Domain" image - not the image that I want.

      When I use the <img> tag with their address in it,
      it works fine.

      Any more ideas on how I can save the image ?

      In theory it should possible to save the image after I have got it
      and served it to my website - after all it is in my server's memory.

      Just to recap - this works:
      Code:
      <img src=\"http://snapcasa.com/get.aspx?code=9360&size=l&url=$Db_url\" width='300px' height='200px' alt='Thumbnail for $title_sht'>


      But this doesn't :

      Code:
      $url = "http://snapcasa.com/get.aspx?code=9360&size=l&url=$Db_url";
      $image_data = file_get_contents($url);
      file_put_contents($image_file, $image_data);
      echo "<img src=\"$image_file\"  width='300px' height='200px' alt='Please wait while thumbnail loads ...'>";

      And by "doesn't work" I mean that it does return an image, but not the one requested,
      it delivers a standard "Unregister ed Domain" image.


      Thanks.



      .

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        I think their delivery script checks that my script or <img> tag
        is coming from a website domain that I have registered with them. [...]
        Maybe when I use the file_get_conten ts($url) method, it is anonymous and
        therefore only gets back their "Unregister ed Domain" image - not the image that I want.
        That should not be the case. If the <img> tag version is meant to work on more than a single compute (which I assume it is?) then checking where the request is coming from and denying access based on that makes no sense. - It's the browser that requests the image, not the server, so that each person visiting your website would be requesting the image from a different, usually domain-less, IP address.

        It's far more likely that the URL you use in your script is somehow malformed.
        In your last example, before you call the file_get_conten ts() function, have you defined the $Db_url variable? If so, are you sure it is valid? - Try printing the $url, just to make sure it is as you expect it to be.

        Comment

        • jeddiki
          Contributor
          • Jan 2009
          • 290

          #5
          Hi Atli,

          I have now changed the code to display the
          url and then afterwards use the url in the img tag.

          Like this:

          Code:
          $url = "http://snapcasa.com/get.aspx?code=9360&size=l&url=$Db_url";
          $image_data = file_get_contents($url);
          file_put_contents($image_file, $image_data);
          echo "<img src=\"$image_file\"  width='300px' height='200px' alt='Please wait while thumbnail loads ...'>
          <br>
          <br><br>The Paydotcom Product Code: $Db_prod
          <br><br>Image Url: $url
          <br><br>
          <img src=\"$url\"  width='300px' height='200px' alt='Please wait while thumbnail loads ...'>
          <br><br>
          <span style = \"font-size:18px; color:darkblue;\"><b>Customer Payments Schedule:<br>$price</b></span>
          I think it proves that the url is fine:

          The result can be seen for yourself:

          Test Page

          You will see the file_get_conten ts function attempt
          in a box with just the ALT showing followed by the immage using the
          same url but in the img tag.

          I hope you can see a fault with my script ;-)

          If not, how else can I save the file ?


          Thanks for your help.



          .

          Comment

          • jeddiki
            Contributor
            • Jan 2009
            • 290

            #6
            Any more suggestions on this one ...


            I am still stuck :(


            .

            Comment

            • philipwayne
              New Member
              • Mar 2010
              • 50

              #7
              Just read the file contents and write them to your own file.

              Code:
              $fh = fopen( "http://snapcasa.com/get.aspx?code=9360&size=l&url=$Db_url", "rb" );
              $fh2 = fopen( "myimage.img", "w" );
              while( ( $content = fread( $fh, 1024 ) ) )
              {
              fwrite( $fh2, $content );
              }
              fclose( $fh );
              fclose( $fh2 );
              Note: allow_url_fopen must be turned on for the above code to work.

              Comment

              Working...