External Image resizing - php

So I have a PHP script file that resizes images on the fly. While this has worked for many sites and servers I have one server where it just won't work.
The script works like this:
<img src="resize/thumb2.php?src=http://a8.sphotos.ak.fbcdn.net/hphotos-ak-snc6/284989_230936523610152_118543444849461_606799_3897837_n.jpg&w=150&h=100&type=crop.">
The result is the following error (it various from browser to browser but the gist is that it can't find the file):
Firefox can't find the file at http://xx.xx.xx.xx/~test/tools/resize/thumb2.php?src=http://a8.sphotos.ak.fbcdn.net/hphotos-ak-snc6/284989_230936523610152_118543444849461_606799_3897837_n.jpg&w=150&h=100&type=crop.
So from the above output you can see it's actually trying to open the whole link as the file.
As this is the only server that isn't working, I'm strongly guessing this a server setting issue?
I've tried setting:
ini_set('allow_url_fopen', 1);
ini_set('allow_url_include', 1);
Any help would be appreciated.
Thanks

Not to steal #Pekka's thunder, but his comment is the correct answer. (If he posts it as an answer I'll happily delete)
You need to urlencode the src and then decode it in thumb2.php
<img src="resize/thumb2.php?src=<?php echo urlencode('http://example.com/logo.gif'); ?>">

All text after "resize/thumb2.php?src=" is badly-coded. Try using urlencode() or something similar, that encodes the "query part" of this URL.

Related

PHP Media Link shows gibberish

Never done a lot of work with media files but I have an odd problem. I have a media link
http://.../wb_media/3343/64999/0aa2233675f94a4fc8a3915175e218f3/1/4e5b9927-3a46-4c69-9929-cc7e2a52f616.png
Which is suppose to show an image in the browser yet it shows gibberish:
Not sure where I should start looking to solve this? I have verified this is indeed the correct link. I would even appreciate knowing what that gibberish is called so I can research the problem more.
You must set header for the file type.
<?php
header("Content-type: image/png");
print (file_get_contents("location/to/image.png");
?>
Or if you are not printing it through php script, then you must look into server configuration. How server handles mime-types.

Web page garbled (encoding?) when downloading from within PHP

I am trying to download this page (http://www.360.ru/) from within PHP. However, when I write the file out and view it, the content is garbled/corrupt. However, a different page from the same site downloads with out problems (http://www.360.ru/goods/category/3/466/). And both work perfect well within Chrome & Firefox (which both report the encoding is UTF-8). I can not think what the problem can be. Here is my PHP code:
<?php
file_put_contents('/temp/out.html', fopen("http://www.360.ru/", 'r'));
file_put_contents('/temp/out2.html', fopen("http://www.360.ru/goods/category/3/466/", 'r'));
exit;
?>
When I open the two files, "out.html" is garbled, corrupt and "out2.html" is perfectly okay. Any help would be really appreciated. Thanks!
Ah, figured it out - the first page was gzipped. Using gzopen instead of fopen fixed the problem. Hope this helps others...

download a file with php and hash

So this is simple to understand what i want to achieve. So i get links like theese:
http://rockdizfile.com/atfmzkm7236t
http://rockdizfile.com/xuj5oincoqmy
http://rockdizfile.com/pg8wg9ej3pou
So theese links are from one cloud storage site I want to make a php script that automates their downloading.
So I can't find which is the script or the thing these links download button starts and how can I start that so i can download it with php on my server?
Basically my idea is to download a lot of files but don't wanna do it manually so need automatic way of doing it. As far as I know I make a request which is the following 2 urls:
http://rockdizfile.com/pg8wg9ej3pou
http://wi32.rockdizfile.com/d/wsli6rbhfp4r2ge4t7cqeeztijrprelfiw4afvqg5iwspmvqabpkmgiz/Desislava%20feat.%20Mandi%20&%20Ustata%20-%20Pusni%20go%20pak%20(CDRIP).mp3
So the first url is executing the next one but here comes the tricky part as far as I tested that last string Desislava%20feat.%20Mandi%20&%20Ustata%20-%20Pusni%20go%20pak%20(CDRIP).mp3 is the file name we get when downloading so if you change it with for example somefile.mp3 it will download somefile.mp3 but with the same file content as http://wi32.rockdizfile.com/d/wsli6rbhfp4r2ge4t7cqeeztijrprelfiw4afvqg5iwspmvqabpkmgiz/Desislava%20feat.%20Mandi%20&%20Ustata%20-%20Pusni%20go%20pak%20(CDRIP).mp3 so the data is hidden in this hash wsli6rbhfp4r2ge4t7cqeeztijrprelfiw4afvqg5iwspmvqabpkmgiz or i think so. And now is the tricky part how to get this hash? we have almost everything we have the code for the url atfmzkm7236t the hash wsli6rbhfp4r2ge4t7cqeeztijrprelfiw4afvqg5iwspmvqabpkmgiz and the filename Desislava%20feat.%20Mandi%20&%20Ustata%20-%20Pusni%20go%20pak%20(CDRIP).mp3 There must be a way to download from this site without clicking so please help me kinda a hack this :)
you can use PHP's header function to force a file to download
header('Content-disposition: attachment; filename=index.php');
readfile('Link');
You should know that this will not give you the ability to download PHP files from external websites.
You can only use this if you got the direct link to a file
It's impossibly to tell you without the source code
e.g. sha1("Test Message") gives you 35ee8386410d41d14b3f779fc95f4695f4851682 but sha256("Vote this up") gives you 65e03c456bcc3d71dde6b28d441f5a933f6f0eaf6222e578612f2982759378ed
totally different... unless you're hidden function add's "65e03c456bcc3d71dde6b28dxxxxxxxxxxxxxxxxxxxxxxxxxx" (where xxxxxxxxxxxxxxxxxxxxxxxxxx is a bunch of numbers I can't be arsed to work out) to each hash...
then sha1("Test Message") gives you 65e03c456bcc3d71dde6b28d441f5a933f6f0eaf6222e578612f2982759378ed
The file is embedded into the swf player.
alert(jwplayer('mp3player').config.file);
Something like:
<?PHP echo file_get_contents($_GET["url"]); ?>
<script>
document.location=jwplayer('mp3player').config.file;
</script>
Though I've actually just noticed they change 5 digits of the URL on each page request, and the script above uses 2 page requests. One to get the URL and HTML source and another to try and download the file, meaning the URL has changed before the second request has started.

Using PHP to upload a file that's generated by a URL

So, I have a little PHP code that uses Kaywa to generate and display a QR code:
echo "<img src='http://qrcode.kaywa.com/img.php?s=10&d=$qr_url' alt='QR code' />";
Easy peasy. But for the sake of having a backup, I'd like to save this image to my server, maybe in "myserver/qrbackups". I know how to make PHP upload a file from a form, but can I do from a retrieved image URL?
See file_get_contents.
$data = file_get_contents("http://qrcode.kaywa.com/img.php?s=10&d=$qr_url");
$saved = file_put_contents('/path/to/myserver/qrbackups/the-code.png', $data);
Keep in mind /path/to/myserver/qrbackups/the-code.png should be a unique file name for each individual QR code.
You can use cURL to programmatically access URLs.
http://us3.php.net/manual/en/curl.examples-basic.php
You can try to use curl or file_get_contents to pull the file from the server.
For example:
http://www.phpriot.com/articles/download-with-curl-and-php
And after getting the file just display the one you got from your server or remote.
copy('http://domain.com/path', '/tmp/file.jpeg');
should work for you. And, as others have pointed you, cURL will work too.
If you have the GD extension on your server, you can use a library like PHP QR Code to eliminate the dependency on Kaywa. Usage requires only one line of code wherever you want to generate a QR code.

Why getting image via PHP does not work?

I'm working with WampServer Version 2.0 on Windows XP and FireFox 3.6.8.
I'm trying to get image content via PHP script like this:
HTML:
<img src='temp_get_file.php' alt='picture not found' />
PHP: (temp_get_file.php)
<?php
header('Content-Type: image/png');
$img = imagecreatefromjpeg("1.png");
imagejpeg($img);
imagedestroy($img);
?>
The HTML, PHP, and 1.png files are located in the www directory of WampServer.
Unfortunately, I got this error (in HTTPFOX plugin in FireFox):
Error loading content (NS_ERROR_DOCUMENT_NOT_CACHED)
and I see "picture not found".
If I put the image in HTML directly like this:
<img src='1.png' alt='picture not found' />
everything works fine.
What's wrong with my PHP ?
This may just be a problem in your example, but this won't work:
imagecreatefromjpeg("1.png")
^ ^
JPEG != PNG
Not with your PHP actually but with your PHP skills :)
Some advises to improve
you have to debug your application instead of asking community.
To do so, you have to
a) request your image file directly, by typing temp_get_file.php into browsers address bar, to let you see output of the script
b) put Content-Type header output as low in the code, as possible, to let PHP sent text/html in case of some errors
c) have displaying errors on
or
instead of all this above you can turn logging errors on and catch the error in the error log.
both methods will let you to get PHP error message - a thing you really need here, instead of useless firefox complains.
and this error message is pretty clear - wrong file format.
if it's the only thing what your script does, you don't need all these GD functions. thats useless. if you need to output some file to the browser, just do it. readfile("1.png") is enough

Categories