Skip to content

Commit

Permalink
Merge pull request #9616 from hypeJunction/merge_1.12_2.0
Browse files Browse the repository at this point in the history
Merge 1.12 2.0
  • Loading branch information
hypeJunction committed Apr 5, 2016
2 parents e7c8b50 + 324e451 commit 668c71a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 54 deletions.
1 change: 1 addition & 0 deletions engine/lib/tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function string_to_tag_array($string) {
$ar = array_map('trim', $ar);
$ar = array_filter($ar, 'is_not_null');
$ar = array_map('strip_tags', $ar);
$ar = array_unique($ar);
return $ar;
}

Expand Down
117 changes: 64 additions & 53 deletions mod/file/actions/file/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,15 @@
$prefix = "file/";

// if previous file, delete it
if ($new_file == false) {
if (!$new_file) {
$filename = $file->getFilenameOnFilestore();
if (file_exists($filename)) {
unlink($filename);
}

// use same filename on the disk - ensures thumbnails are overwritten
$filestorename = $file->getFilename();
$filestorename = elgg_substr($filestorename, elgg_strlen($prefix));
} else {
$filestorename = elgg_strtolower(time().$_FILES['upload']['name']);
}

$filestorename = elgg_strtolower(time().$_FILES['upload']['name']);

$file->setFilename($prefix . $filestorename);
$file->originalfilename = $_FILES['upload']['name'];
$mime_type = $file->detectMimeType($_FILES['upload']['tmp_name'], $_FILES['upload']['type']);
Expand All @@ -109,61 +105,76 @@

$guid = $file->save();

// if image, we need to create thumbnails (this should be moved into a function)
if ($guid && $file->simpletype == "image") {
$file->icontime = time();

$thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 60, 60, true);
if ($thumbnail) {
$thumb = new ElggFile();
$thumb->setMimeType($_FILES['upload']['type']);
$thumb = new ElggFile();
$thumb->owner_guid = $file->owner_guid;

$sizes = [
'small' => [
'w' => 60,
'h' => 60,
'square' => true,
'metadata_name' => 'thumbnail',
'filename_prefix' => 'thumb',
],
'medium' => [
'w' => 153,
'h' => 153,
'square' => true,
'metadata_name' => 'smallthumb',
'filename_prefix' => 'smallthumb',
],
'large' => [
'w' => 600,
'h' => 600,
'square' => false,
'metadata_name' => 'largethumb',
'filename_prefix' => 'largethumb',
],
];

$remove_thumbs = function () use ($file, $sizes, $thumb) {
if (!$file->guid) {
return;
}

$thumb->setFilename($prefix."thumb".$filestorename);
$thumb->open("write");
$thumb->write($thumbnail);
$thumb->close();
unset($file->icontime);

$file->thumbnail = $prefix."thumb".$filestorename;
unset($thumbnail);
foreach ($sizes as $size => $data) {
$filename = $file->{$data['metadata_name']};
if ($filename !== null) {
$thumb->setFilename($filename);
$thumb->delete();
unset($file->{$data['metadata_name']});
}
}
};

$thumbsmall = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 153, 153, true);
if ($thumbsmall) {
$thumb->setFilename($prefix."smallthumb".$filestorename);
$thumb->open("write");
$thumb->write($thumbsmall);
$thumb->close();
$file->smallthumb = $prefix."smallthumb".$filestorename;
unset($thumbsmall);
}
$remove_thumbs();

$jpg_filename = pathinfo($filestorename, PATHINFO_FILENAME) . '.jpg';

if ($guid && $file->simpletype == "image") {
$file->icontime = time();

foreach ($sizes as $size => $data) {
$image_bytes = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), $data['w'], $data['h'], $data['square']);
if (!$image_bytes) {
// bail and remove any thumbs
$remove_thumbs();
break;
}

$thumblarge = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 600, 600, false);
if ($thumblarge) {
$thumb->setFilename($prefix."largethumb".$filestorename);
$filename = "{$prefix}{$data['filename_prefix']}{$jpg_filename}";
$thumb->setFilename($filename);
$thumb->open("write");
$thumb->write($thumblarge);
$thumb->write($image_bytes);
$thumb->close();
$file->largethumb = $prefix."largethumb".$filestorename;
unset($thumblarge);
unset($image_bytes);

$file->{$data['metadata_name']} = $filename;
}
} elseif ($file->icontime) {
// if it is not an image, we do not need thumbnails
unset($file->icontime);

$thumb = new ElggFile();

$thumb->setFilename($prefix . "thumb" . $filestorename);
$thumb->delete();
unset($file->thumbnail);

$thumb->setFilename($prefix . "smallthumb" . $filestorename);
$thumb->delete();
unset($file->smallthumb);

$thumb->setFilename($prefix . "largethumb" . $filestorename);
$thumb->delete();
unset($file->largethumb);
}

} else {
// not saving a file but still need to save the entity to push attributes to database
$file->save();
Expand Down
6 changes: 5 additions & 1 deletion mod/file/thumbnail.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
if (!elgg_instanceof($file, 'object', 'file')) {
exit;
}
/* @var ElggFile $file */

$simpletype = $file->simpletype;
if ($simpletype == "image") {
Expand All @@ -46,7 +47,10 @@
$readfile = new ElggFile();
$readfile->owner_guid = $file->owner_guid;
$readfile->setFilename($thumbfile);
$mime = $file->getMimeType();
$mime = $readfile->detectMimeType();
if ($mime === 'application/octet-stream') {
$mime = 'image/jpeg';
}
$contents = $readfile->grabFile();

// caching images for 10 days
Expand Down

0 comments on commit 668c71a

Please sign in to comment.