Jump to content

saving an output png file from imagecreatefrompng (error in saving)


yekis

Recommended Posts

  currently i have two files

 

  1) workingscript.html --> html form

  2) redirection.php --> page where the created (png) file is displayed on the browser...

 

  problem (@ redirection.php): when i try to save the created image (png) file in my hard disk (right click > save picture as) ....i cannot to preview/open the png file that i just earlier saved in my hard disk

 

     

      See  figure_1.jpg                  

              output png image at redirection.php

               

        See figure_2.jpg  

              Saving the image as outputPNGImage.png in my hard disk

 

      See figure_3.jpg         

              There was an error displaying outputPNGImage.png that was just earlier saved in my hard disk

 

anyway, here's my code

 

1) workingscript.html

 

 

<html>
<head>


<SCRIPT LANGUAGE="JavaScript">

function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);

else 
countfield.value = maxlimit - field.value.length;
}

</script>

<meta name="author" content="Rexcel Cariaga">
<link href="xampp.css" rel="stylesheet" type="text/css">
<title>Buy and Sell @ Cube iTV</title>


</head>
<body>
<img src="background.png">
<form name="ff" action="redirection.php" method="post">
<table width="400" border="0">
<tr>
<td width="200"></td>
<td width="200"></td>
</tr>
<tr>
<td>First Name of Seller</td>
<td><input name="text" type="text" size="25" maxlength="14"></td>
</tr>
<tr>
<td><p>Contact Number</p>
</td>
<td><label>
<input name="contact" type="text" id="contact" value="091XXXXXXXX" size="25" maxlength="12">
</label></td>
</tr>
<tr>
<td>Product Name</td>
<td><label>
<input name="product" type="text" id="product" size="25" maxlength="25">
</label></td>
</tr>
<tr>
<td>Description</td>
<td><label>

<font size="1" face="arial, helvetica, sans-serif"> ( You may enter up to 100 characters. )<br>
<textarea name="description" id="description" wrap=physical cols=28 rows=4 onKeyDown="textCounter(this.form.description,this.form.remLen,100);" onKeyUp="textCounter(this.form.description,this.form.remLen,100);"></textarea>

<br>
<input readonly type=text name=remLen size=3 maxlength=3 value="100"> characters left</font>
<br>
</label></td>
</tr>
<tr>
<td>Price (PHP)</td>
<td><label>
<input name="price" type="text" id="price" size="25" maxlength="7">
</label></td>
</tr>
<tr>
<td> </td>
<td><center><input type="submit" name="submit" value="Submit">
</center></td>
</tr>
</table>
</form>
<p>
</body>
</html>

 

2) redirection.php

 

<?php
### when the user presses the submti button then use gd functions... 
if (isset($_POST['submit'])) 
{
header("Content-type: image/png");

$name = $_POST['text']; # get value for name...
$contact = $_POST['contact']; # get value for contact...
$product = $_POST['product']; # get value for product...
$price = $_POST['price']; # get value for price...
$description = $_POST['description']; # get value for price...

$im     = imagecreatefrompng("background.png");
$white = imagecolorallocate($im, 255, 255, 255);

#loads font 
   $font = "ariblk.ttf"; 

# this is for product 
     // imagestring($im, 5, 190, 20, $product, $white);
 imagettftext($im, 18, 0, 10, 40, $white, $font, $product);
     
# this is for description...

  $text = $description;
  $new_text = wordwrap($text, 15, "\n", true);
  imagettftext($im, 18, 0, 10, 90, $white, $font, $new_text);

# this is for name
    //imagestring($im, 5, 300, 300, $name, $white);
     imagettftext($im, 14, 0, 300, 315, $white, $font, $name);

# this is for contact number
//imagestring($im, 5, 300, 320, $contact, $white);
     imagettftext($im, 14, 0, 300, 340, $white, $font, $contact);

# this is for price
//imagestring($im, 5, 370, 80, $price, $white);
   imagettftext($im, 17, 0, 363, 88, $white, $font, $price);

imagepng($im);
imagedestroy($im);
}
?>


<html>
<head>


<meta name="Rexcel Cariaga" content="Interactive PNG Creation" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

 

 

  i have also attached background.png that i'm currently using in my script..tnx!!

             

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

 

you can not output html into the image..

<?php
### when the user presses the submti button then use gd functions... 
if (isset($_POST['submit'])) 
{
header("Content-type: image/png");

$name = $_POST['text']; # get value for name...
$contact = $_POST['contact']; # get value for contact...
$product = $_POST['product']; # get value for product...
$price = $_POST['price']; # get value for price...
$description = $_POST['description']; # get value for price...

$im     = imagecreatefrompng("background.png");
$white = imagecolorallocate($im, 255, 255, 255);

#loads font 
   $font = "ariblk.ttf"; 

# this is for product 
     // imagestring($im, 5, 190, 20, $product, $white);
 imagettftext($im, 18, 0, 10, 40, $white, $font, $product);
     
# this is for description...

  $text = $description;
  $new_text = wordwrap($text, 15, "\n", true);
  imagettftext($im, 18, 0, 10, 90, $white, $font, $new_text);

# this is for name
    //imagestring($im, 5, 300, 300, $name, $white);
     imagettftext($im, 14, 0, 300, 315, $white, $font, $name);

# this is for contact number
//imagestring($im, 5, 300, 320, $contact, $white);
     imagettftext($im, 14, 0, 300, 340, $white, $font, $contact);

# this is for price
//imagestring($im, 5, 370, 80, $price, $white);
   imagettftext($im, 17, 0, 363, 88, $white, $font, $price);

imagepng($im);
imagedestroy($im);
exit; //<--ADDED this stops the html loading after the post!
}
?>


<html>
<head>


<meta name="Rexcel Cariaga" content="Interactive PNG Creation" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

Link to comment
Share on other sites

thanks but it didn't work....(i simply copied and paste the php code you gave and made no modifications...or should i modify it in one way or another)

 

btw, here's the a part of a php code (iart.php) that i got from a site that enabled me to saved the created image....i believe that this code just simply displays the created image  (i just don't know how to modify it to suit my php code)..hehe..sorry..php newbie here...

 

 

<p>
	<h1><?php echo $TEXT['iart-head']; ?></h1>

	<img width="520" height="320" src="iart.php?img=1&text=<?=urlencode($text)?>" alt=""><p class="small">
	<?=$TEXT['iart-text1']?><p>

 

 

full source for code iart.php

 

<?php



		$text=$_REQUEST['text'];
        if($text=="") $text="";
        if($_REQUEST['img']!=1)
        {
?>
<html>
<head>
	<meta name="author" content="Kai Oswald Seidler">
	<link href="xampp.css" rel="stylesheet" type="text/css">
	<title></title>
</head>

<body>
	 <p>
	<h1> </h1>

<img width="520" height="320" src="iart.php?img=1&text=<?=urlencode($text)?>" alt="">
	<p class="small">
    
<p>
	<form name="ff" action="iart.php" method="get">
		<input type="text" name="text" size="30">
		<input type="submit" value="Submit">
	</form>
	<p>
    
</body>
</html>
<?php
	exit;
}

$fontfile = "./AnkeCalligraph.TTF";

$size = 9;
$h = 320;
$w = 520;

$im  =  ImageCreate($w, $h);

$fill = ImageColorAllocate($im, 251, 121, 34);
$light = ImageColorAllocate($im, 255, 255, 255);
$corners = ImageColorAllocate($im, 153, 153, 102);
$dark = ImageColorAllocate($im, 51, 51 , 0);
$black = ImageColorAllocate($im , 0, 0 , 0);

$colors[1] = ImageColorAllocate($im, 255, 255, 255);
$colors[2] = ImageColorAllocate($im, 255 * 0.95, 255 * 0.95, 255 * 0.95);
$colors[3] = ImageColorAllocate($im, 255 * 0.9, 255 * 0.9, 255 * 0.9);
$colors[4] = ImageColorAllocate($im, 255 * 0.85, 255 * 0.85, 255 * 0.85);

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

srand(time());

$c = 1;
$anz = 10;
$step = (4 / $anz);
for ($i = 0; $i < $anz; $i += 1) {
	$size = rand(7, 70);
	$x = rand(-390, 390);
	$y = rand(-100, 400);
	$color = $colors[$c];
	$c += $step;
	ImageTTFText($im, $size, 0, $x, $y, $color, $fontfile, $_GET['text']);
}

ImageLine($im, 0, 0, $w - 1, 0, $light);
ImageLine($im, 0, 0, 0, $h - 2, $light);
ImageLine($im, $w - 1, 0, $w-1, $h, $dark);
ImageLine($im, 0, $h - 1, $w - 1, $h - 1, $dark);
ImageSetPixel($im, 0 , $h - 1, $corners);
ImageSetPixel($im, $w - 1, 0, $corners);

ImagePNG($im);
exit;
?>

 

so basically, that's the code that i got from a certain site that allows the users to create their own "interactive text art" image and enables them to save it on their local hard disk

 

i just don't know how incorporate/modify the php code in my script so the users can also save the created image in my php file...

 

tnx for reply guys, i'm really learning here!!!

Link to comment
Share on other sites

you can not output html into the image..

 

you need to use a php script in replacement of an image BUT you still need to write the HTML to use it as an image ie

<?php
header("Content-type: image/png");

$name = $_GET['text']; # get value for name...

$im     = imagecreatefrompng("background.png");
$white = imagecolorallocate($im, 255, 255, 255);

#loads font 
   $font = "ariblk.ttf"; 

# this is for name
     imagettftext($im, 14, 0, 300, 315, $white, $font, $name);

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

 

save that as image.php

 

now

the image to use that would be

<img src="image.php?name=Hello">

 

**untested

 

Link to comment
Share on other sites

If you want to save the image, shouldnt it be

 

imagepng($im,"test");

 

example of my old script

 

<?php
$im = imagecreate(100, 100);

// Get coordinates
$x1 = imagesx($im) / 2;
$y1 = imagesy($im) / 2 ;
// Shuffle
$green = rand(10,200);
$red = rand(50,200);
$blue = rand(20,200);
// sets background to red
$background = imagecolorallocate($im, 255, 0, 0);
$green      = imagecolorallocate($im,$green,$red,$blue); 
$red      = imagecolorallocate($im,$green,$red,$blue); 

// Set text color
$text    = imagecolorallocate($im,0,0,110);
// Set a string
$string = imagestring($im,5,10,45,"H e l l o",$text);
// line
$line   = imageline($im,0,10,100,100,$green);
$line   = imageline($im,0,$green,10,100,$red);
// allocate some colors
$white2 = imagecolorallocate($im, 255, 255, 255);
$red2   = imagecolorallocate($im, 255,   0,   0);
$green2 = imagecolorallocate($im,   0, 255,   0);
$blue2  = imagecolorallocate($im,   0,   0, 255);

// draw the head
imagearc($im, 100, 100, 200, 200,  0, 360, $white2);
// mouth
imagearc($im, 100, 100, 150, 150, 25, 155, $red2);
// left and then the right eye
imagearc($im,  60,  75,  50,  50,  0, 360, $green2);
imagearc($im, 140,  75,  50,  50,  0, 360, $blue2);


imagepng($im,"testing",9);
?> 

 

 

 

edit: NVM, lol, I didnt read the "imagecreatefrompng" part.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.