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.
What would be the error shown? There is something in the logs?
– Inkeliz
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.
– Filipe Moraes
friend am new in php and in programming more appears the black screen with a comic in the center
– Roger Moraes de Moura
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]
– Victor Stafusa
[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.
– Victor Stafusa