picarica Posted December 2, 2021 Share Posted December 2, 2021 hello so i am trying to resize all image when uploading, or when saving, i tried so hard but i failed, i tried firstly when saving, to foreach every image and resize them down and then delete the old one Result? the newly generated image was blank, 0x0 0bytes , empty files i dont know why here is code to that <?php #$this->addHookBefore('Pages::saveReady', function(HookEvent $event) { $wire->addHookBefore("InputfieldFile::fileAdded", function(HookEvent $event) { // Get the object the event occurred on, if needed $pages = $event->object; // An 'after' hook can retrieve and/or modify the return value $return = $event->return; // Get values of arguments sent to hook (if needed) $page = $event->arguments(0); $event->return = $return; foreach($page->repeat_page as $repeat) { foreach($repeat->image_slider as $slider_i) { if ($slider_i->width > 1601 || $slider_i->height > 1601) { $slider_i_w = $slider_i->width; $slider_i_h = $slider_i->height; if ($slider_i_h > $slider_i_w) { $which = "height"; } if ($which == "height") { $slider_new = $slider_i->size(0,1600); } else { $slider_new = $slider_i->size(1600, 0); } $page->save(); //$repeat->image_slider->replace($slider_i->filename, $slider_new->filename); $repeat->image_slider->insertBefore($slider_new, $slider_i); $page->save(); //$repeat->image_slider = $slider_new->filename; $repeat->image_slider->delete($slider_i); $page->save(); } } foreach($page->repeat_page as $repeat) { } } $event->return = $return; }); ?> so this failed but it worked when the old one wasnt deleted, but only after saving second time, also failed to mention all field are in repeat field my next attempt was this wire()->addHookAfter('InputfieldFile::fileAdded', function($event) { $inputField = $event->object; $image = $event->argumentsByName('pagefile'); if ($inputField->name == 'image_slider') { // Do whatever you want with $image here $image->size(0, 1600, ['upscaling' => true]); } // Process other image fields here }); this also failed, infinite hang never completes not sure why? i dont understand hooks that much what am i doing wrong Link to comment Share on other sites More sharing options...
ngrmm Posted December 2, 2021 Share Posted December 2, 2021 2 hours ago, picarica said: <?php if ($which == "height") { $slider_new = $slider_i->size(0,1600); } else { $slider_new = $slider_i->size(1600, 0); } ?> maybe because of the 0 values for x and y? btw, do you do that? Link to comment Share on other sites More sharing options...
Zeka Posted December 2, 2021 Share Posted December 2, 2021 ... Link to comment Share on other sites More sharing options...
monollonom Posted December 2, 2021 Share Posted December 2, 2021 Hmm, I think there's a few mistakes here. You should be hooking to InputfieldImage::fileAdded() instead of InputfieldFile::fileAdded() as you wouldn't want to resize files. Also in your first code you're trying to resize every images along with the current one uploaded, I don't think there's a need for that. Your hook could simply be: $this->addHookAfter('InputfieldImage::fileAdded', function(HookEvent $event) { if ($event->object->name === 'image_slider') return; $image = $event->arguments(0); // no need to save the page or the image as it is automatically generated / saved as a variation if($image->width > $image->height) { $image->width(1600, true); // from the doc: a boolean is assumed to be for "upscaling" } else { $image->height(1600, true); } }); And then afterwards you can call your image with: $image = $page->images->first(); $image = $image->width > $image->height ? $image->width(1600) : $image->height(1600); echo $image->url; However seeing how you check for a max-width/height why not just rely on the admin UI, in the image field settings ("Input" tab), to simply set a max size ? 2 Link to comment Share on other sites More sharing options...
Zeka Posted December 2, 2021 Share Posted December 2, 2021 4 hours ago, monollonom said: $this->addHookAfter('InputfieldImage::fileAdded', function(HookEvent $event) { if ($event->object->name === 'image_slider') return; $image = $event->arguments(0); // no need to save the page or the image as it is automatically generated / saved as a variation if($image->width > $image->height) { $image->width(1600, true); // from the doc: a boolean is assumed to be for "upscaling" } else { $image->height(1600, true); } }); In this way it will generate an image variation and the original image will stay untouched, but as I understood @picaricawant to resize the original image. If so, you can do something like $this->addHookAfter('InputfieldImage::fileAdded', function(HookEvent $event) { $inputfield = $event->object; if ($inputfield->hasField != 'image' && $inputfield->hasField != 'images') return; $image = $event->argumentsByName("pagefile"); $maxSize = 780; $portrait = $image->height > $image->width; $property = $portrait ? 'height' : 'width'; $width = $portrait ? 0 : $maxSize; $height = $portrait ? $maxSize : 0; // if the current size exceeds the max, proceed a resize if ($image->$property >= $maxSize) { $is = new ImageSizer($image->filename, ['quality' => 100]); $done = $is->resize($width, $height); } }); 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now