Uploaded images
Quote from Beezer on November 25, 2018, 12:34 pmI’m trying to optimize the performance of my forum and the biggest problem I have at the moment is the delivery of images. The uploaded images are downloading in full when someone views a thread even if they’re displayed as a thumbnail. Normally, Wordpress would auto-generate different image variants and then I’d be able to serve different sizes with a “responsive images” plugin. However Asgaros forum is not using the media library so none of this works.
So, before I dive in and try to create a plugin, is there any recommended way of serving thumbnails, etc? Perhaps some plugin that works with the forum?
I’m trying to optimize the performance of my forum and the biggest problem I have at the moment is the delivery of images. The uploaded images are downloading in full when someone views a thread even if they’re displayed as a thumbnail. Normally, Wordpress would auto-generate different image variants and then I’d be able to serve different sizes with a “responsive images” plugin. However Asgaros forum is not using the media library so none of this works.
So, before I dive in and try to create a plugin, is there any recommended way of serving thumbnails, etc? Perhaps some plugin that works with the forum?
Quote from Beezer on November 25, 2018, 4:12 pmSo, I ended up diving in after all š
I had hoped to use a hook or filter to add the functionality I needed but seems I can’t get at the attachments. So I looked in “forum-uploads.php” and found that the function “show_uploaded_files” was using “wp_get_image_editor” but never doing anything with it. I modified it as so, and now it’s creating a thumbnail if one doesn’t exist. Any chance we can get something similar added officially?
if ($imageThumbnail && !is_wp_error($imageThumbnail)) { $dir_name = strtolower(pathinfo($upload, PATHINFO_DIRNAME)); $file_name = pathinfo($upload, PATHINFO_FILENAME); $file_extension = strtolower(pathinfo($upload, PATHINFO_EXTENSION)); $new_file = $dir_name.$file_name.'-thumbnail.'.$file_extension; if (!is_file($path.wp_basename($new_file))) { // Create thumbnail $imageThumbnail->resize( 300, 300, true ); $imageThumbnail->save($path.wp_basename($new_file)); } $uploadedFiles .= '<li><a class="uploaded-file" href="'.$url.utf8_uri_encode($upload).'" target="_blank"><img class="resize" src="'.$url.utf8_uri_encode($new_file).'" alt="'.$upload.'" /></a></li>'; } else { $uploadedFiles .= '<li><a class="uploaded-file" href="'.$url.utf8_uri_encode($upload).'" target="_blank">'.$upload.'</a></li>'; }
So, I ended up diving in after all š
I had hoped to use a hook or filter to add the functionality I needed but seems I can’t get at the attachments. So I looked in “forum-uploads.php” and found that the function “show_uploaded_files” was using “wp_get_image_editor” but never doing anything with it. I modified it as so, and now it’s creating a thumbnail if one doesn’t exist. Any chance we can get something similar added officially?
if ($imageThumbnail && !is_wp_error($imageThumbnail)) { $dir_name = strtolower(pathinfo($upload, PATHINFO_DIRNAME)); $file_name = pathinfo($upload, PATHINFO_FILENAME); $file_extension = strtolower(pathinfo($upload, PATHINFO_EXTENSION)); $new_file = $dir_name.$file_name.'-thumbnail.'.$file_extension; if (!is_file($path.wp_basename($new_file))) { // Create thumbnail $imageThumbnail->resize( 300, 300, true ); $imageThumbnail->save($path.wp_basename($new_file)); } $uploadedFiles .= '<li><a class="uploaded-file" href="'.$url.utf8_uri_encode($upload).'" target="_blank"><img class="resize" src="'.$url.utf8_uri_encode($new_file).'" alt="'.$upload.'" /></a></li>'; } else { $uploadedFiles .= '<li><a class="uploaded-file" href="'.$url.utf8_uri_encode($upload).'" target="_blank">'.$upload.'</a></li>'; }
Quote from Asgaros on November 25, 2018, 6:38 pmHello @beezer
I also was just looking into it. You are right, I call theĀ wp_get_image_editor function to check if its a file which can be included as an image without creating “real” thumbnails (just resized ones via css).
One way would be to create the thumbnail during the post-saving process but we need to find some good fallbacks when something is going wrong – especially in combination with malformed files or special characters.
Another way would be yours: Create thumbnails on the fly when they dont exist. Fallbacks are needed here as well, for example: You upload an image.png file; an image-thumbnail.png file gets created. So what happens when you upload another file calledĀ image-thumbnail.png?
As you can see we have to handle a lot of things here and I am not happy with the current implementation. Thats why I want to switch to the WordPress media-handling in the future. The only reason why I am still using a custom implementation is the lack of certain API-functions – but to be honest its already a while since I checked it last time.
I will definitely have a look into it but currently I still have some other high-priority functionalities on my todo-list for which users were waiting since a long time: topic-approval, global stickys, buddypress-integration, ads-management, etc.
If you have an idea for some good hook/filter-solution for your custom-code, please let me know. š
Hello @beezer
I also was just looking into it. You are right, I call theĀ wp_get_image_editor function to check if its a file which can be included as an image without creating “real” thumbnails (just resized ones via css).
One way would be to create the thumbnail during the post-saving process but we need to find some good fallbacks when something is going wrong – especially in combination with malformed files or special characters.
Another way would be yours: Create thumbnails on the fly when they dont exist. Fallbacks are needed here as well, for example: You upload an image.png file; an image-thumbnail.png file gets created. So what happens when you upload another file calledĀ image-thumbnail.png?
As you can see we have to handle a lot of things here and I am not happy with the current implementation. Thats why I want to switch to the WordPress media-handling in the future. The only reason why I am still using a custom implementation is the lack of certain API-functions – but to be honest its already a while since I checked it last time.
I will definitely have a look into it but currently I still have some other high-priority functionalities on my todo-list for which users were waiting since a long time: topic-approval, global stickys, buddypress-integration, ads-management, etc.
If you have an idea for some good hook/filter-solution for your custom-code, please let me know. š
Quote from Beezer on November 25, 2018, 8:49 pmI would be good to use Wordpress media library instead so any standard media optimization plugin works out of the box, but you’ll also have to consider how to migrate all existing uploaded content.
Doing the thumbnail generation on upload would be ideal rather than on-the-fly because then it wouldn’t slow down browsing. However, I think we need both mechanisms so that we catch both new content and existing content (that doesn’t have a thumbnail).
You should also consider if it’s worth creating more than 1 variant image and use img srcset to allow the browser to select which one he wants to use based on screen size, screen density, etc.
I’ll have a think about a hook/filter that might make a plugin possible as a short term fix.
I would be good to use Wordpress media library instead so any standard media optimization plugin works out of the box, but you’ll also have to consider how to migrate all existing uploaded content.
Doing the thumbnail generation on upload would be ideal rather than on-the-fly because then it wouldn’t slow down browsing. However, I think we need both mechanisms so that we catch both new content and existing content (that doesn’t have a thumbnail).
You should also consider if it’s worth creating more than 1 variant image and use img srcset to allow the browser to select which one he wants to use based on screen size, screen density, etc.
I’ll have a think about a hook/filter that might make a plugin possible as a short term fix.
Quote from Beezer on November 26, 2018, 10:54 pmI was reading up about Image proxies today and learnt about Photon and how Jetpack uses it. So, I turned on image optimization in Jetpack and what do you know, it resizes and optimizes the attachments in the forum, out of the box with no tweaks.
It could be better with careful use of the Photon API https://developer.wordpress.com/docs/photon/api/. Might be worth adding a configuration option to utilize that.
In fact, adding a width=300 attribute to the img tag makes Jetpack+Photon automatically do its thing.
I was reading up about Image proxies today and learnt about Photon and how Jetpack uses it. So, I turned on image optimization in Jetpack and what do you know, it resizes and optimizes the attachments in the forum, out of the box with no tweaks.
It could be better with careful use of the Photon API https://developer.wordpress.com/docs/photon/api/. Might be worth adding a configuration option to utilize that.
In fact, adding a width=300 attribute to the img tag makes Jetpack+Photon automatically do its thing.
Quote from Beezer on December 4, 2018, 1:25 pmMy cheap provider (one.com) for some reason has very poor disk read speeds. If I replace the “wp_get_image_editor” code inĀ “forum-uploads.php” with the following code, it speeds up quite noticeably. Obviously this isn’t as “safe” as actually checking if it’s an image or not, but it’s faster.
if (in_array($file_extension, array('jpg','jpeg','gif','png'))) {
My cheap provider (one.com) for some reason has very poor disk read speeds. If I replace the “wp_get_image_editor” code inĀ “forum-uploads.php” with the following code, it speeds up quite noticeably. Obviously this isn’t as “safe” as actually checking if it’s an image or not, but it’s faster.
if (in_array($file_extension, array('jpg','jpeg','gif','png'))) {
Quote from Asgaros on December 4, 2018, 6:30 pmHello @beezer
Actually this is fast but users can write scripts (PHP) in a file and save them with a file-extension of an image. In this case it would pass your upload-check and it will be possible for attackers to run any code on your server (delete/read/write data, etc).
I would not suggest to use this!
Hello @beezer
Actually this is fast but users can write scripts (PHP) in a file and save them with a file-extension of an image. In this case it would pass your upload-check and it will be possible for attackers to run any code on your server (delete/read/write data, etc).
I would not suggest to use this!
Quote from Beezer on December 4, 2018, 6:34 pmAsgaros, this is in the show_uploaded_files function, where it populates the img tag. Surely if there’s a risk of upload of malware, then the check should be on upload not on usage?
Asgaros, this is in the show_uploaded_files function, where it populates the img tag. Surely if there’s a risk of upload of malware, then the check should be on upload not on usage?
Quote from Asgaros on December 4, 2018, 6:37 pm@beezer Yes, you are right. If you only change it in the displaying-part then it should be not so problematic.
@beezer Yes, you are right. If you only change it in the displaying-part then it should be not so problematic.
Quote from JohniGo on December 8, 2018, 9:20 pmHello, @asgaros !
Please tell me can you implement in plugin settings:
– Specify the size of the thumbnails to display in the message .
– Place the thumbnails in the message not vertically, but horizontally.
Hello, @asgaros !
Please tell me can you implement in plugin settings:
– Specify the size of the thumbnails to display in the message .
– Place the thumbnails in the message not vertically, but horizontally.