Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attachment thumb #426

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 4 additions & 61 deletions sources/controllers/Display.controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ function prepareDisplayContext($reset = false)
*/
function loadAttachmentContext($id_msg)
{
global $attachments, $modSettings, $txt, $scripturl, $topic, $smcFunc;
global $attachments, $modSettings, $txt, $scripturl, $topic;

// Set up the attachment info - based on code by Meriadoc.
$attachmentData = array();
Expand Down Expand Up @@ -1216,67 +1216,10 @@ function loadAttachmentContext($id_msg)
{
$filename = getAttachmentFilename($attachment['filename'], $attachment['id_attach'], $attachment['id_folder']);

require_once(SUBSDIR . '/Graphics.subs.php');
if (createThumbnail($filename, $modSettings['attachmentThumbWidth'], $modSettings['attachmentThumbHeight']))
{
// So what folder are we putting this image in?
$path = getAttachmentPath();
$id_folder_thumb = getAttachmentPathID();

// Calculate the size of the created thumbnail.
$size = @getimagesize($filename . '_thumb');
list ($attachment['thumb_width'], $attachment['thumb_height']) = $size;
$thumb_size = filesize($filename . '_thumb');

// These are the only valid image types.
$validImageTypes = array(1 => 'gif', 2 => 'jpeg', 3 => 'png', 5 => 'psd', 6 => 'bmp', 7 => 'tiff', 8 => 'tiff', 9 => 'jpeg', 14 => 'iff');

// What about the extension?
$thumb_ext = isset($validImageTypes[$size[2]]) ? $validImageTypes[$size[2]] : '';

// Figure out the mime type.
if (!empty($size['mime']))
$thumb_mime = $size['mime'];
else
$thumb_mime = 'image/' . $thumb_ext;

$thumb_filename = $attachment['filename'] . '_thumb';
$thumb_hash = getAttachmentFilename($thumb_filename, false, null, true);

// Add this beauty to the database.
$smcFunc['db_insert']('',
'{db_prefix}attachments',
array('id_folder' => 'int', 'id_msg' => 'int', 'attachment_type' => 'int', 'filename' => 'string', 'file_hash' => 'string', 'size' => 'int', 'width' => 'int', 'height' => 'int', 'fileext' => 'string', 'mime_type' => 'string'),
array($id_folder_thumb, $id_msg, 3, $thumb_filename, $thumb_hash, (int) $thumb_size, (int) $attachment['thumb_width'], (int) $attachment['thumb_height'], $thumb_ext, $thumb_mime),
array('id_attach')
);
$old_id_thumb = $attachment['id_thumb'];
$attachment['id_thumb'] = $smcFunc['db_insert_id']('{db_prefix}attachments', 'id_attach');
if (!empty($attachment['id_thumb']))
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}attachments
SET id_thumb = {int:id_thumb}
WHERE id_attach = {int:id_attach}',
array(
'id_thumb' => $attachment['id_thumb'],
'id_attach' => $attachment['id_attach'],
)
);

$thumb_realname = getAttachmentFilename($thumb_filename, $attachment['id_thumb'], $id_folder_thumb, false, $thumb_hash);
rename($filename . '_thumb', $thumb_realname);

// Do we need to remove an old thumbnail?
if (!empty($old_id_thumb))
{
require_once(SUBSDIR . '/Attachments.subs.php');
removeAttachments(array('id_attach' => $old_id_thumb), '', false, false);
}
}
}
require_once(SUBSDIR . '/Attachments.subs.php');
$attachment = $attachment + updateAttachmentThumbnail($filename, $id_msg, $attachment['id_thumb']);
}

// Only adjust dimensions on successful thumbnail creation.
if (!empty($attachment['thumb_width']) && !empty($attachment['thumb_height']))
{
Expand Down
77 changes: 77 additions & 0 deletions sources/subs/Attachments.subs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2374,4 +2374,81 @@ function setRemovalNotice($messages, $notice)
'notice' => '<br /><br />' . $notice,
)
);
}

/**
* Update an attachment's thumbnail
*
* @param string $filename
* @param int $id_msg
* @param int $old_id_thumb = 0
* @return array The updated information
*/
function updateAttachmentThumbnail($filename, $id_msg, $old_id_thumb = 0)
{
global $modSettings;

$attachment = array();

require_once(SUBSDIR . '/Graphics.subs.php');
if (createThumbnail($filename, $modSettings['attachmentThumbWidth'], $modSettings['attachmentThumbHeight']))
{
// So what folder are we putting this image in?
$id_folder_thumb = getAttachmentPathID();

// Calculate the size of the created thumbnail.
$size = @getimagesize($filename . '_thumb');
list ($attachment['thumb_width'], $attachment['thumb_height']) = $size;
$thumb_size = filesize($filename . '_thumb');

// These are the only valid image types.
$validImageTypes = array(1 => 'gif', 2 => 'jpeg', 3 => 'png', 5 => 'psd', 6 => 'bmp', 7 => 'tiff', 8 => 'tiff', 9 => 'jpeg', 14 => 'iff');

// What about the extension?
$thumb_ext = isset($validImageTypes[$size[2]]) ? $validImageTypes[$size[2]] : '';

// Figure out the mime type.
if (!empty($size['mime']))
$thumb_mime = $size['mime'];
else
$thumb_mime = 'image/' . $thumb_ext;

$thumb_filename = $filename . '_thumb';
$thumb_hash = getAttachmentFilename($thumb_filename, false, null, true);

$db = database();

// Add this beauty to the database.
$db->insert('',
'{db_prefix}attachments',
array('id_folder' => 'int', 'id_msg' => 'int', 'attachment_type' => 'int', 'filename' => 'string', 'file_hash' => 'string', 'size' => 'int', 'width' => 'int', 'height' => 'int', 'fileext' => 'string', 'mime_type' => 'string'),
array($id_folder_thumb, $id_msg, 3, $thumb_filename, $thumb_hash, (int) $thumb_size, (int) $attachment['thumb_width'], (int) $attachment['thumb_height'], $thumb_ext, $thumb_mime),
array('id_attach')
);

$attachment['id_thumb'] = $db->insert_id('{db_prefix}attachments', 'id_attach');
if (!empty($attachment['id_thumb']))
{
$db->query('', '
UPDATE {db_prefix}attachments
SET id_thumb = {int:id_thumb}
WHERE id_attach = {int:id_attach}',
array(
'id_thumb' => $attachment['id_thumb'],
'id_attach' => $attachment['id_attach'],
)
);

$thumb_realname = getAttachmentFilename($thumb_filename, $attachment['id_thumb'], $id_folder_thumb, false, $thumb_hash);
rename($filename . '_thumb', $thumb_realname);

// Do we need to remove an old thumbnail?
if (!empty($old_id_thumb))
{
removeAttachments(array('id_attach' => $old_id_thumb), '', false, false);
}
}
}

return $attachment;
}