Danilo
Danilo

Reputation: 1

'imagecreatefrompng' is outputting a crashed image

I’m having problems with gd library in PHP when I try to do a imagecratefrompng. I’m running a script where the user input a text and it is added to a pre-created image. Problem is, when I output the image the image show as broken.

Can anyone help pointing if something is wrong with my script/image?

The image is a PNG, 600x956, 220kb file size.

GD Library is enabled. PNG, JPEG, GIF support are enabled.

Here is the code.

// Text inputed by user
  $text = $_POST['text'];
// Postion of text inputed by the user
  $text_x = 50;
  $text_y = 817;
// Color of the text
  $text_color = imagecolorallocate($img, 0, 0, 0);
// Name of the file (That is in the same directory of the PHP file)
  $nomeDaImagem = "Example";


$img = imagecreatefrompng($nomeDaImagem);

//Text is retrieved by Post method
imagestring($img, 3, $text_x, $text_y, $text, $text_color);

header("Content-type: image/png");
imagepng($img);

imagedestroy($img);

Upvotes: 0

Views: 2161

Answers (2)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

There are a number of issues with your script:

  1. You attempt to allocate the colour to the image, before you have actually created the image.
  2. Your string to be written is in the variable $nome, but you are printing $text.
  3. You don't check if $_POST['text'] exists, which may result in a Notice-level error.
  4. You don't check if the file exists, which may result in a Warning-level error.

Here's an example of your code, fixed:

// Text inputed by user 
  $nome = isset($_POST['text']) ? $_POST['text'] : "<Nothing to write>"; 
// Postion of text inputed by the user 
  $text_x = 50; 
  $text_y = 817; 
// Name of the file (That is in the same directory of the PHP file) 
  $nomeDaImagem = "Example"; 

$img = file_exists($nomeDaImagem)
   ? imagecreatefrompng($nomeDaImagem)
   : imagecreate(imagefontwidth(3)*strlen($nome)+$text_x,imagefontheight(3)+$text_y);

// Color of the text 
  $text_color = imagecolorallocate($img, 0, 0, 0); 
//Text is retrieved by Post method 
imagestring($img, 3, $text_x, $text_y, $nome, $text_color); 

header("Content-type: image/png"); 
imagepng($img); 
imagedestroy($img); 

Upvotes: 2

Abid Hussain
Abid Hussain

Reputation: 7762

Read more:--

http://php.net/manual/en/function.imagecreatefrompng.php

http://www.php.net/manual/en/function.imagecreatefromstring.php

or try this

<?php
function LoadPNG($imgname)
{
    /* Attempt to open */
    $im = @imagecreatefrompng($imgname);

    /* See if it failed */
    if(!$im)
    {
        /* Create a blank image */
        $im  = imagecreatetruecolor(150, 30);
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc  = imagecolorallocate($im, 0, 0, 0);

        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);

        /* Output an error message */
        imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
    }

    return $im;
}

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

$img = LoadPNG('bogus.image');

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

Upvotes: 0

Related Questions