Join our new community forum for support & discussion
Join NowLimit image dimension on profile picture with filter?
-
AuthorPosts
-
kvkvn 1 year, 6 months ago
Regardless “Media” settings of wordpress.
I’ve been searching for few days on google and here and still can’t figure it out 😐
I need users to be allowed to only upload 512×512, but this to be only effective in profile settings, not listings.
Sorry!kvkvn 1 year, 6 months agoadd_filter( 'hivepress/v1/models/vendor', function( $model ) { if ( isset( $model['fields']['image'] ) ) { $model['fields']['image']['required'] = true; } return $model; }, 1000 );
This makes the profile picture required, so I suppose the missing part for image dimensions should be just under
$model['fields']['image']['required'] = true;
, but I can not figure out how to write the condition.Sorry, there’s no way to restrict this, but you can adjust the media settings (e.g. change the Square (small) size to 512×512 if required), and any larger image will be cropped automatically. You can also limit the maximum upload file size https://www.cloudways.com/blog/increase-media-file-maximum-upload-size-in-wordpress/
kvkvn 1 year, 6 months agoCould forms from filters like this :
add_filter( 'hivepress/v1/forms/user_update', function( $form ) { if ( isset( $form['fields']['image'] ) ) { $form['fields']['image']['required'] = true; } return $form; }, 1000 );
be used in some conditional logic to determine from which current page the user is trying to upload an image, so based on that to load my own upload rules?
So far i tried to compare
$current_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
and
$accURL = 'http://localhost/account/settings';
To
if ($current_url == $accURL ){ // blah blah }
But it seems such is not doing it, how can you determine on which hivepress page the user is as even the above matches the pages it seems that stuff is happening on some modular level?
All I’m trying to do is to set different upload rules for the upload of listings and profile pictures.kvkvn 1 year, 6 months agoFound out that this will work, but it works for both profile picture and listing picture at the same time.
It limits the upload size to 4MB and it also limits the uploaded file to 1280×1280
How can the bellow code be altered to declare different dimensions for profile picture and listing images?
It just needs some condition that states ‘if this is profile update page do this, if this is listing submit page do that’add_filter('wp_handle_upload_prefilter','mdu_validate_image_size'); function mdu_validate_image_size( $file ) { // Calculate the image size in KB $image_size = $file['size']/1024; // File size limit in KB $limit = 4000; $image = getimagesize($file['tmp_name']); $maximum = array( 'width' => '1280', 'height' => '1280' ); $image_width = $image[0]; $image_height = $image[1]; $too_big = "File file size is too big. It needs to be smaller than $limit KB. Please save photos as jpg no larger than {$maximum['width']} by {$maximum['height']} pixels."; $too_large = "File dimensions are too large. Maximum size is {$maximum['width']} by {$maximum['height']} pixels. Uploaded image is $image_width by $image_height pixels."; // Check if it's an image $is_image = strpos($file['type'], 'image/gif'); if ( ( $image_size > $limit ) && ($is_image !== false) ) { //add in the field 'error' of the $file array the message $file['error'] = $too_big; return $file; } elseif ( $image_width > $maximum['width'] || $image_height > $maximum['height'] ) { //add in the field 'error' of the $file array the message $file['error'] = $too_large; return $file; } else return $file; }
Sorry, there’s no easy way to do this because at the time of uploading there are no file details that can be used to detect the current form or page (in the available WordPress filters/actions). I highly recommend adjusting the image size so WordPress will resize and crop it automatically, the original uploaded image will not be used nor displayed anyway. You can limit the upload size globally to avoid storing files that are too big, allowing only regular photos/images. Also, if you want to save space you can offload media to some CDN like Amazon S3 and don’t store it locally https://wordpress.org/plugins/amazon-s3-and-cloudfront/
kvkvn 1 year, 6 months agoIf anyone else knows how to include the above code to work differently on different pages, please reply.
aportodas 10 months, 2 weeks agoHello ihor in the answer you gave before you comment on this solution …
You can limit the upload size globally to avoid storing files that are too big, allowing only regular photos/images.
But how is it done?By default, WordPress generates a separate image for every registered images size, if you don’t use other images sizes (HivePress image sizes have “hp_” and “ht_” prefix) you can disable these https://wordpress.org/plugins/image-sizes/
aportodas 10 months, 2 weeks agoI don’t mean that.
What I propose is that if a user decides to upload an image from a 25-megay listing, he will not be allowed. Because my web hosting server will fill up in a few days. Therefore, what I need is just to let, for example, upload photos of at most 3 megabytes.
You can restrict the upload file size at WordPress or PHP configuration level https://www.cloudways.com/blog/increase-media-file-maximum-upload-size-in-wordpress/
-
AuthorPosts
New Reply
This forum has been archived and is no longer accepting new posts or replies. Please join our new community forum for support & discussion.