News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

phpBB2 attachment mod : Viewing thumbs in SMF posts

Started by SillyCone, January 22, 2007, 06:16:32 PM

Previous topic - Next topic

SillyCone

SMF Version: SMF 1.1.1
Hello there,

I don't know about you, but I was very annoyed by the fact that the attachements were correctly copied over to SMF from phpBB2 attachment mod, but that the image didn't display in the posts... When I asked here in the community if it was possible, I was told : "SMF Convert doesn't support thumbnails yet..."

So I hacked the phpbb2_to_smf.sql to copy the attachment twice, resize the second copy, and insert it in the db with attachmentType set to 3... It worked, but nothing more was displayed. Damn, I hoped I didn't have anything to change elsewhere in the db, like the posts tables and so...
So I simply created a new post as a simple member, uploaded a picture, and it displayed as I wanted it to be. A quick look in the db showed that the only difference between my converted attachments and the native ones was... the width and the height columns in the smf_attachments table ! :)

So I changed the width and height of my attachments in the db, and voilà, everything was working perfectly !
I don't even need an actual thumbnail file nor a record in the attachments table, just setting the width and height of the pictures made my day.

I hope it's not breaking anything, not until now after a few tests, here is the code to change in phpbb2_to_smf.sql.

find this (at the very end of the file) :

while ($row = mysql_fetch_assoc($result))
{
if (!file_exists($oldAttachmentDir . '/' . $row['encrypted']))
$row['encrypted'] = strtr($row['encrypted'], '& ', '__');

// Get the true filesize in case the old db lied!
$fileSize = filesize($oldAttachmentDir . '/' . $row['encrypted']);
if (!is_integer($fileSize))
continue;

// Frankly I don't care whether they want encrypted filenames - they're having it - too dangerous.
$newfilename = getAttachmentFilename($row['filename'], $ID_ATTACH);

if (strlen($newfilename) <= 255 && copy($oldAttachmentDir . '/' . $row['encrypted'], $attachmentUploadDir . '/' . $newfilename))
{
$attachments[] = "($ID_ATTACH, $fileSize, SUBSTRING('" . addslashes($row['filename']) . "', 1, 255), $row[ID_MSG], $row[downloads])";

$ID_ATTACH++;
}
}

if (!empty($attachments))
convert_query("
INSERT INTO {$to_prefix}attachments
(ID_ATTACH, size, filename, ID_MSG, downloads)
VALUES " . implode(', ', $attachments));


and replace with this :

while ($row = mysql_fetch_assoc($result))
{
if (!file_exists($oldAttachmentDir . '/' . $row['encrypted']))
$row['encrypted'] = strtr($row['encrypted'], '& ', '__');

// Get the true filesize in case the old db lied!
$fileSize = filesize($oldAttachmentDir . '/' . $row['encrypted']);
if (!is_integer($fileSize))
continue;

//SillyCone hack - 22/01/2007 - add the width and height of images, so they can be viewed in the post where attached !
//first get extension of file and test if it's a jpg, a gif or a png - add any other image type in here, sizeable by php
$attachextension = strtolower(substr($row['filename'],-4));
if (($attachextension != 'jpeg') && ($attachextension != '.jpg') && ($attachextension != '.gif') && ($attachextension != '.png')) {
$attachextension = '';
}

// Frankly I don't care whether they want encrypted filenames - they're having it - too dangerous.
$newfilename = getAttachmentFilename($row['filename'], $ID_ATTACH);

//SillyCone hack - 22/01/2007 - store source file path in a variable to get its size later if it's an image
$sourcefile = $oldAttachmentDir.'/'.$row['encrypted'];

if (strlen($newfilename) <= 255 && copy($sourcefile, $attachmentUploadDir.'/'.$newfilename))
{
//SillyCone hack - 22/01/2007 - get size of the image to store in attachments table
$width = 0;
$height = 0;
if ($attachextension != '') {
$imagesizearray = getimagesize($sourcefile);
if ($imagesizearray) {
$width = $imagesizearray[0];
$height = $imagesizearray[1];
}
}
//SillyCone hack - 22/01/2007 - added width and height to the VALUES string
$attachments[] = "($ID_ATTACH, $width, $height, $fileSize, SUBSTRING('".addslashes($row['filename'])."', 1, 255), $row[ID_MSG], $row[downloads])";
$ID_ATTACH++;
}
}

if (!empty($attachments))
//SillyCone hack - 22/01/2007 - added width and height to the query
convert_query("
INSERT INTO {$to_prefix}attachments
(ID_ATTACH, width, height, size, filename, ID_MSG, downloads)
VALUES " . implode(', ', $attachments));


Cheers,
SillyCone.

JayBachatero

SillyCone thanks for posting this.  It should come in handy.
Follow me on Twitter

"HELP!!! I've fallen and I can't get up"
This moment has been brought to you by LifeAlert

SillyCone

#2
I just shortened the code, a bit optimized. Shouldn't change a thing, it's just prettier codewise.
Hihi coders, hé :P

JayBachatero

Ok I added this to the latest converter.  I made it a bit different though.  More SMFish ;).


while ($row = mysql_fetch_assoc($result))
{
if (!file_exists($oldAttachmentDir . '/' . $row['encrypted']))
$row['encrypted'] = strtr($row['encrypted'], '& ', '__');

// Get the true filesize in case the old db lied!
$fileSize = filesize($oldAttachmentDir . '/' . $row['encrypted']);
if (!is_integer($fileSize))
continue;

// Is this an image???
$attachmentExtension = strtolower(substr(strrchr($row['filename'], '.'), 1));
if (!in_array($attachmentExtension, array('jpg', 'jpeg', 'gif', 'png'))
$attachmentExtention = '';

// Lets just set one variable for the old attachment.  It has the dir name and file name.
$oldfilename = $oldAttachmentDir . '/' . $row['encrypted'];

// Frankly I don't care whether they want encrypted filenames - they're having it - too dangerous.
$newfilename = getAttachmentFilename($row['filename'], $ID_ATTACH);

if (strlen($newfilename) <= 255 && copy($oldfilename, $attachmentUploadDir . '/' . $newfilename))
{
// Set the default empty values.
$width = 0;
$height = 0;

// Is an an image?
if (!empty($attachmentExtension)
list ($width, $height) = getimagesize($oldfilename);

// Setup the attachments insert
$attachments[] = "
($ID_ATTACH, $row[ID_MSG], SUBSTRING('" . addslashes($row['filename']) . "', 1, 255), $fileSize, $row[downloads], $width, $height)";

// Increase it.
$ID_ATTACH++;
}
}

if (!empty($attachments))
convert_query("
INSERT INTO {$to_prefix}attachments
(ID_ATTACH, ID_MSG, filename, size, downloads, width, height)
VALUES " . implode(', ', $attachments));
Follow me on Twitter

"HELP!!! I've fallen and I can't get up"
This moment has been brought to you by LifeAlert

SillyCone

#4
Héhé, nice touch, the strrchr ;)

Although I'm not sure what will output getimagesize to $height in case the image file is corrupt, that's why I first tested its output... Will probably stick a 0 anyway, so... grmbl, hihi ;)

So you think creating an actual thumb file isn't usefull ? (it would be quite easy to do, otherwise, I just have to get that backup file)

Cheers,
SillyCone.

JayBachatero

getimagesize should return 0 or null for all the keys in the array.

I don't think creating the thumb file is needed.  SMF creates a thumb once you view the topic.  Saves us some coding ;).
Follow me on Twitter

"HELP!!! I've fallen and I can't get up"
This moment has been brought to you by LifeAlert

fshagan

I'm having this problem ... none of the attachments are making it over to my converted SMF forums.  I'm using the latest converter, and then even tried this code to see if it would work.

Any hints on this? 

SleePy

Is this issue solved or do you still require assistance?

Do you know if the database made it or anything? You might just be missing the files.
Jeremy D ~ Site Team / SMF Developer ~ GitHub Profile ~ Join us on IRC @ Libera.chat/#smf ~ Support the SMF Support team!

Advertisement: