php - How to save uploaded image names in MySQL database -
part of answered question
i have beneath php code upload 1 cropped image 3 different width , height in
- avatar directory width , height 200*200
- avatar1 directory width , height 500*500
- avatar2 directory width , height 700*700
and renames image md5 random.
<?php function uploadimagefile() { // note: gd library required function if ($_server['request_method'] == 'post') { $ijpgquality = 100; if ($_files) { // if no errors , size less 250kb if (! $_files['image_file']['error'] && $_files['image_file']['size'] < 250 * 1024) { if (is_uploaded_file($_files['image_file']['tmp_name'])) { if (!is_dir('avatar')) { mkdir('avatar'); } // new unique filename $stempfilename = 'avatar/' . md5(time().rand()); // move uploaded file cache folder move_uploaded_file($_files['image_file']['tmp_name'], $stempfilename); // change file permission 644 @chmod($stempfilename, 0644); $sresultfilename = copyimagefile('avatar', $stempfilename, 200, 200, $ijpgquality); if ($sresultfilename) { copyimagefile('avatar1', $stempfilename, 500, 500); copyimagefile('avatar2', $stempfilename, 700, 700); @unlink($stempfilename); return $sresultfilename; } } } } } return false; } function copyimagefile($dirname, $originimagename, $iwidth, $iheight, $ijpgquality = 90) { if (file_exists($originimagename) && filesize($originimagename) > 0) { $asize = getimagesize($originimagename); // try obtain image info if (!$asize) { @unlink($originimagename); return; } // check image type switch($asize[2]) { case imagetype_jpeg: $sext = '.jpg'; $vimg = @imagecreatefromjpeg($originimagename); break; /*case imagetype_gif: $sext = '.gif'; // create new image file $vimg = @imagecreatefromgif($stempfilename); break;*/ case imagetype_png: $sext = '.png'; $vimg = @imagecreatefrompng($originimagename); break; default: @unlink($originimagename); return; } // create new true color image $vdstimg = @imagecreatetruecolor( $iwidth, $iheight ); // copy , resize part of image resampling imagecopyresampled($vdstimg, $vimg, 0, 0, (int)$_post['x1'], (int)$_post['y1'], $iwidth, $iheight, (int)$_post['w'], (int)$_post['h']); // define result image filename if (!is_dir($dirname)) { mkdir($dirname); } $newimagename = $dirname . directory_separator . md5(time().rand()) . $sext; // output image file imagejpeg($vdstimg, $newimagename, $ijpgquality); //@unlink($stempfilename); return $newimagename; } return false; } $simage = uploadimagefile(); echo '<img src="'.$simage.'" />'; ?> my question
i want save url of 3 recent uploaded image inara mysqldatabase in
avatarcolumn= url of image uploaded avatar directoryavatar1column= url of image uploaded avatar1 directoryavatar2column= url of image uploaded avatar2 directory
database name: ara
database user: root
database password:
database address: 127.0.0.1
table name: profiles
column names: avatar, avatar1 , avatar2
copyimagefile('avatar1', $stempfilename, 500, 500); copyimagefile('avatar2', $stempfilename, 700, 700); replace code after
include('connect/mysql.php'); $avatar1=copyimagefile('avatar1', $stempfilename, 500, 500); $avatar2=copyimagefile('avatar2', $stempfilename, 700, 700); $user_name = mysqli_real_escape_string($con, $_session['username']); mysqli_query($con,"update profiles set avatarimage='".$sresultfilename."',avatarimagebig='".$avatar1."',avatarimagesmall='".$avatar2."' username = '$user_name'"); mysqli_close($con);
Comments
Post a Comment