how do I name an image when I create it?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
So far I've got the code you see below. I've not yet figured out how
to change the name of the file. I'm creating several images of
different sizes. I need to give them all different names. How do I
control the name?

function makeImage($pathToImage=false, $desiredSize=false) {
    if (file_exists($pathToImage)) {
        if ($desiredSize) {
            // 04-26-03 - This whole next section is off limits till I find out
how to test for image support in the
            // PHP build in use - my own hosting company doesn't offer support
for functions like imagecreatefromjpeg.
            // 05-25-03 - function_exists() is the correct way to test.
            // 07-22-04 - when images are uploaded, a thumbnail should be
            // 11-16-04 - I'm going to try to revive this function today and
make it operational.
            // It's been in the code but unused for a year and a half.
            // Since most PHP installations don't have the GD library, I think
the way to go is
            // to simply test for every function.
            $controllerForAll = & getController();
            $resultsObject = & $controllerForAll->getObject("McResults",
            if (function_exists("getimagesize")) {
                if (function_exists("imagecreate")) {
                    if (function_exists("imagecreatefromjpeg")) {
                        if (function_exists("imagecopyresized")) {
                            if (function_exists("imagesx") && function_exists("imagesy")) {
                                if (function_exists("imagejpeg")) {
                                    if (function_exists("imagecreatefromgif")) {
                                        if (function_exists("imagegif")) {
                                            $size = getimagesize($pathToImage);
                                            // 04-13-03 - Now we make sure we only output the jpgs and
                                            // 04-13-03 - this next line is to find out if we are
outputting jpg or gif
                                            // WE ONLY OUTPUT JPG AND GIF
                                            if ($size[2] == 1 || $size[2] == 2) {
                                                $oldWidth = $size[0];
                                                $oldHeight = $size[1];
                                                // 11-20-04 - we reset the width to whatever the desired
size is.
                                                // standardImageInsert calls this function 4 times with
values of
                                                // 50, 100, 250, and 400. We then compare the desired size
to the
                                                // old width to get a ratioOfChange which we can then
apply to
                                                // the height to get a propotional change. The division
                                                // leave us with a float, but we need an integer for the
                                                // height value, so we hit the result with round().
                                                $newWidth = $desiredSize;
                                                $ratioOfChange = $oldWidth / $desiredSize;
                                                $newHeight = $oldHeight / $ratioOfChange;
                                                $newHeight = round($newHeight);
                                                $newImage = imagecreate($newWidth, $newHeight);
                                                if ($size[2] == 2) {
                                                    $srcImage = imagecreatefromjpeg($image);
                                                    imagecopyresized($newImage, $srcImage, 0, 0, 0, 0,
$newWidth, $newHeight, imagesx($srcImage), imagesy($srcImage));
                                                } elseif ($size[2] == 1) {
                                                    $srcImage = imagecreatefromgif($image);
                                                    imageCopyResized($newImage, $srcImage, 0, 0, 0, 0,
$newWidth, $newHeight, imagesx($srcImage), imagesy($srcImage));
        } else {
            $resultsObject->error("In makeImage, the second parameter should
have told us what width was desired for this image, but it did not.",
    } else {
        if ($pathToImage == "") {
            $resultsObject->error("In makeImage(), the first parameter should
have told us where to find this image (what path to it), but it did
not.", "makeImage");
        } else {
            $resultsObject->error("In makeImage(), we were told to resize an
image called '$pathToImage' but the image didn't seem to exist on the
server.", "makeImage");

Re: how do I name an image when I create it?

lawrence wrote:
Quoted text here. Click to load it

The imagejpeg/gif/png ake an optional  filename argument and output the
jpeg image to that file if it is given.
bool imagejpeg ( resource image [, string filename [, int quality]])
Your function can take the filename as an argument and store the image
to the fiven filename.
For example imagejpeg($image,$fname."-fmt-jpg".'jpg')

Other than that, you have far too many nested ifs .. cannot help  but
think "d00d, you are screwed"

Re: how do I name an image when I create it?

Quoted text here. Click to load it

What's a better way to handle the situation? You and I both know that
90% of the people out there won't be able to use this code because the
PHP GD library won't be installed. What is your concern about this
code? That it will run slowly? I'm under the impression that
function_exists() is a fast function. It's not as if it needs to do
any complex calculations.

Still, if you know of a more concise way to guarantee that my fuction
will never call a PHP function that is not installed, I'd like to know

One of the web design companies I work with has their servers with
Interland. The server setup is a nightmare. It's running Free BSD but
its a virtual environment such that its been hard for our local linux
gurus to upload a new RPM of PHP. For that reason they are still
running PHP 4.0.6  on their servers. For that reason, since they are
my biggest client, when I use functions in GD or PHP > 4.0.6 I tend to
test for the existence of the function.

It's a bad situation, but how else to deal with it? They are moving
all their clients to some new servers they've leased through
Rackspace, and Rackspace offers a much better setup. Till then, we
need to proceed carefully, writing PHP code that looks like Javascript
code, in the sense that we test excessively for the existence of

Re: how do I name an image when I create it?

lawrence wrote:
Quoted text here. Click to load it

My concern is that the code is too complex.  But that maybe just
because I have a small brain :-)

Quoted text here. Click to load it

Yes, I think you can handle the above code more elegantly.  From a
cursory glance it seems that

- All the functions (imagesx/y, imagecreatefromjpeg, imagejpeg etc.)
*must* exist to make your function work correctly.

- you call a function *only* when it is guranteed to exist

- You do not have an else part i.e. if some function is not found, you
bail out.

My solution is as follows

function do_required_image_functions_exist()
if (
function_exists(getimagesize) &&
function_exists(getimagecreate) &&
function_exists(magecreatefromgif) &&
return true;
return false;

function makeImage($pathToImage=false, $desiredSize=false)
if (do_required_image_functions_exist())


Quoted text here. Click to load it

In a vhost,  you can compile and install your own version of PHP.  Why
do you need RPMs for FreeBSD.  maybe you should contact your hosting
provider for further guidance.  From what I know, it can be done but

Re: how do I name an image when I create it? (lawrence) wrote in message
Quoted text here. Click to load it

   1. Using md5() on parameters. eg. $image_name =

  <?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com

Site Timeline