Current location: Hot Scripts Forums » Programming Languages » PHP » Saving thumbnail into Database


Saving thumbnail into Database

Reply
  #1 (permalink)  
Old 02-16-05, 07:33 AM
astro astro is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Saving thumbnail into Database

I have a mysql database that is storing some images and text. I have 2 blob field one for a full sized image and one for a thumbnail.
When my form is posted with the data and image I first read the image and put it into the blob field. Then I resize it and try to save it to a folder so i can insert it into the blob field. I have had alot of problems with functions expecting an string and a resource is given instead.

Any Help would be extreamly usefull.

here is the resize and update code:

$img = null;
$ext = $_FILES['blob']['type'];
if ($ext == 'image/pjpeg' || $ext == 'image/jpeg' || $ext == 'image/jpg') {
$img = imagecreatefromjpeg($image_path);
} else if ($ext == 'image/png') {
$img = imagecreatefrompng($image_path);
}else{
echo "can't create thumbnail from this type of image. Please use JPEG or PNG image file types" ;
}

# If an image was successfully loaded, test the image for size
if ($img) {

# Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
echo $scale;

# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);

# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);

# Copy and resize old image into new image
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
echo $img;
}
}
if($img){
$uploaddir = 'img\\';
$uploadfile = $uploaddir . basename($img);//basename is where current script sits.
if (move_uploaded_file($img, $uploadfile)) {
echo "File is valid, and was successfully uploaded.<br />";
} else {
echo GetImageSize();
}
}


# Create error image if necessary
if (!$img) {
/*$img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);*/
echo 'no image loaded <br />';
}
if($img){
$blobThumb = "";
//get file
$fp = fopen('img/test.jpg', "rb"); // rd=read binary
while(!feof($fp)){ // while feof(test for-end-of-file)
$blobThumb .= fread($fp, 1); //read into $imgData 1 byte at a time
}
fclose($fp);
$blobThumb = addslashes($blobThumb);

$queryT= "UPDATE test SET testBlobThumb='$blobThumb' WHERE testName='$name'";

$resultT=mysql_query($queryT);
if ($result){
echo 'thumb inserted <br />';
}else{
echo 'thumb insert failed <br />';
mysql_free_result($resultT);
}
}
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Form to update a MySQL database? Scoobler PHP 9 09-04-08 01:41 AM
Script for uploading data from local database to production database pual_cruzs PHP 1 07-06-04 12:44 AM
Share database over the Internet nitinkedia The Lounge 2 11-20-03 02:07 PM
Share database over the Internet nitinkedia New Members & Introductions 1 07-10-03 02:50 PM


All times are GMT -5. The time now is 05:30 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.