how do I name an image when I create it?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lawrence

    how do I name an image when I create it?

    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($path ToImage=false, $desiredSize=fa lse) {
    if (file_exists($p athToImage)) {
    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 imagecreatefrom jpeg.
    //
    // 05-25-03 - function_exists () is the correct way to test.
    //
    // 07-22-04 - when images are uploaded, a thumbnail should be
    created.
    //
    // 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.

    $controllerForA ll = & getController() ;
    $resultsObject = & $controllerForA ll->getObject("McR esults",
    "makeImage" );

    if (function_exist s("getimagesize ")) {
    if (function_exist s("imagecreate" )) {
    if (function_exist s("imagecreatef romjpeg")) {
    if (function_exist s("imagecopyres ized")) {
    if (function_exist s("imagesx") && function_exists ("imagesy")) {
    if (function_exist s("imagejpeg" )) {
    if (function_exist s("imagecreatef romgif")) {
    if (function_exist s("imagegif") ) {

    $size = getimagesize($p athToImage);
    // 04-13-03 - Now we make sure we only output the jpgs and
    gifs
    // 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.
    // standardImageIn sert 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
    will
    // 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($newHeigh t);

    $newImage = imagecreate($ne wWidth, $newHeight);

    if ($size[2] == 2) {
    $srcImage = imagecreatefrom jpeg($image);
    imagecopyresize d($newImage, $srcImage, 0, 0, 0, 0,
    $newWidth, $newHeight, imagesx($srcIma ge), imagesy($srcIma ge));
    imagejpeg($newI mage);
    } elseif ($size[2] == 1) {
    $srcImage = imagecreatefrom gif($image);
    imageCopyResize d($newImage, $srcImage, 0, 0, 0, 0,
    $newWidth, $newHeight, imagesx($srcIma ge), imagesy($srcIma ge));
    imagegif($newIm age);
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    } else {
    $resultsObject->error("In makeImage, the second parameter should
    have told us what width was desired for this image, but it did not.",
    "makeImage" );
    }
    } 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" );
    }
    }
    }
  • lunatech

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

    lawrence wrote:[color=blue]
    > 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?[/color]

    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($imag e,$fname."-fmt-jpg".'jpg')

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

    Comment

    • lawrence

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

      "lunatech" <r.shekhar@gmai l.com> wrote in message news:<110101532 0.152898.80180@ z14g2000cwz.goo glegroups.com>. ..[color=blue]
      > Other than that, you have far too many nested ifs .. cannot help but
      > think "d00d, you are screwed"[/color]

      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
      it.

      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
      functions.

      Comment

      • lunatech

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

        lawrence wrote:[color=blue]
        > "lunatech" <r.shekhar@gmai l.com> wrote in message[/color]
        news:<110101532 0.152898.80180@ z14g2000cwz.goo glegroups.com>. ..[color=blue][color=green]
        > > Other than that, you have far too many nested ifs .. cannot help[/color][/color]
        but[color=blue][color=green]
        > > think "d00d, you are screwed"[/color]
        >
        > 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[/color]
        the[color=blue]
        > 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.[/color]

        My concern is that the code is too complex. But that maybe just
        because I have a small brain :-)
        [color=blue]
        >
        > 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[/color]
        know[color=blue]
        > it.[/color]

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

        - All the functions (imagesx/y, imagecreatefrom jpeg, 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_ima ge_functions_ex ist()
        {
        if (
        function_exists (getimagesize) &&
        function_exists (getimagecreate ) &&
        function_exists (magecreatefrom gif) &&
        function_exists (BLAH_BLAH)
        )
        return true;
        else
        return false;
        }




        function makeImage($path ToImage=false, $desiredSize=fa lse)
        {
        if (do_required_im age_functions_e xist())
        {
        YOUR CODE
        }

        else
        {
        HANDLE ERROR CONDITION
        }
        }



        [color=blue]
        >
        > 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.[/color]

        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
        YMMV

        Comment

        • R. Rajesh Jeba Anbiah

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

          lkrubner@geocit ies.com (lawrence) wrote in message news:<da7e68e8. 0411201942.409b f27e@posting.go ogle.com>...[color=blue]
          > 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?[/color]

          1. Using md5() on parameters. eg. $image_name =
          'img'.md5($x.$y .$size).'.jpg';
          //imgacbd18db4cc2 f85cedef654fccc 4a4d8.jpg
          2. http://in.php.net/uniqid

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

          Comment

          Working...