Turn on suggestions
Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
Showing results for
PHP MySQL and Uploading jpegs or Gifs
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Plusnet Community
- :
- Forum
- :
- Help with my Plusnet services
- :
- Everything else
- :
- PHP MySQL and Uploading jpegs or Gifs
PHP MySQL and Uploading jpegs or Gifs
21-08-2007 1:52 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
I usualy use codewalkers for this sort of thing but they have changed their site and its giving me head aches now.
I'm trying to write a page that will allow users to upload their images to their profile page. I've seen a few ways to go about this but its not as simple as I first thought.
There are probably pages out there that would explain this but I have search and have had no joy.
General briefing:
I have thought about using the database to store the code for each image. I'm not sure that this is the best solution.
I have also thought about the possibility of sending the image file to a folder on the server along with the appropriate link, stored in the users MySQL profile field.
If anyone has successfully managed to write such a page I would appreciate your wise knowlegde on this subject or a link to a page that can help.
I'm trying to write a page that will allow users to upload their images to their profile page. I've seen a few ways to go about this but its not as simple as I first thought.
There are probably pages out there that would explain this but I have search and have had no joy.
General briefing:
- Users can logon and upload/delete an image through there "edit profile" page.
Public would be able to view users page and image displayed as a thumbnail with the option to expand the image by clicking the image.
I have thought about using the database to store the code for each image. I'm not sure that this is the best solution.
I have also thought about the possibility of sending the image file to a folder on the server along with the appropriate link, stored in the users MySQL profile field.
If anyone has successfully managed to write such a page I would appreciate your wise knowlegde on this subject or a link to a page that can help.
Message 1 of 3
(3,038 Views)
2 REPLIES 2
Re: PHP MySQL and Uploading jpegs or Gifs
21-08-2007 3:10 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
I have never understood the pros's and cons of storing images in a database vs as a file.
I usually store images as files and as you mentioned just include the link to the image in the database although if you rename the image to the user's id you don't even need to store that.
The code below is from an image upload script i use, it creates 2 smaller versions renaming the files along the way and eventually storing details in the database, you should be able to pull something useful out of it.
I usually store images as files and as you mentioned just include the link to the image in the database although if you rename the image to the user's id you don't even need to store that.
The code below is from an image upload script i use, it creates 2 smaller versions renaming the files along the way and eventually storing details in the database, you should be able to pull something useful out of it.
if (!empty($_FILES['image']['name'])) {
$image = (!empty($_FILES['image']['name']));
$image_attr = "";
$thumb_image_attr = "";
$thumb_image = "";
} else {
$error .= "No Image Specified<br />\n";
}
#if there are no other errors, process the image
if (empty($error)) {
$image_tempname = $_FILES['image']['name']; #get filename
$ImageDir = "D:/htdocs/uploads/images/";
$ImageName = $ImageDir . $image_tempname;
if (move_uploaded_file($_FILES['image']['tmp_name'], $ImageName)) {
#add watermark
#imagettftext($ImageName, 8, 0, 5, 5, 0, "arial.ttf", "a star solutions");
#get information about the image being uploaded
list($width, $height, $imgtype, $attr) = getimagesize($ImageName);
switch ($imgtype) {
case 2:
$ext = ".jpg";
break;
default:
$error .= "You can only upload jpg files<br />";
unlink($ImageName); # delete file
break;
}
}
}
#generate smaller images and move to correct locations
if (empty($error)) {
$Dir = "D:/htdocs/images/products/";
$WebDir = "/images/products/";
$newfilename = $Dir . $id . $ext;
$newthumbfilename = $Dir . $id . "_thumb" . $ext;
$newsmallfilename = $Dir . $id . "_small" . $ext;
$thumb_width = 150;
$thumb_ratio = ($width / $thumb_width);
$thumb_height = ($height / $thumb_ratio);
$small_width = 50;
$small_ratio = ($width / $small_width);
$small_height = ($height / $small_ratio);
if (file_exists($newfilename)) {
unlink($newfilename);
}
if (file_exists($newthumbfilename)) {
unlink($newthumbfilename);
}
if (file_exists($newsmallfilename)) {
unlink($newsmallfilename);
}
#create thumbnail
$largeimage = imagecreatefromjpeg($ImageName); #Create image from uploaded image
$thumb = imagecreatetruecolor($thumb_width,$thumb_height);
imagecopyresampled($thumb,$largeimage,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
imagejpeg($thumb,$newthumbfilename);
imagedestroy($thumb);
$small = imagecreatetruecolor($small_width,$small_height);
imagecopyresampled($small,$largeimage,0,0,0,0,$small_width,$small_height,$width,$height);
imagejpeg($small,$newsmallfilename);
imagedestroy($small);
imagedestroy($largeimage); #Remove temp image
#rename large image - move from admin dir
rename($ImageName, $newfilename);
#get attr details for the two small images
list($width, $height, $imgtype, $thumb_attr) = getimagesize($newthumbfilename);
list($width, $height, $imgtype, $small_attr) = getimagesize($newsmallfilename);
#get the dir details for the database
$ImgDir = $WebDir . $id . $ext;
$thumbImgDir = $WebDir . $id . "_thumb" . $ext;
$smallImgDir = $WebDir . $id . "_small" . $ext;
$sql = "UPDATE products " .
"SET product_image='" . $ImgDir .
"', product_image_attr='" . urlencode($attr) .
"', product_thumb_image='" . $thumbImgDir .
"', product_thumb_image_attr='" . urlencode($thumb_attr) .
"', product_small_image='" . $smallImgDir .
"', product_small_image_attr='" . urlencode($small_attr) .
"', product_date_updated='" . date("Y-m-d H:i:s",time()) .
"', product_user_updated='" . $_SESSION['user_id'] .
"' WHERE product_id=" . $id;
mysql_query($sql, $conn) or die(mysql_error() . "<br />" . $sql);
Message 2 of 3
(777 Views)
Re: PHP MySQL and Uploading jpegs or Gifs
22-08-2007 11:15 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
Thanks for that. I'll give it a wirl. Cant see it being completed in the next day or so but what i see seems to contain the answers.
Thanks again
Thanks again
Message 3 of 3
(777 Views)
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Plusnet Community
- :
- Forum
- :
- Help with my Plusnet services
- :
- Everything else
- :
- PHP MySQL and Uploading jpegs or Gifs