Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

generate random image from database php

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

generate random image from database php

Postby benrussellvick » Mon May 06, 2013 2:57 am

I'm trying to create a random image script that selects from an existing list of different users images within a database but i'm having no luck. Anyone knows of a way to do it?

Here's the code... 'rnprofile.php'

Code: Select all
if (isset($_FILES["image"]["name"]))
{
    $TempFile = $_FILES["image"]["tmp_name"];
    $FolderName = 'images/'; // Setting the upload directory
    $CurrentTime = time(); // Time of upload (UNIX)
    $Filename = $user."_".$CurrentTime.".jpg"; // Filename of the file
    $saveto = $FolderName.$Filename; // Will look something like"images/alex_1365249148.jpg"   

    //First debug

    //echo "DEBUG ECHO:<br/>We're uploading the file named: ".$Filename."<br/>And we're putting it into: ".$saveto."<br/>END DEBUG ECHO";
    //exit();



    move_uploaded_file($_FILES["image"]["tmp_name"], $saveto);
    $typeok = TRUE;


    switch($_FILES["image"]["type"])
    {
        case "image/gif":   $src = imagecreatefromgif($saveto); break;

        case "image/jpeg":  // Both regular and progressive jpegs
        case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;

        case "image/png":   $src = imagecreatefrompng($saveto); break;

        default:            $typeok = FALSE; break;
    }

    if ($typeok)
    {
        list($w, $h) = getimagesize($saveto);
        $max = 600;
        $tw  = $w;
        $th  = $h;

        if ($w > $h && $max < $w)
        {
            $th = $max / $w * $h;
            $tw = $max;
        }
        elseif ($h > $w && $max < $h)
        {
            $tw = $max / $h * $w;
            $th = $max;
        }
        elseif ($max < $w)
        {
            $tw = $th = $max;
        }

        $tmp = imagecreatetruecolor($tw, $th);
        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
        imageconvolution($tmp, array( // Sharpen image
                                array(-1, -1, -1),
                                array(-1, 16, -1),
                                array(-1, -1, -1)
                               ), 8, 0);
        imagejpeg($tmp, $saveto);
        log_image_upload($UserData['user_id'], $Filename, $saveto, $CurrentTime); // Logging the image upload into MySQL
        imagedestroy($tmp);
        imagedestroy($src);
    }
}

echo <<<_END
<form method='post' action='rnprofile.php'
    enctype='multipart/form-data'>
    </br>
<h3>Upload your designs here:</h3><br />

Image: <input type='file' name='image' size='14' maxlength='32' />
<input type='submit' value='Upload design' />
</pre></form>
_END;

'rnfunctions.php'...

Code: Select all
function log_image_upload($UserID, $Filename, $Location, $Time) {
    //add in sanitization code in here for all variables being parsed.
    mysql_query("INSERT INTO `images` (`user_id`, `location`, `filename`, `time`) VALUES ('$UserID', '$Location', '$Filename', '$Time')");

}

function showProfile($user) {

    //$UserID = mysql_result(mysql_query("SELECT `user_id` FROM `rnprofiles` WHERE `user` = '$user'"), 0) or die(mysql_error());



    $result=mysql_query("SELECT `user_id` FROM `rnprofiles` WHERE `user` = '$user'") or die(mysql_error());
    if(mysql_num_rows($result) or die(mysql_error())) {
    while($row = mysql_fetch_assoc($result)) {
    $UserID = $row['user_id'];
    }
    } else {
    echo "no rows found";
    }   $Query = mysql_query("SELECT * FROM `images` WHERE `user_id` = '$UserID'") or die(mysql_error());
    while ($ImageData = mysql_fetch_assoc($Query)) {

        echo '<p><a title="'.$user.'" class="fancybox" data-fancybox-group="gallery" href="'.$ImageData['location'].'"><img src="'.$ImageData['location'].'" border="1px" align="left" height="150"  alt=""/></a></p>';// Display each image belonging to the user
    }

    $result = mysql_query("SELECT * FROM rnprofiles WHERE user='$user'") or die(mysql_error());

    if (mysql_num_rows($result))
    {
        $row = mysql_fetch_row($result);
        echo stripslashes($row[8]) . "<a><br clear=left /><br /></a>";

    }

}

Thanks a lot in advance!
benrussellvick
New php-forum User
New php-forum User
 
Posts: 3
Joined: Sun Apr 07, 2013 11:09 am

Re: generate random image from database php

Postby pbs » Sat May 11, 2013 3:11 am

Use "ORDER BY RAND()" in your SELECT query
pbs
New php-forum User
New php-forum User
 
Posts: 39
Joined: Sat May 11, 2013 1:40 am
Location: Nashik, India


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.