PHP Media Link shows gibberish - php

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.

Related

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...

PHP function file_get_contents retrieves encoded information - header: "'Content-Type: image/png'"

Hi everyone I am having a bit of a problem related to the php function file_get_contents.
I used it many times and no problems but when I am trying to get some information from a particular site the information I get when I echo the result is pretty much encoded (Example: ���IHDR�).
I looked at the header of the site and instead of saying
Content-Type: text/html;
it is saying
Content-Type: image/png
How do I decode that so I can get the source code (html) of the site? The web-site when I go to it in a browser, it looks like a regular web-site: text, images nothing out of ordinary.
When I look at the source code nothing out of ordinary there either. But when I do a file_get_contents I do not get the source code like I used to get on other websites.
Any ideas?
Note: I had the same problem in the past it was encoded in GZIP and I was able to find a function to decode it but with Content-Type: image/png I do not know how to proceed.
Why not, create a basic test script to the output the returned image, tho I suspect its an image saying:
Stop scrapping my site!!! Yada Yada
header('Content-Type: image/png');
echo file_get_contents('http://example.com');
The Content-Type header tells you which content-type the requested file has, in your case it is a PNG image (image/png).
You find a description of many content-types (written in a so called mime-type specification) online, this is a nice list: fileformat.info MIME types.
As you might can imagine, it's not possible to display an image in text-form (at least not before converting it to ascii art) so you will not have much luck this time.
Check the URI if it is really the one you wanted to obtain.

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

TCPDF outputs weird characters in IE8

Today I started experimenting with PHP-based PDF generators. I tried TCPDF and it works fine for the most part, although it seems to be a little slow. But when I load the PHP file that generates my PDF in Internet Explorer 8, I see lines and lines of weird characters. Chrome however recognizes it as a PDF.
I'm assuming that I have to set a special MIME type to tell IE that it should interpret the page output as a PDF file. If yes, how can I do this?
putting "application/pdf" or "application/octet-stream" mime types might help. keep in mind that "application/octet-stream" will force download of the file and might prevent it from opening in the browser..
in case you wonder, you can do it like that:
header('Content-type: application/octet-stream');
I had this problem also but what I did to get it work is I added
exit();
at the end of pdf output.
You need to handle IE differently for dynamic-generated content. See this article,
http://support.microsoft.com/default.aspx?scid=kb;en-us;293792
In my code, I do this,
if(isset($_SERVER['HTTP_USER_AGENT']) AND ($_SERVER['HTTP_USER_AGENT']=='contype')) {
header('Content-Type: application/pdf');
exit;
}
This problem may also explain slowness you mentioned because your page actually sends the whole PDF multiple times without this logic.
#Pieter: I was experiencing the same issue using tcpdf (with fpdi), and loading the page that was generating the pdf using an ajax call. I changed the javascript to load the page using window.location instead, and the issue went away and the performance was much better. I believe that the other two posters are correct in the idea that the document header is causing the issue. In my case, because of the ajax call, the header was not being applied to the whole document, and causing the issue. Hope this helps.
I found this to be a problem too, and for me this all hinged on the code:
if (php_sapi_name( != 'cli') {
on line 7249 of the tcpdf.php file.
I commented this 'if' statement (and related '}')and all works fine for my other browser and ie8
Hope this helps

Displaying image with php

I have a script which displays images like this:
header("Content-Type: image/{$ext}");
readfile($image->path);
This has worked fine for weeks and now suddenly it has stopped working. The response header looks fine (Content-Type: image/jpg), I have no ending php-tag and I have made no changes to my code, server- or php-setup which could have caused this to malfunction. Does anyone have a clue as to what may be going wrong?
Thanks!
======================
UPDATE
The image doesn't display although you can download it (file->save as) and save it to computer. Openeing it locally though won't work either which leads me to think that the image has been corrupted somehow. Anyone experienced something similar? I'm thinking maybe som php errors/warnings get injected into the stream and corrupts the image.
One source of possible issues is that the MIME type for JPEG images is image/jpeg, not image/jpg. This is a case where the type doesn't agree with the fairly-common, 3-character version of the file extension.
Some thoughts:
File is to big
File path causing problems
The right content-type for JPG images is "Content-type: image/jpeg".
Note that the T of type is lower case.
UPDATE
I don't know if it will be useful but try something like this:
$info=pathinfo($image->path);
$ext=strtolower($info["extension"]);
if($ext=="jpg") $ext="jpeg";
header("Content-type: image/$ext");
imagejpeg(imagecreatefromjpeg($image->path));

Categories