如何通过php存储转储信息[关闭]

I want to store images created by php in real time. Currently each image is destroyed after creation, but instead I want to store the images in an unique folder on my website.

I have seen many websites that use a dedicated dump folder, e.g. http://www.example.com/dump/4592898349.jpg, and I was wondering if someone could explain to me how I could do that.

This is my html code with the form that starts the image generation:

<html>
    <body>
        <form action="result.php" method="get">
            <input type="text" name="name" id="name">
            <input type="button" name="submit" id="submit">
        </form>
    </body>
</html>

And here is my php code, which creates the image with the given text:

<?php
    header('Content-type: image/jpeg');
    $jpg_image = imagecreatefromjpeg('Desert.jpg');
    $white = imagecolorallocate($jpg_image, 73, 41, 236);
    $font_path = 'OpenSans-Italic.TTF';
    $text = $_GET['name'] ;
    imagettftext($jpg_image, 25, 0, 75, 50, $white, $font_path, $text);
    imagejpeg($jpg_image);
    imagedestroy($jpg_image); 
?>

However, I still don't know where would be the best place to save the image.

change

imagejpeg($jpg_image);

to

imagejpeg($jpg_image,"imagefolderpath/image.jpg");

that will write the image to file, and not output the image...so...

$image_url="dump/".rawurlencode(trim($text)).".jpg";
imagejpeg($jpg_image,$image_url);
readfile($image_url);

So, not to be to strict on learners, the solution Marc B suggested was looking closer at the imagejpeg() function:

bool imagejpeg ( resource $image [, string $filename [, int $quality ]] )

So there is a second optional parameter $filename which, when given, will tell GD to store the image in that file.

BUT it also says, that if you pass a filename, that the image is not automatically output to the browser, as it happens if you don't pass second parameter.

So there are three steps that you will have to accomplish:

  1. Create unique filename

  2. Save image to file

  3. Display image in browser

Now the simplest solution would be:

/* --- create unique filename --- */

// get name passed or set default, this will be prepended to
// the unique filename to make it more readable
$prepend_filename = (isset($_GET['name']) ? rawurlencode(trim($_GET['name'])) : 'ing';

// set to the path where you want your images stored
$dirname = "/your/base/path/wherever/";
// use a while loop to make sure or new filename does not exist
while (true) {
    // now build unique filename
    $filename = uniqid($prepend_filename, true) . '.jpg';
    // if filename is unique (file does not exist) then exit loop 
    if (!file_exists($dirname . $filename)) break;
}

/* --- save image to file --- */

imagejpeg( $jpg_image, $dirname . $filename );

/* --- output image to browser --- */

readfile($dirname . $filename);

So this would basically work. The only thing that is not so nice about this solution is, that first you are accessing disk to write the image and then you have to re-access the file to output it to the browser. There is another technique that you could use to reduce file access:

/* --- output image to browser ---*/

// use output buffering to capture outputted image stream
ob_start();
// output the image to output buffer
imagejpeg($jpg_image);
// stop capture, flush output to browser and save as string
$img_data = ob_get_flush(); 

/* --- save image to file --- */

$fp = fopen($dirname . $filename, 'w');
fwrite($fp, $img_data);
fclose($fp);

So the advantage of this is, that you will not have to access disk twice.

If you don't want to return the image as raw image format, but actually want to display it right away in your html page, there are two methods.

Method 1 (Removed comments on the repeated parts from above) Here we create the image, save it file and generate html output with the filename we created.

<?php

/* --- create unique filename --- */

$prepend_filename = (isset($_GET['name']) ? rawurlencode(trim($_GET['name'])) : 'ing';
$dirname = "/your/base/path/wherever/";
while (true) {
    $filename = uniqid($prepend_filename, true) . '.jpg';
    if (!file_exists($dirname . $filename)) break;
}

/* --- save image to file --- */

imagejpeg( $jpg_image, $dirname . $filename );

// now the difference starts

?><html>
    <head>
        <title>Your title here</title>
        <!-- the rest of your head here -->
    </head>
    <body>
        <img src="http://yourserver.com/your/base/path/wherever/<?php
            echo $filename; 
        ?>" width="100%" />
    </body>
</html>   

Method 2 The start is the same as above, difference starts when saving image, we use the output buffering method again, but this time we pass the output to img tag as base64 encoded string. Nasty, nasty :)

/* Generating the filename is the same as above (not copied here).
   Output buffering same as prior sample, with one difference:
   Now we use ob_get_clean() instead of ob_get_flush() because
   we do not want to output image.
*/
ob_start();
imagejpeg($jpg_image);
$img_data = ob_get_clean();  // NOTE: ob_get_clean() not ob_get_flush()

/* --- save image to file --- */

$fp = fopen($dirname . $filename, 'w');
fwrite($fp, $img_data);
fclose($fp);

?><html>
    <head>
        <title>Your title here</title>
        <!-- the rest of your head here -->
    </head>
    <body>
        <img src="data:image/jpeg;base64,<?php
            echo  base64_encode( $img_data );
        ?>" width="100%" />
    </body>
</html>   

So here we used the buffered $img_data for writing the file to disk and for directly outputting it in the html page without browser having to call file from server (but with larger html source obviously)