Jump to content

Recommended Posts

so I used GD and created a png image, and then you press a "Save" button and it runs my save.php file,this file creates the image and then uses

 

<?php
Header('Content-type: image/png');
ImagePNG($im,$location);
ImageDestroy($im);
?>

 

to save it, the image shows up in the folder, but when I view it, it is just ablack box, my image wasn't really created. I can preview the image and see that it works, is there something I am missing? and..do you need more code to help me?

Link to comment
https://forums.phpfreaks.com/topic/67136-solved-saving-png-in-php/
Share on other sites

Here is part of my code..the images saves in teh folder, but none of the what was drawn on it saves, it just saves as the box with a black fill.

 


<?php

session_start();

if(isset($_SESSION['validUser'])){

$bgColor = $_GET['bgColor'];
$logo = $_GET['logo'];

$ln1[0] = $_GET['ln1_text'];
$ln1[1] = $_GET['ln1_font'];
$ln1[2] = $_GET['ln1_size'];
$ln1[3] = $_GET['ln1_color'];

$ln2[0] = $_GET['ln2_text'];
$ln2[1] = $_GET['ln2_font'];
$ln2[2] = $_GET['ln2_size'];
$ln2[3] = $_GET['ln2_color'];

$ln3[0] = $_GET['ln3_text'];
$ln3[1] = $_GET['ln3_font'];
$ln3[2] = $_GET['ln3_size'];
$ln3[3] = $_GET['ln3_color'];

$ln4[0] = $_GET['ln4_text'];
$ln4[1] = $_GET['ln4_font'];
$ln4[2] = $_GET['ln4_size'];
$ln4[3] = $_GET['ln4_color'];

$ln5[0] = $_GET['ln5_text'];
$ln5[1] = $_GET['ln5_font'];
$ln5[2] = $_GET['ln5_size'];
$ln5[3] = $_GET['ln5_color'];


function createImage($bgColor, $logo, $ln1, $ln2, $ln3, $ln4, $ln5){

//create canvas
$width = 500;
$height = 203;
$im = ImageCreateTrueColor($width,$height);
switch($bgColor){

case 'Black' :
  $canvas = ImageColorAllocate($im,0,0,0);
  break;
case 'White' :
  $canvas = ImageColorAllocate($im,255,255,255);
  break;
case 'Gray' :
  $canvas = ImageColorAllocate($im,150,150,150);
  break;
case 'Red' :
  $canvas = ImageColorAllocate($im,255,0,0);
  break;
case 'Dark Red' :
  $canvas = ImageColorAllocate($im,150,0,0);
  break;
case 'Light Green' :
  $canvas = ImageColorAllocate($im,0,255,0);
  break;
case 'Green' :
  $canvas = ImageColorAllocate($im,0,150,0);
  break;
case 'Dark Green' :
  $canvas = ImageColorAllocate($im,0,100,0);
  break;
case 'Blue' :
  $canvas = ImageColorAllocate($im,0,0,255);
  break;
case 'Yellow' :
  $canvas = ImageColorAllocate($im,255,255,0);
  break;
case 'Cyan' :
  $canvas = ImageColorAllocate($im,0,255,255);
  break;
case 'Purple' :
  $canvas = ImageColorAllocate($im,150,0,150);
  break;
case 'Violet' :
  $canvas = ImageColorAllocate($im,255,0,255);
  break;
case 'Pink' :
  $canvas = ImageColorAllocate($im,255,100,255);
  break;

}
ImageFill($im,0,0,$canvas);

//outline canvas
$black = ImageColorAllocate($im,0,0,0);
ImageRectangle($im,0,0,$width-1,$height-1,$black);

//insert logo



//Line Array
//
//ln[0] = text
//ln[1] = font
//ln[2] = size
//
//End Line Array

//format line 1
$font = 'c:/windows/fonts/'.$ln1[1].'.ttf';
switch($ln1[3]){

case 'Black' :
  $color = ImageColorAllocate($im,0,0,0);
  break;
case 'White' :
  $color = ImageColorAllocate($im,255,255,255);
  break;
case 'Gray' :
  $color = ImageColorAllocate($im,150,150,150);
  break;
case 'Red' :
  $color = ImageColorAllocate($im,255,0,0);
  break;
case 'Dark Red' :
  $color = ImageColorAllocate($im,150,0,0);
  break;
case 'Light Green' :
  $color = ImageColorAllocate($im,0,255,0);
  break;
case 'Green' :
  $color = ImageColorAllocate($im,0,150,0);
  break;
case 'Dark Green' :
  $color = ImageColorAllocate($im,0,100,0);
  break;
case 'Blue' :
  $color = ImageColorAllocate($im,0,0,255);
  break;
case 'Yellow' :
  $color = ImageColorAllocate($im,255,255,0);
  break;
case 'Cyan' :
  $color = ImageColorAllocate($im,0,255,255);
  break;
case 'Purple' :
  $color = ImageColorAllocate($im,150,0,150);
  break;
case 'Violet' :
  $color = ImageColorAllocate($im,255,0,255);
  break;
case 'Pink' :
  $color = ImageColorAllocate($im,255,100,255);
  break;

}

$box = ImageTTFBBox($ln1[2],0,$font,$ln1[0]);

$d = $box[2] - $box[0];

$align = $width/2 - $d/2;

ImageTTFText($im,$ln1[2],0,$align,45,$color,$font,$ln1[0]);

// Repeats for 4 more lines...



//draw some lines

ImageLine($im,0,150,$width,150,$black);
ImageLine($im,200,150,200,203,$black);
ImageLine($im,375,150,375,203,$black);




//Save to the database

//this will save the path and ID number etc

$link = mysql_connect('localhost', 'testUser', 'sra259') or die('Could not connect: ' . mysql_error());

mysql_select_db('iluvsocaldb') or die('Could not select database');

$userName = $_SESSION['validUser'];

$query_business = "SELECT businessID FROM business WHERE userName='$userName'";

$result_business = mysql_query($query_business) or die('Query failed: '.mysql_error());

//more database code.......


//where I create teh $locaton variable

$query_ID = "SELECT couponID FROM coupons WHERE businessID='$businessID' AND startDate='$date'";

$result_ID = mysql_query($query_ID) or die('Query failed: '.mysql_error());

$couponID = mysql_result($result_ID,0);

$location = $couponID . '.png';

$query_add = "UPDATE coupons SET location='$location' WHERE couponID='$couponID'";

$result_add = mysql_query($query_add) or die('Query failed: '.mysql_error());

//create and destroy
Header('Content-type: image/png');
ImagePNG($im,$location);
ImageDestroy($im);

//close database

mysql_close($link);

}//ends create image function

createImage($bgColor, $logo, $ln1, $ln2, $ln3, $ln4, $ln5);

}//ends isset function
else{

echo 'You are not authorized to view this page.';

}
?>

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.