user2250371
user2250371

Reputation: 13

PHP display png image from binary file

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

Upvotes: 1

Views: 1376

Answers (1)

leonbloy
leonbloy

Reputation: 75946

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.

Upvotes: 3

Related Questions