Jump to content

Pulling images from old db, converting and putting in new db


ukscotth

Recommended Posts

Hi,

 

I currently run a busy social networking site and im trying to change the software it runs off.

 

Ive managed to transfer all the user data and the forum data but im stuck on transferring the pictures.  The main problem im having is that on the old site they are stored in a folder on the server but on the new site they are stored in the database itself.

 

heres the database structures for both sites :

 

OLD SITE

 

picture_id  picture_username  picture_type  picture_path  picture_label  picture_status  adult_status  reason  gallery

 

1 KellyIOW 1 pictures/193242254_nightclub_girls.jpg  1 0  ungrouped

 

NEW SITE

 

userid  filedata  dateline  filename  visible  filesize  width  height  filedata_thumb  width_thumb  height_thumb 

 

30 [bLOB - 1.6 KB] 1247656853 me.jpg 1 1604 80 64 [bLOB - 1.4 KB] 60 48

 

 

I think Id need to do something like this :

 



$fh = fopen("MyImg.jpg", "r"); 
$data = addslashes(fread($fh, filesize("MyImg.jpg"))); 
fclose($fh); 
  
// Create the query 
$SQL = " 
INSERT INTO Image(ImgTitle, ImgType, ImgData) 
VALUES('My Image', 'jpg', '$data')"; 
  
// Execute the query         
$RESULT = mysql_query($SQL) or die("Couldn't insert image");

 

But im unsure how to work out the right heights and widths for the images and the thumbnails :(

 

Can anyone help plzzzz ?

 

Thanks,

 

Scott.

 

 

Scott,

 

You'll need to have php 'read' the binary content of the image and then store that content in a BLOB database column.  It's also handy to store the original file name, file size and mime type.

 

$filename = "test.jpg";
$content = file_get_contents($filename);
$size = filesize($filename);

thanks ryan.

 

So would I do something like this ?

 


$filename = "test.jpg";
$content = file_get_contents($filename);
$size = filesize($filename);				

mysql_query("INSERT INTO customavatar (`userid`, `filedata`, `filename`, `filesize`, `filedata_thumb`) 

VALUES 

('20', '".$content."', '".$filename."','".$size."','".$content."')");

Ok, Ive got it to work sort of using this :

 


$fh = fopen("test.jpg", "r"); 
$data = addslashes(fread($fh, filesize("test.jpg"))); 
fclose($fh); 
  
// Create the query 
$SQL = " 
INSERT INTO customavatar(userid, filedata, filename,filedata_thumb) 
VALUES('13','$data','test.jpg','$data')"; 
  
// Execute the query         

$RESULT = mysql_query($SQL) or die("Couldn't insert image");

 

Not sure how to get the dimensions and create the thumbnail tho :(

<?php
function makeThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) {

  $dir = opendir($pathToImages);

  while (false !== ($fname = readdir($dir))) {

    $info = pathinfo($pathToImages . $fname);

    if (strtolower($info['extension']) == 'jpg') {
       
    	echo "Creating thumbnail for $fname <br />";

    $img = imagecreatefromjpeg($pathToImages . $fname);
    
    $width = imagesx($img);
    
    $height = imagesy($img);
      
    $new_width = $thumbWidth;
    
    $new_height = floor($height * ($thumbWidth / $width));
    
    $tmp_img = imagecreatetruecolor($new_width, $new_height);

      	imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      	imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}");
    }
    
  }

  closedir($dir);
  
}

makeThumbs("upload/","upload/thumbs/",100);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.