stangen Posted January 21, 2014 Share Posted January 21, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285564-save-dynamic-php-image/ Share on other sites More sharing options...
Ch0cu3r Posted January 21, 2014 Share Posted January 21, 2014 (edited) 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 January 21, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285564-save-dynamic-php-image/#findComment-1466090 Share on other sites More sharing options...
stangen Posted January 21, 2014 Author Share Posted January 21, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/285564-save-dynamic-php-image/#findComment-1466092 Share on other sites More sharing options...
Ch0cu3r Posted January 21, 2014 Share Posted January 21, 2014 (edited) 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 January 21, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285564-save-dynamic-php-image/#findComment-1466093 Share on other sites More sharing options...
Barand Posted January 21, 2014 Share Posted January 21, 2014 If you insert a new record in the database first you can grab the auto_increment id of that record (mysqli_insert_id() ) then save the image to the file system using that id as the image name Quote Link to comment https://forums.phpfreaks.com/topic/285564-save-dynamic-php-image/#findComment-1466094 Share on other sites More sharing options...
Solution stangen Posted January 21, 2014 Author Solution Share Posted January 21, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285564-save-dynamic-php-image/#findComment-1466096 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.