How it would be possible to resize the images when uploading them?
There exists i.e. the function:
$image->resizeByWidth(width)
Where it can be implemented for having a resizing of the images
which are uploaded?
How could it be added to the following example of upload:
class GalleryPage extends Page {
private static $many_many = array(
'GalleryImages' => 'Image'
);
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab(
'Root.Upload',
$uploadField = new UploadField(
$name = 'GalleryImages',
$title = 'Upload one or more images (max 10 in total)'
)
);
$uploadField->setAllowedMaxFileNumber(10);
return $fields;
}
Thank you
It's done automatically by the CMS. When you go to use the image in your template, you tell it the dimensions, and it will be resized as soon as you save or publish (if it hasn't been already)
If you're concerned about disk space from your CMS users uploading RAW files e.g. 5MB JPGs, then you can actually resample on upload as asked in your question and then further resize as needed within your templates.
If you're using SilverStripe 2.4+ then you could use this ResampleUpload extension. It resamples the image on upload and deletes the original.
If you're using SilverStripe 3+ then you'd need to change the above extension to extend DataExtension instead of DataObjectDecorator. I've not used this under SS3 myself so there may be other minor tweaks required but looking at it I can't see any changes needed as it's modifying the onAfterUpload and onAfterWrite methods which still exist on the Image class in SS 3.
As stated before, no out of box functionality for this in SS 3. This mod offers the solution you're looking for though: https://github.com/heyday/silverstripe-optimisedimage
First Install the mod in root.
Then write the code below in your mysite/_config/config.yml to activate image resampling (resizing).
Image:
extensions:
- ResampleImage
In the same config.yml file, write the max resolution (x = width, y = height obviously). If user uploads a bigger res photo, it will be scaled down (maintaining aspect ratio) to fit within the given max res dimensions.
ResampleImage:
max_x: 2000
max_y: 2000
Related
I actually create an extranet space on my website, with pages where you must to be logged-in to see the content.
My problem is about media uploads.
I want to make them private for a specific custom post type, and upload them in a sub directory (uploads/private).
I use the upload_dir filter (add_filter('upload_dir', 'extranet_upload_directory');) with success for thumbnail for exemple, but when I upload an image in the content with a gutenberg block, this one is upload in the initial foldier (uploads), not in uploads/private.
Does anyone have an idea where to look at ?
My code below :
add_filter('upload_dir', 'extranet_upload_directory');
function extranet_upload_directory( $param ){
$id = $_REQUEST['post_id'];
if ( ( get_post_type( $id ) == 'test-extranet' ) ) {
$param['subdir'] = '/private' . $param['subdir'];
$param['path'] = $param['basedir'] . $param['subdir'];
$param['url'] = $param['baseurl'] . $param['subdir'];
}
return $param;
}
This may be a bug... I have a plugin that offers the same functionality as yours and noticed the following:
While creating a post of that new custom post type (e.g. PATHTOsite.fr/wp-admin/post-new.php?post_type=post_type_name):
When I immediately press upload after opening an new image block the uploaded image goes to default directory, uploads.
but, if I first press 'media library' after opening an new image block and then go to the upload button there and upload the image there, the image will be uploaded in the modified uploads directory.
I want to generate an extra image size in WordPress. WP lets you use add_image_size like this:
// Make sure featured images are enabled
add_theme_support( 'post-thumbnails' );
// Add featured image sizes
add_image_size( 'original-img', 2000, 99999 );
If the image has the min. width of 2000px, the image size will be generated.
However if the image is smaller the custom size won't be generated.
Is there any option to create an image for the custom image size everytime, no matter of the uploaded image size? Or maybe its possible to additionally save the original image?
To explain why I am trying to achieve: I need a watermark on my images, but need also the original image (or a very big image) as well without the watermark.
I also tried with the "large" image, but here the same problem. By uploading an image smaller than 1024px, the image size wouldn't be created.
You can hook into wp_generate_attachment_metadata and make a copy of the uploaded image there if it wasn't already created. I've included the code to do this below.
Add this to your functions.php. Each step is commented to explain what its doing, but basically during the upload process:
we check if WP created your custom size
If it was, do nothing
Otherwise create a copy of the image called imagename-WxH.extn (e.g.if the uploaded image was cat.jpg and was 700x520px, our copy will be called cat-700x520px.jpg)
Add the copied image as your custom size original-img
Then you can use your original-img custom size in your code and always have an image.
// Make sure featured images are enabled
add_theme_support( 'post-thumbnails' );
// Add featured image sizes
add_image_size( 'original-img', 2000, 99999 );
// Hook into the upload process
add_filter('wp_generate_attachment_metadata','copy_original_img');
// Check if the original image was added, if not make a copy and add it as original-img
function copy_original_img($image_data) {
// if the original-img was created, we don't need to do anything
if (isset($image_data['sizes']['original-img']))
return;
// 1. make a copy of the uploaded image to use for original-img
$upload_dir = wp_upload_dir();
$uploaded_img = $image_data['file'];
$img_w = $image_data['width'];
$img_h = $image_data['height'];
// construct the filename for the copy in the format: imagename-WxH.extn
$path_parts = pathinfo($uploaded_img);
$new_img = $path_parts['filename']."-".$img_w."x".$img_h.".".$path_parts['extension'];
// make a copy of the image
$img_to_copy = $upload_dir['path'] . '/'.$uploaded_img;
$new_img_path = $upload_dir['path'] . '/'.$new_img;
$success = copy($img_to_copy,$new_img_path);
// 2. If the image was copied successfully, add it into the image_data to be returned:
if ($success){
$image_data['sizes']['original-img']['file'] = $new_img;
$image_data['sizes']['original-img']['width'] = $img_w ; // same as uploaded width
$image_data['sizes']['original-img']['height'] = $img_h; // same as uploaded height
$image_data['sizes']['original-img']['mime-type'] = mime_content_type($new_img_path);
}
return $image_data;
}
I would like to let Wordpress calculate the aspect ratio & orientation when the media is uploaded instead of doing this in the templates. Then I can call images and ask for image.orientation which will give me 'portrait' or 'landscape' as a value.
Example, how it would look like after uploading an image:
Is this possible or do I have to do this in the frontend?
You can write some code for it. Use http://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata to get the width and the height, and then if $width/$height > 1 => landscape, otherwise its portrait. Store this value in the attachment using: http://codex.wordpress.org/Function_Reference/wp_update_attachment_metadata.
You can use an action for this, such as:
add_action('add_attachment', 'process_images');
Adding the solution as well, following from your code attempt (I HAVENT TESTED THIS):
function attachment_orientation_meta($id){
$attachmentInfo = wp_get_attachment_metadata($id);
$attachmentOrientation = ( $attachmentInfo['height'] > $attachmentInfo['width'] ) ? 'portrait' : 'landscape';
update_post_meta($id, 'orientation', $attachmentOrientation);
}
add_action( 'add_attachment', 'attachment_orientation_meta' );
I'm coding a shortcode that accept one media ID as an attribute. This shortcode should then display the image at an existing size registered with add_image_size. Problem : my image is not displayed at the correct size.
Explanations :
My media image has been uploaded in the WP library. The original file size is huge (1227x924). The media has the ID 294.
I registered an image size in functions.php :
function my_after_setup_theme(){
add_image_size('my-image-size', 210, 136, true);
}
add_action('after_setup_theme', 'my_after_setup_theme');
I insert my shortcode in one of my pages : [my_shortcode imageid="294"]
My shortcode code is :
function my_shortcode_func($atts)
{
$a = shortcode_atts(array(
'imageid' => 0,
), $atts);
if ($a['imageid'] == 0) {
// default placeholder image
$img = 'img src="http://placehold.it/210x136"/>';
} else {
// get resized (NOT WORKING !)
$img = wp_get_attachment_image($a['imageid'], 'my-image-size');
}
return $img;
}
add_shortcode('my_shortcode', 'my_shortcode_func');
I expect the original image to be resized at the correct size (ie : 210x136) in a new thumbnail file. Instead, the $img displays the original image (1227x924) , displayed at an intermediate size via the width and height attributes of the HTML <img> tag :
What am I doing wrong ?
thanks for help.
Did you register the thumbnail size after uploading the media? If so, you will need to use a plugin to regenerate the thumbnails.
Additionally, I don't believe you need to wrap thumbnail size declarations as you have done, I have the following;
if (function_exists('add_theme_support'))
{
add_theme_support('post-thumbnails'); // Featured image support for posts - you may not need this bit.
add_image_size('large', 700, '', true); // Large Thumbnail - This is the bit you would need
}
So it would be worth checking if your thumbnails are actually be generated to start with, as I have never done it the way you have defined in your question.
I'm building a site with Drupal 7.2, and using several useful modules (Views, Display Suite, and 20+ more).
I've added an image field in a content type (article), and set the 'Field Settings' -> 'number of values' to 5, which means users can upload 5 images for this field.
Under 'Full Content' view mode, I'd like to display all the images, but How DO I display only ONE image under 'Teaser' mode? Is there any modules can do this?
I solved this problem by adding a new template for this field type like field--field_image.tpl.php in my case with the following code:
// Reduce image array to single image in teaser view mode
if ($element['#view_mode'] == 'teaser') {
$items = array(reset($items));
}
print render($items);
Hope this helps.
Edit: Here's the (probably) correct way to do it:
function MYTHEME_process_field(&$vars) {
$element = $vars['element'];
// Field type image
if ($element['#field_type'] == 'image') {
// Reduce number of images in teaser view mode to single image
if ($element['#view_mode'] == 'teaser') {
$item = reset($vars['items']);
$vars['items'] = array($item);
}
}
}
It did not work for me, i had to adjust the code sligthly:
function MYTHEME_process_field(&$vars) {
$element = $vars['element'];
// Reduce number of images in teaser view mode to single image
if ($element['#view_mode'] == 'node_teaser' && $element['#field_type'] == 'image') {
$vars['items'] = array($vars['items'][0]);
}
}
One option without having to change code is to use the module Field Multiple Limit. With this module you can choose how many items to show in which view for fields that allow for multiple entries.
Then on the teaser view of the field you can choose how many items to show or to skip.
Just saw this is already answered in Drupal Stack Exchange
function MYTHEME_process_field(&$vars) {
$element = $vars['element'];
// Reduce number of images in teaser view mode to single image
if ($element['#view_mode'] == 'teaser' && $element['#field_type'] == 'image') {
$vars['items'] = array($vars['items'][0]);
}
}
I've changed node_teaser to teaser and it worked for me. Drupal v 7.19
P.S. Do not forget to flush a cache.