1

I have binary file where is stored image, i try get this image, but display only black color image, what can be wrong with my code or binary file.

<?php
function LoadPNG ($imgname) {
    $im = @imagecreatefrompng ($imgname); 

    if (!$im) { 
        $im= imagecreate (150, 30); 
        $bgc = imagecolorallocate ($im, 255, 255, 255);
        $tc= imagecolorallocate ($im, 0, 0, 0);
        imagefilledrectangle ($im, 0, 0, 150, 30, $bgc);

        imagestring ($im, 1, 5, 5, "Error loading $imgname", $tc);
    }
    return $im;
}

header('Content-Type: image/png');

$img = LoadPNG('452');

imagepng($img);
imagedestroy($img);
?>

with this code I get I error that can't load file

File: testams.serveriai.lt.lazdynas.serveriai.lt/452 Script: testams.serveriai.lt.lazdynas.serveriai.lt/crypt.php

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • This may help you http://stackoverflow.com/questions/2070603/php-recreate-and-display-an-image-from-binary-data – Pooshonk Apr 11 '13 at 08:27
  • I can see the "Error loading 452" image on your script page in Safari. Is it only breaking in a particular browser? – Matt Gibson Apr 11 '13 at 08:32
  • @MattGibson it't in all browsers – user2250371 Apr 11 '13 at 08:33
  • @Pooshonk I think my binary file is not coded in base64 – user2250371 Apr 11 '13 at 08:34
  • Did you try fetching the file via php? (f.e. using http://php.net/manual/en/function.file-get-contents.php) – Chris Apr 11 '13 at 08:37
  • @Chris try, but get only symbols – user2250371 Apr 11 '13 at 08:43
  • Can you try getting the file via file_get_contents and use ``imagecreatefromstring`` instead of ``imagecreatefrompng`` ? – Chris Apr 11 '13 at 08:46
  • 1
    Do you get any helpful errors if you stop suppressing the error output from `imagecreatefrompng` with `@`? Do you see the right file path if you print out @imgname? (You may need to temporarily stop sending the Content-Type header for easier debugging; leaving it in will probably prevent you seeing errors in the browser.) – Matt Gibson Apr 11 '13 at 08:50
  • @MattGibson I get this `Warning: imagecreatefrompng(): '452' is not a valid PNG file in /home/testams/domains/testams.serveriai.lt/public_html/crypt.php on line 3 �PNG IHDR��,uPLTE���U��~iIDAT(�c` ������������&��@���N�-�%y�)�猧,��LWf�LY��!'Ш�S�t`���ӂe�̓Pu�������`I���@�߀��D����2߇6sIEND�B`�' – user2250371 Apr 11 '13 at 09:01
  • Guess imagecreatefrompng wants the correct image header which is not given. You're only outputting the source of the image file (on the given link) and not the image itself, which is definitely a header problem. imagecreatefromstring could work in this case or adding the correct header to the script that outputs the image. – Chris Apr 11 '13 at 09:16
  • What is the path you're passing in $imgname? Are you loading it as a URL (using http://...)? If so, you need to fix the headers the web server sends; it's encoding your file as `text/plain`. It might be enough simply to rename it as 452.png; many web servers will guess the Content-Type they should send based on file extension. – Matt Gibson Apr 11 '13 at 09:23
  • Your PNG is invalid, if you open it with any image viewer or image editor, it won't even be able to open it, so how in the world would PHP be able to ? – HamZa Apr 11 '13 at 09:24
  • Also -- having grabbed your image file, it looks invalid. ImageMagick's `identify` says "improper image header". Are you *sure* that's a valid PNG you've got there? If it loads okay for you locally, then something's killing it on the way out of the door. – Matt Gibson Apr 11 '13 at 09:25

1 Answers1

3

Your PNG image is corrupt, it has a \n character instead of \r\n, (byte position 5) typically a problem arising from FTP transfering a binary image in text mode from Windows to Unix.

Before messing with PHP, you should check simply that the image is OK, eg adding the .png extension, placing it in a visible folder (in the web server) and browsing it.

leonbloy
  • 73,180
  • 20
  • 142
  • 190