DeeEmm

Pragmatism in code

If you are using Media Gallery to provide image handling functionality to your Geeklog site, the following will help you migrate your images. Media gallery is now an integral part of GLfusion, and so the same principles should apply to GLFusion too.

If you are using 4Images, it is possible to migrate to Media Gallery, using the media gallery import functions, or alternatively you may modify the code below to migrate direct.

The version of Media Gallery that I have migrated from is 1.5.1. and is no longer available as a stand alone plugin. However, a diligent web search will likely be able to turn up a version.

So.... On with the migration.

The first thing to do is to copy the following tables from your old geeklog database to your new Joomla database.

gl_mg_media
gl_mg_media_albums
gl_mg_albums

Once these have been copied over, you can start to migrate the data.

 

First up we copy the image info over

INSERT INTO `jos_community_photos` (albumid, caption, published, creator, permissions, image, thumbnail, original, created, hits)
SELECT
b.album_id,
a.`media_title`,
'1',
c.owner_id,
'0',
concat('images/photos/',concat(a.media_user_id,concat('/',concat(b.album_id,concat('/',concat((SELECT media_filename from gl_mg_media WHERE media_id = a.media_id), concat('.', a.media_mime_ext))))))),
concat('images/photos/',concat(a.media_user_id,concat('/',concat(b.album_id,concat('/thumb_',concat((SELECT media_filename from gl_mg_media WHERE media_id = a.media_id), concat('.', a.media_mime_ext))))))),
concat('images/originalphotos/',concat(a.media_user_id,concat('/',concat(b.album_id,concat('/',(SELECT media_filename from gl_mg_media WHERE media_id = a.media_id), concat('.', a.media_mime_ext)))))),
a.media_upload_time,
a.media_views
FROM gl_mg_media a
JOIN gl_mg_media_albums b ON b.media_id = a.media_id
JOIN gl_mg_albums c ON c.album_id = b.album_id
WHERE c.album_parent='45'



Next the album info...

insert into jos_community_photos_albums (id, photoid, creator, name, description, permissions, path, type,hits)
SELECT album_id, '', owner_id, album_title, album_desc, '0', concat('images/photos/',concat( owner_id, '/')), 'user', album_views from gl_mg_albums WHERE album_parent = '45'

 

And finally, we set the album cover image

//set gallery default image (arbitrary image)
UPDATE jos_community_photos_albums a, jos_community_photos b
SET a.`photoid` = b.id
WHERE a.`creator` = b.`creator`
AND a.`photoid` ='0'

 

Once done all that is left is to copy the images into the correct location. This is done with the aid of a bit of PHP code. The images need to be organised into the proper folder hierarchy, which involves interrogating the database, creating the relevant folder structure, and then copying the images.

Points to note...

  • Be sure to change the database login details + database name
  • If not on the same domain, you will need to copy the existing /images/mediaobjects/orig/ contents across.
  • Correct folder permission will need to be granted to allow the script to create the folders.

 



//copy function
function copyemz($file1,$file2){
$contentx =@file_get_contents($file1);
$openedfile = fopen($file2, "w");
fwrite($openedfile, $contentx);
fclose($openedfile);
if ($contentx === FALSE) {
$status=false;
}else $status=true;

return $status;
}

//estabish database connection
$con = mysql_connect('localhost','USER','PASSWORD');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('DATABASENAME', $con);


//get filename from database
$result = mysql_query("SELECT Image FROM jos_community_photos");

while($row = mysql_fetch_array($result))
{
//generate filenames + paths

$strpart = explode('/', $row['Image']);
$filename = $strpart[4];
$filepath = explode($filename, $row['Image']);

$sourcepart = explode( '_', $filename);

//get current directory
$thisdir = getcwd();



//get filename and directory
$SourceFile = 'images/mediaobjects/orig/' . $sourcepart[0] . '/' . $filename;
$DestFile = $row['Image'];
$DestPath = $thisdir . '/images/photos/' . $strpart[2] . '/' . $strpart[3];

//create user directory
if(mkdir($DestPath , 0777, true)) {
echo "Directory $DestPath has been created successfully...
";
} else {
echo "Failed to create $DestPath directory...
";
}

//copy file
if (!copyemz($SourceFile, $DestFile)) {    
echo "failed to copy $DestFile ...
";
} else {
echo $SourceFile . ' -> ' . $DestFile . ' copied successfully
';
}

//get filename and directory for thumbnails
$SourceFile = 'images/mediaobjects/tn/' . $sourcepart[0] . '/' . $filename;
$DestFile = 'images/photos/' . $strpart[2] . '/' . $strpart[3] . '/thumb_' . $filename;

if (!copyemz($SourceFile, $DestFile)) {    
echo "failed to copy $DestFile ...
";
} else {
echo $SourceFile . ' -> ' . $DestFile . ' copied successfully
';
}



//get filename and directory
$SourceFile = 'images/mediaobjects/orig/' . $sourcepart[0] . '/' . $filename;
$DestFile = 'images/originalphotos/' . $strpart[2] . '/' . $strpart[3] . '/' . $filename;
$DestPath = $thisdir . '/images/originalphotos/' . $strpart[2] . '/' . $strpart[3];

//create user directory
if(mkdir($DestPath , 0777, true)) {
echo "Directory $DestPath has been created successfully...
";
} else {
echo "Failed to create $DestPath directory...
";
}

if (!copyemz($SourceFile, $DestFile)) {    
echo "failed to copy $DestFile ...
";
} else {
echo $SourceFile . ' -> ' . $DestFile . ' copied successfully
';
}


}

mysql_close($con);

?>

Once you have run the script, you should now be able to see all of your users photo albums

/DM