Error showing image with GD

Asked

Viewed 410 times

-4

I’m trying to display a PNG image on the client’s browser screen using PHP, but only a black screen with a comic in the center appears.

What am I doing wrong? Here’s my code:

<?php

        $file  = "img.png";
        $width = 500;
        $height= 500;

        list($width_origin,$height_origin) = getimagesize($file);
        $ratio = $width_origin / $height_origin;

        if ($width/$height > $ratio) {
            $width = $height * $ratio;
        }else{
            $height = $width / $ratio;
        }

        $image_final  = imagecreatetruecolor($width, $height);
        $imagem_origin= imagecreatefrompng($file);
        imagecopyresampled($image_final,$imagem_origin,0,0,0,0,$width,$height,$width_origin,$height_origin);
        header("Content-type: image/png");
        imagepng($image_final,"imagem_nova.png");


    ?>
  • What would be the error shown? There is something in the logs?

  • Welcome to Stackoverflow! Please explain the problem better, and if possible include a example of code that reproduces what is happening, because your question is not perceptible. See Help Center How to Ask.

  • friend am new in php and in programming more appears the black screen with a comic in the center

  • Friend, I edited your question to try to avoid its closure (already has 4 negative votes and 4 closing). If it wasn’t for the answer of the colleague below, I would also have closed it, because looking at your text, I didn’t understand what you were asking (I only understood with the answer), since you weren’t asking anything at all. You just said you made a mistake (without saying what the mistake was) and nothing talked about the code or what you’re trying to do. [continues]

  • [continuation] Well, I edited your question to put some text and maybe with that it can remain open. But next time, try to formulate it better to avoid annoyances of this kind, which are very common on this site, and endeavor to describe well the problem you are having and value a good essay. There are many questions here that would be good, but end up closed because who posted it did not know how to express himself right and other users did not understand the question. Tip.

1 answer

2


If you put the second parameter in imagepng it will not display but save, as described in the documentation http://php.net/manual/en/function.imagepng.php

If not set or NULL, the raw image stream will be outputted directly.

That is if the second parameter is not defined or is NULL it will display, otherwise it will save to a file.

So whenever you have questions, do a search in the documentation and read how to use each parameter and see the examples. In case your code should look like this:

<?php

$file  = "img.png";
$width = 500;
$height= 500;

list($width_origin, $height_origin) = getimagesize($file);

$ratio = $width_origin / $height_origin;

if ($width/$height > $ratio) {
    $width = $height * $ratio;
}else{
    $height = $width / $ratio;
}

$image_final  = imagecreatetruecolor($width, $height);
$imagem_origin= imagecreatefrompng($file);
imagecopyresampled($image_final,$imagem_origin, 0, 0, 0, 0, $width, $height, $width_origin, $height_origin);
header("Content-Type: image/png");

imagepng($image_final); //Exibe a imagem

If you want to view and save at the same time, you can use various methods, such as saving to a stream or file directly:

  • Using readfile:

    if (imagepng($image_final, $file)) //salva para um arquivo
    {
        header("Content-Type: image/png");
        readfile($file);
    } else {
        echo 'Erro ao salvar';
    }
    
  • fopen+stream_get_contents

    This is a little more complicated, but if you need to do some manipulation in the stream it might be interesting to use it.

    $handle = fopen($handle, 'wb');
    
    if (!$handle) {
        die('Erro ao abrir o arquivo para escrita');
    }
    
    if (imagepng($image_final, $handle)) //salva para um arquivo
    {
        header("Content-Type: image/png");
        echo stream_get_contents($handle);
    } else {
        echo 'Erro ao salvar';
    }
    
    fclose($handle);
    

What each parameter does:

The function is basically

bool imagepng ( resource $image [, mixed $to [, int $quality [, int $filters ]]] )
  • $image

    Must be a Resource of an image, returned by image creation functions, for example imagecreatetruecolor(), imagecreatefrompng(), etc..

  • $to

    The path or an open stream (which will be automatically closed when the function returns) to save the file. If not set or is NULL, the image will be displayed in the output.

  • $quality

    Compaction level: if 0 (no compaction) up to 9.

  • $filters

    Allows you to reduce the size of the PNG file. This is a bitmask which may define any combination of constant PNG_FILTER_XXX. Use PNG_NO_FILTER or PNG_ALL_FILTERS or using them respectively will disable or enable all filters.

  • I understood yes there is with the 2 parameter more without it equal does not appear the image, only appears the black screen and a square in the small center

  • yes there is the second parameter worked, will the embedded server in php7 supports that library

  • https://drive.google.com/open?id=0Bwln2qIiY0sYXzh3YkRDRTlvck0 Look who it is. It’s the print

  • All image code with multiple queries appears

  • PNG IHDR�8"�� IDATx��i�$G�%�T�����:P@c�{zv���B!�� /�P(2��N�4�P��Wn����4�="� =M!?��� nn��O�M���7+����-4��e�o|��M}P������;? /n��|

  • only the first line put

  • @Rogermoraesdemoura has some spacing before PNG IHDR?

  • PNG IHDR is like this

  • only with a [] in the middle of it

  • For if you look well you are like this ? PNG [] IHDR if you count it as space has one after png and one before the IHDR

  • @Rogermoraesdemoura does so, exchange /*header("Content-Type: image/png");*/ for header("Content-Type: text/plain"); (remove the /* and */ also) and then check if there’s any space or line breaks, like a spacing or something like that

  • opened with gedit has a large space after PNG

  • @Rogermoraesdemoura and before anything right? Then send me the file you used in $file = "img.png"; by googledrive

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.