SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot ShinoKage's Avatar
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    imagecopyresampled() - How Do You Edit A Filename?

    Okay so imagecopyresampled().... let's say I just uploaded the file funnycat.jpg, so for the thumbnail I want to call it funnycatthm.jpg. How would I do that??? When you get the filename it automatically includes the extension. But it (http://www.php.net/manual/en/functio...yresampled.php) says to enter the "Destination image link resource" as a parameter. So that means the entire file name (directories included and all, like site.com/images/coolstuff/funnycat.jpg), right? So how the heck do I add something before the extension to give the new file a name? I'm so confused.

    Any help with this would be immensely appreciated.

  2. #2
    Non-Member
    Join Date
    Dec 2010
    Location
    /home/pc
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use imagejpeg to output a jpg file to whatever file name you like.

  3. #3
    SitePoint Zealot ShinoKage's Avatar
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I understand that I can give it whatever name I want, but the images that are upload have file names that are generated for them, so I would like to just be able to tack something onto the end of it (such as 'thm'), I want to know if it's possible to do that.

    Is there a way for it to tell me what the extension is? Even that would be helpful (since, for example JPEGs can be either jpg or jpeg), because then I could just take off the file extension, add what I want, and then stick it back on....

  4. #4
    Non-Member
    Join Date
    Dec 2010
    Location
    /home/pc
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ShinoKage View Post
    Well, I understand that I can give it whatever name I want, but the images that are upload have file names that are generated for them, so I would like to just be able to tack something onto the end of it (such as 'thm'), I want to know if it's possible to do that.

    Is there a way for it to tell me what the extension is? Even that would be helpful (since, for example JPEGs can be either jpg or jpeg), because then I could just take off the file extension, add what I want, and then stick it back on....
    What is the code that generates the file names for the uploaded files? I would think you should be able to adjust that code to add 'thm' to the file name.

  5. #5
    SitePoint Zealot ShinoKage's Avatar
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $now time();
    while (
    file_exists($uploadFileName $uploaddir $now '-' $_FILES['file']['name']))
    {
        
    $now++;

    That's the code, but I was just thinking that I can just add what I want to the beginning instead of the end ^_^; -sigh- My problems are always so simple to solve.

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,031
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    What you want to do is separate the filename bit from the extension, append what you want to the filename, and then glue them back together. Like so:

    PHP Code:
    // assume uploaded file
    $filename=$_FILES['myfile']['name'];


    // get the position of the last (!) dot in the filename
    $pos=strrpos($filename'.')

    // get the file name
    $name=substr($filename0$pos);

    // get the extension
    $ext=substr($filename$pos 1);

    // set the new name
    $newName=$name.'hth';

    // glue back together
    $newFilename=$newName.'.'.$ext
    Note that this uses $ext without the dot in the extension, which makes it easer if you want to check the extension later; 'jpg' is easier to read and type than '.jpg'
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Zealot ShinoKage's Avatar
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ooooooh; I knew there must've been a way to do that. Thank you!!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •