Welcome Guest, Not a member yet? Register   Sign In
uploading and using images
#2

[eluser]textnotspeech[/eluser]
As far as storing an image in a database, most people store the "image_name.ext" in the database and then generate a link from the query. Conversely you could store the "path/to/image/image_name.ext" but this could cause problems if you change the directory architecture. There is a way to store binary info in a database but I've never come across a reason to use this.

As far as appending "_foo" to your image names I can give you my handy little append function:

Code:
// Takes two arguments, the image file name and the string you wish to append before the extension. ie. append("my_picture.jpg", "_thumb")
// NOTE: if you pass in a four letter extension other than .jpeg, this function will fail.
function append($filename, $append)
        {
            $imagename = substr($filename, 0, -4); // extract the image name
            $extension = substr($filename, -4); // extract the extension
            // Not all image extensions are created equal, we're expecting a 3 letter extension so if we get a .jpeg extension, let's convert it
            if($extension == "jpeg")
            {
                $imagename = substr($imagename, 0, -1);
                $extension = '.jpg';
            }
            $extension = strtolower($extension); // I like uniform lowercase file extensions
            $new_filename = $imagename.$append.$extension; // put the filename string together
            return $new_filename;
        }

As for how you associate that image with a user completely depends on your model, the short answer however is: "by user id". If you want to get fancy with filenames, you could use this function that will append or prepend strings to your filename:

Code:
// Takes three arguments, the image file name, the string you wish to append or prepend and the mode (default is append).
// To prepend a string: pend("my_picture.jpg", "pre_", TRUE)
// NOTE: if you pass in a four letter extension other than .jpeg, this function will fail.
function pend($filename, $string, $prepend=FALSE)
        {
            $imagename = substr($filename, 0, -4); // extract the image name
            $extension = substr($filename, -4); // extract the extension
            // Not all image extensions are created equal, we're expecting a 3 letter extension so if we get a .jpeg extension, let's convert it
            if($extension == "jpeg")
            {
                $imagename = substr($imagename, 0, -1);
                $extension = '.jpg';
            }
            $extension = strtolower($extension); // I like uniform lowercase file extensions
            
            $new_filename = $prepend ? $string.$imagename.$extension : $imagename.$string.$extension; // put the filename string together based on the mode
            
            return $new_filename;
        }

Hope that helps!


Messages In This Thread
uploading and using images - by El Forum - 09-14-2008, 08:24 AM
uploading and using images - by El Forum - 09-14-2008, 07:36 PM
uploading and using images - by El Forum - 09-16-2008, 01:51 PM
uploading and using images - by El Forum - 09-16-2008, 05:38 PM
uploading and using images - by El Forum - 09-17-2008, 03:00 AM
uploading and using images - by El Forum - 09-17-2008, 09:24 AM
uploading and using images - by El Forum - 09-17-2008, 09:39 AM
uploading and using images - by El Forum - 09-17-2008, 10:11 AM
uploading and using images - by El Forum - 09-17-2008, 10:55 AM
uploading and using images - by El Forum - 09-17-2008, 11:05 AM
uploading and using images - by El Forum - 09-17-2008, 11:19 AM
uploading and using images - by El Forum - 09-17-2008, 11:42 AM
uploading and using images - by El Forum - 09-17-2008, 12:16 PM
uploading and using images - by El Forum - 09-18-2008, 11:28 AM
uploading and using images - by El Forum - 09-19-2008, 06:20 AM
uploading and using images - by El Forum - 09-19-2008, 06:36 AM
uploading and using images - by El Forum - 09-19-2008, 06:38 AM
uploading and using images - by El Forum - 09-19-2008, 07:00 AM
uploading and using images - by El Forum - 09-19-2008, 07:08 AM
uploading and using images - by El Forum - 09-19-2008, 07:09 AM
uploading and using images - by El Forum - 09-19-2008, 07:11 AM
uploading and using images - by El Forum - 09-19-2008, 07:12 AM
uploading and using images - by El Forum - 09-19-2008, 07:19 AM
uploading and using images - by El Forum - 09-19-2008, 07:21 AM
uploading and using images - by El Forum - 09-19-2008, 09:13 AM
uploading and using images - by El Forum - 09-20-2008, 12:54 PM
uploading and using images - by El Forum - 09-20-2008, 02:46 PM
uploading and using images - by El Forum - 09-20-2008, 03:11 PM
uploading and using images - by El Forum - 09-20-2008, 03:28 PM
uploading and using images - by El Forum - 09-20-2008, 03:59 PM
uploading and using images - by El Forum - 09-20-2008, 04:10 PM
uploading and using images - by El Forum - 09-20-2008, 04:32 PM
uploading and using images - by El Forum - 09-20-2008, 05:41 PM
uploading and using images - by El Forum - 09-22-2008, 01:35 PM
uploading and using images - by El Forum - 09-22-2008, 01:59 PM
uploading and using images - by El Forum - 09-22-2008, 02:01 PM
uploading and using images - by El Forum - 09-23-2008, 02:57 AM
uploading and using images - by El Forum - 09-23-2008, 03:02 AM
uploading and using images - by El Forum - 09-23-2008, 03:19 AM
uploading and using images - by El Forum - 09-23-2008, 03:24 AM
uploading and using images - by El Forum - 09-23-2008, 03:38 AM
uploading and using images - by El Forum - 09-23-2008, 03:47 AM
uploading and using images - by El Forum - 09-23-2008, 04:16 AM
uploading and using images - by El Forum - 09-23-2008, 04:21 AM
uploading and using images - by El Forum - 09-23-2008, 10:11 AM



Theme © iAndrew 2016 - Forum software by © MyBB