Jump to content

Save dynamic php image


stangen
Go to solution Solved by stangen,

Recommended Posts

I've been working off some code I found and have made it work for my needs.

 

What it does is overlays text over an image using DTR

 

I've setup a form that uses the GET method to change the text color, text, font etc.

 

so the image src looks like <img src="preview.php?text=SampleTextcolor=#FF0000">

 

I am looking to figure out how to save that image and assign it a auto-increment ID number

 

But being that the image is dynamic, I can not figure out how to do so. Any help on this matter would be a appreciated.

Link to comment
Share on other sites

I assume your preview.php script uses GD Image library to generate the image.

 

Basically you need to save the image to the filesystem first  and then save the image path to the database. Once that is done you can output the generated image.

 

Something like

.... your current code for generating the image here ....

// ----- THIS IS WHAT YOU WILL NEED TO DO NEXT -----

// before outputting the image you need to,
// 1. generate a random name 
// 2. save image to filesystem and then 
// 3. save filepath in the database

// where on the server to save the image
$image_dir = 'images/';

// generate a random name for the image
// function taken from php.net/manual/en/function.mt-rand.php#112889
function mt_rand_str ($l, $c = 'abcdefghijklmnopqrstuvwxyz1234567890') {
    for ($s = '', $cl = strlen($c)-1, $i = 0; $i < $l; $s .= $c[mt_rand(0, $cl)], ++$i);
    return $s;
}
$image_name = mt_rand_str(10) . '.jpg';

// the full image file path
$image_path = $image_dir. $image_name;

// save the image to filesystem
imagejpeg($image, $image_path);

// save image to database
mysql_query("INSERT INTO image_table SET image = '$image_path'");

// now output the generated image
imagejpeg($image);
Edited by Ch0cu3r
Link to comment
Share on other sites

 

I assume your preview.php script uses GD Image library to generate the image.

 

Basically you need to save the image to the filesystem first  and then save the image path to the database. Once that is done you can output the generated image.

 

Something like

.... your current code for generating the image here ....

// ----- THIS IS WHAT YOU WILL NEED TO DO NEXT -----

// before outputting the image you need to,
// 1. generate a random name 
// 2. save image to filesystem and then 
// 3. save filepath in the database

// where on the server to save the image
$image_dir = 'images/';

// generate a random name for the image
// function taken from php.net/manual/en/function.mt-rand.php#112889
function mt_rand_str ($l, $c = 'abcdefghijklmnopqrstuvwxyz1234567890') {
    for ($s = '', $cl = strlen($c)-1, $i = 0; $i < $l; $s .= $c[mt_rand(0, $cl)], ++$i);
    return $s;
}
$image_name = mt_rand_str(10) . '.jpg';

// the full image file path
$image_path = $image_dir. $image_name;

// save the image to filesystem
imagejpeg($image, $image_path);

// save image to database
mysql_query("INSERT INTO image_table SET image = '$image_path'");

// now output the generated image
imagejpeg($image);

Thanks for the help with this.

 

I do not want every image generated to do this, as there will be several generated before the user pics the one they like.

 

Is there a way to do this in a separate functoin? Such as maybe a form submit? 

Link to comment
Share on other sites

You could store the generated image in a session var instead. You'd then only save the image in the database once the user is happy with the image.

 

 

Is there a way to do this in a separate functoin? Such as maybe a form submit?

Yes you can use a form for allowing the user to choose the text and color. Are you not using a form currently?

Edited by Ch0cu3r
Link to comment
Share on other sites

  • Solution

You could store the generated image in a session var instead. You'd then only save the image in the database once the user is happy with the image.

 

Yes you can use a form for allowing the user to choose the text and color. Are you not using a form currently?

 

 

I am using a form, however, the form is to generate the preview, and on the same page I have a separate form to submit an order using the POST method. Right now I have it set to pull the data from the first form into hidden input and save the data into the database with the order. I suppose I could just create a separate PHP file to re-generate the preview from the data in the DB in the admin section, no file saving needed.

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.