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);
}
}