Forum Moderators: coopster
I have this wonderful PHP script that generates gradient graphics dynamically and I would like to do the following:
1) Output the image.
2) Save the image to the folder "images/gradients/" using the name gradient_$height_$width_$color1_$color2.png, where the variables all represent their respective values.
3) The next time a user request that image, verify if the image already exists. If so, output image to the user. If not, create a new one.
4) Also, when the user sees the image, it would be great if the extension was PNG with the given name of the file (ie: gradient_400_300_FFF_000.png) instead of gradient_graphics.php?VAR1=Something%VAR2=Something Else, etc...
Here is my code:
<?php
// set the PNG header type for the request
header("Content-Type: image/png");
require_once('gradient_image_function.php');
// set variables
$height = $_GET["height"];
$width = $_GET["width"];
$color1 = $_GET["color1"];
$color2 = $_GET["color2"];// now check if an image created for of these dimensions
// is in the image cache
// if not, then we will have to make a new one
$image_path = "images/gradients/gradient_".$height."x".$width."_".$color1."_".$color2.".png";
if (file_exists($image_path)) {
// send the cached image to the output buffer (browser)
readfile($image_path);
} else {
// create a new image using GD functions
$image = new gd_gradient_fill($_GET[width],$_GET[height],vertical,$_GET[color1],$_GET[color2]);// send the new image to the browser
ImagePNG($image);// now save a copy of the new image to the cache directory
ImagePNG($image, $image_path);// destroy the image in memory to free up server resources
ImageDestroy($image);
}
?>
It requires a file called gradient_image_function.php, where I stored the function gd_gradient_fill, which lets me create the gradient graphics.
So my problem is that I can't really output this image as a .png file and even save it to a folder in my server. It would be great if I could cache this, since many users will request the same image over and over again, it could save some CPU from my servers if they could just output an image that had already been generated. And if that image does not exist in the servers, they simply create a new one. But the advantage of this technique is that the image will only be generated and saved once, not 1,000 times, depending on the number of visits my site gets. Plus, it won't reveal my server-side scripting language extension (.php). All they will see is a .png file, they won't have a clue that those images are being created dynamically.
Sorry if I haven't made myself clear, but if you don't understand what I mean to do with this script, please ask me. Thank you for reading this and hopefully you guys can help.
Thanks,
Cosmoyoda
// now save a copy of the new image to the cache directory
ImagePNG($image, $image_path);
so that it saves before sending to browser, although I can't say why I think that might make a difference.
Have you checked error logs to make sure it's not something silly like a directory that doesn't have write access or the like? Since you're not using an absolute path, it may not be trying to go where you think.
It still doesn't work. It doesn't save the images to a folder and after they are created it doesn't read them. It simply generates the dynamic image again and I always see the .php extension instead of .png extension. Do you know why this might not be working?
<?php
// set the PNG header type for the requestrequire_once('gradient_image_function.php');
// set variables
$height = $_GET["height"];
$width = $_GET["width"];
$color1 = $_GET["color1"];
$color2 = $_GET["color2"];// now check if an image created for of these dimensions
// is in the image cache
// if not, then we will have to make a new one$image_path = "images/gradients/gradient_".$height."x".$width."_".$color1."_".$color2.".png";
if (file_exists($image_path)) {
// send the cached image to the output buffer (browser)
header("Content-Type: image/png");//set content type
readfile($image_path);
} else {
// create a new image using GD functions
$image = new gd_gradient_fill($_GET[width],$_GET[height],vertical,$_GET[color1],$_GET[color2]);// now save a copy of the new image to the cache directory
ImagePNG($image, $image_path);// destroy the image in memory to free up server resources
ImageDestroy($image);chmod($image_path,0777); // change file permissions
header("Content-Type: image/png");//set content type
readfile($image_path);}
?>
// send the new image to the browser
ImagePNG($image);
I don't think it will make a difference either, but it's one of those doesn't-make-sense things I'll try in such a situation.
I don't see any reason that you're not able to save the file. Did you look at your server error log? Try setting error reporting to E_ALL to see if there's any warnings reported. Have you echoed the path to the browser to make sure it's what you expect? You might want to specify an absolute location & filename (/home/myname/public_html/test.png - wherever you save files on your server) so you know exactly where the file's going. Have you checked the return value of imagepng to make sure it's returning true? There's a couple of user contributed notes for imagepng [php.net] that you might want to look at. Also, just for investigative purposes, you might try imagegif or imagejpeg to see if it's an issue just with the imagepng function.