1

Tumblr image example:

https://69.media.tumblr.com/ec3874e541a767ab578495b762201a53/tumblr_ph9je0Akck1r89eh5o1_1280.jpg

Code:

<form method="get">
    <input type="text" name="url"/>
    <input type="submit" value=" загрузить "/>
</form>

<?php
 $name = md5(date('Y-m-d H:i:s').rand(0, 1000));
 $folder = 'upload/';
 $source = ($_GET['url']);
 $dest = $folder.$name.'.png';

 copy($source, $dest);
 echo 'http://mysite.example/'.$folder.$name.'.png';
?>

I found this in another question on this site:

If you can view the image in a browser that is on the same machine as your program, then it might be that the server won't send the picture unless you look like a user rather than a program. In that case, modifying the browser identification string might fix your problem. If you cannot view the image from a browser running on the program's PC, you will need to look elsewhere for the source of your problem.

I think, a problem i have is similar to this. Tumblr gives picture to view in a browser, but doesn't allow to copy it with a script.

How to fix that? For example, sites like imgur can upload Tumblr images by url with no any problem.

P.S. For images from other sites copying with this script goes normally.

Addition 01:

As it turned out, the problem is with my site. When i run this code on another site, it works normally with Tumblr images. I have a free domain .ml and free hosting Byethost. I have two guessings. The first is, my domain or hosting is in a blacklist on Tubmlr. The second one, i have some wrong settings on my site. If first guessing is right, is there any way to make it works without changing domain or hosting? If the second is true, what a settings i must check and change?

1 Answers1

0

Tumblr appears to be inspecting the HTTP request and generating different responses depending on how you get it. Your code is fine, as you know, for most sites. When I run it as-is, I get a 403 denied error.

Changing the code to use Curl instead allowed me to download your file. My guess is the default headers used in PHP's copy() are blocked.

<?php
function grab_image($url,$saveto){
    $ch = curl_init ($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($saveto)){
        unlink($saveto);
    }
    $fp = fopen($saveto,'x');
    fwrite($fp, $raw);
    fclose($fp);
}

$name = md5(date('Y-m-d H:i:s').rand(0, 1000));
$folder = 'upload/';
$source = ($_GET['url']);
$dest = $folder.$name.'.png';

grab_image($source, $dest);

The grab_image() function was a SO answer for another question.

Dan Bowling
  • 1,205
  • 1
  • 15
  • 41
  • I try to use your code (copyed and pasted how it is), it works fine for images from other sites, but for Tumblr images still not working. Page with result loads long (i guess until the time out), after that, in a download directory appears a file with .png extension, 0 bytes, empty, with no data inside. This is where i try to do this: [link](http://alisabadass.ml/ex.php) – Inga Buhtilova Oct 30 '18 at 01:27
  • Do you have shell access to the server? If so, try running ‘curl -I https://69.media.tumblr.com/ec3874e541a767ab578495b762201a53/tumblr_ph9je0Akck1r89eh5o1_1280.jpg’ and see what the output is. Maybe paste that as an edit in your question. Also, your sample code saves to .png, just like what I pasted, but the URL you gave is a .jpg. I don’t think that would affect the download though. – Dan Bowling Oct 30 '18 at 01:41
  • I have no shell access, free hosting on byethost. Changing extention to .jpg gives nothing. When you run this code on your server, do you get a proper image file in download directory? Not empty? Is it depends only of my hosting settings? – Inga Buhtilova Oct 30 '18 at 02:15
  • I ran the function on my local host and it had no problem—I was able to open the image in a preview app. Let’s try making sure your PHP script can properly write to a file. Can you try using one of the examples here to write a text file? http://php.net/manual/en/function.file-put-contents.php. That will tell us if you have the proper permissions on the directory. – Dan Bowling Oct 30 '18 at 02:40
  • It works. I put a .php file with a code from example to my download directory, and it created a people.text with "John Smith" text inside. – Inga Buhtilova Oct 30 '18 at 03:10
  • I tried to use another browser to save Tumblr image with this script. I got an image.jpg in downloads directory, saved it and opened with code editor. It contains a html page code with javascript: [screenshot](https://i.imgur.com/473G7GNr.png) ( `` ). I changed it's extension to .htlm and opened it in a browser. It showed me listing of my C:\ drive [screenshot](https://i.imgur.com/S5wYLvCr.png). Here is a source code of that "image": [jsfiddle.net/4qjdycL3](https://jsfiddle.net/4qjdycL3/). – Inga Buhtilova Oct 30 '18 at 13:56
  • I tried to run this script on another site, and it works perfect with Tumblr images. Also, the first code, without Curl, just with copy(), works too. So the problem seems in my site. I use free domain .ml (i got it on Freenom) and free hosting Byethost. I guess, one of them of both are in a blacklist on Tumblr. Is there any way to avoid this without changin domain or hosting? – Inga Buhtilova Oct 31 '18 at 14:37
  • Inga, I’m glad to hear if this development. Im not quite sure what advice I can give regarding Tumblr blacklisting your host. It seems unlikely that they have blacklisted your domain, and more likely that the IP address of your free web host is blocked. If I was in your shoes, I would try a new host first. I have no experience with free hosting so I can’t suggest any. There are many reputable low cost hosting providers though if you can afford $5 per month. – Dan Bowling Oct 31 '18 at 16:52
  • Thank you for your help. I have a new guessing, the problem is in unsecure connection. My free hosting gives automatically a Self-signed certificate, and i think, it is not anough for Tumblr. I found an information, how to get a certificate for free, and now i trying to get it. It requires to have an email like this one: "postmaster, hostmaster, webmaster or email from whois". I trying now to get one, it is requires 48 h to applying a MX Record added on a hosting. That all thisngs is new for me, but i will try and hope. Thank you. – Inga Buhtilova Oct 31 '18 at 17:38
  • P.S. Paid hosting, even a cheap one, is not a solution, because this project i make just for myself and just for fun. It makes square pictures from ones with portrait or landscape orientation without cropping. Now it works perfect with images uploaded from device, but i want to add a possibility to upload an images from web. That requires some CORS magic or uploading remote images to my server first. Here is this project: [http://alisabadass.ml/blur5/blur.html](http://alisabadass.ml/blur5/blur.html) – Inga Buhtilova Oct 31 '18 at 17:39
  • You can easily test your theory about SSL by trying an image on any domain with https enabled. Making https requests as a client has more to do with your trust of the remote server though, which is handled with certificates installed in the OS other than a SSL very tied to your domain. Installing an SSL cert on your domain simply permits people to connect to you via SSL. Also, security errors are usually logged as such, not as timeouts. – Dan Bowling Oct 31 '18 at 21:37