XF 2.2 Build an addon with a resize icon to maintain the aspect ratio issue

Yenxji

Active member
When you are trying to upload an image that would resize the image to 320x320, but XenForo built self keeps using Maintain Aspect Ratio to adjust the image, is there any method to prevent Maintain Aspect Ratio and let resize the image only?

PHP:
        if ($upload = $this->request->getFile('image_icons')) {
            if ($upload->isValid()) {
                $dataDir = \XF::getRootDirectory() . '/data/image_icons/';
                \XF\Util\File::createDirectory($dataDir, false);
              
                $fileName = $mandoo->mandoo_id . '.jpg';
                $tempFile = $upload->getTempFile();
              
                if ($tempFile) {
                    $imageManager = \XF::app()->imageManager();
                  
                    $image = $imageManager->imageFromFile($tempFile);
                  
                    if ($image) {
                        $targetWidth = 320;
                        $targetHeight = 320;
                      
                        $image->resizeAndCrop($targetWidth, $targetHeight);
                      
                        if ($image->save($dataDir . $fileName)) {
                            $mandoo->fastUpdate('new_image_date', \XF::$time);
                        }
                    }
                }
            }
        }
 
Last edited:
Use \XF\Image\AbstractDriver::resize (instead of resizeAndCrop).
It still has the same issue, I tried to replace this and decided to upload a feature image cover on AdminCP and got the error:


My first method:
Code:
Error: Non-static method XF\Image\AbstractDriver::resize() cannot be called statically in src\addons\ELF\ELFMandoo\Admin\Controller\mandoo.php at line 147
ELF\ELFMandoo\Admin\Controller\mandoo->actionSave() in src\XF\Mvc\Dispatcher.php at line 352
XF\Mvc\Dispatcher->dispatchClass() in src\XF\Mvc\Dispatcher.php at line 258
XF\Mvc\Dispatcher->dispatchFromMatch() in src\XF\Mvc\Dispatcher.php at line 115
XF\Mvc\Dispatcher->dispatchLoop() in src\XF\Mvc\Dispatcher.php at line 57
XF\Mvc\Dispatcher->run() in src\XF\App.php at line 2485
XF\App->run() in src\XF.php at line 524
XF::runApp() in admin.php at line 13

PHP:
        if ($upload = $this->request->getFile('image_icons')) {
            if ($upload->isValid()) {
                $dataDir = \XF::getRootDirectory() . '/data/image_icons/';
                \XF\Util\File::createDirectory($dataDir, false);
              
                $fileName = $mandoo->mandoo_id . '.jpg';
                $tempFile = $upload->getTempFile();
              
                if ($tempFile) {
                    $imageManager = \XF::app()->imageManager();
                    $image = $imageManager->imageFromFile($tempFile);
                  
                    if ($image) {
                        \XF\Image\AbstractDriver::resize($image, 320, 320);
                      
                        if ($image->save($dataDir . $fileName, IMAGETYPE_JPEG)) {
                            $mandoo->fastUpdate('feature_image_date', \XF::$time);
                        }
                    }
                }
            }
        }

Your method:
Code:
ParseError: syntax error, unexpected fully qualified name "\XF\Image\AbstractDriver", expecting identifier or variable or "{" or "$" in src\addons\ELF\ELFMandoo\Admin\Controller\mandoo.php at line 151
[LIST=1]
[*]Composer\Autoload\{closure}() in src\vendor\composer\ClassLoader.php at line 427
[*]Composer\Autoload\ClassLoader->loadClass()
[*]class_exists() in src\XF\Container.php at line 267
[*]XF\Container->createObject() in src\XF\App.php at line 1623
[*]XF\App->XF\{closure}() in src\XF\Container.php at line 234
[*]XF\Container->create() in src\XF\App.php at line 2960
[*]XF\App->controller() in src\XF\Mvc\Dispatcher.php at line 291
[*]XF\Mvc\Dispatcher->dispatchClass() in src\XF\Mvc\Dispatcher.php at line 258
[*]XF\Mvc\Dispatcher->dispatchFromMatch() in src\XF\Mvc\Dispatcher.php at line 115
[*]XF\Mvc\Dispatcher->dispatchLoop() in src\XF\Mvc\Dispatcher.php at line 57
[*]XF\Mvc\Dispatcher->run() in src\XF\App.php at line 2485
[*]XF\App->run() in src\XF.php at line 524
[*]XF::runApp() in admin.php at line 13
[/LIST]

PHP:
        if ($upload = $this->request->getFile('image_icons')) {
            if ($upload->isValid()) {
                $dataDir = \XF::getRootDirectory() . '/data/image_icons/';
                \XF\Util\File::createDirectory($dataDir, false);
             
                $fileName = $mandoo->mandoo_id . '.jpg';
                $tempFile = $upload->getTempFile();
             
                if ($tempFile) {
                    $imageManager = \XF::app()->imageManager();
                 
                    $image = $imageManager->imageFromFile($tempFile);
                 
                    if ($image) {
                        $targetWidth = 320;
                        $targetHeight = 320;
                  
                        $image->\XF\Image\AbstractDriver::resize($targetWidth, $targetHeight);
                  
                        if ($image->save($dataDir . $fileName)) {
                            $mandoo->fastUpdate('new_image_date', \XF::$time);
                        }
                    }
                }
            }
        }
 
Last edited:
It still has the same issue, I tried to replace this and decided to upload a feature image cover on AdminCP and got the error:

PHP:
Error: Non-static method XF\Image\AbstractDriver::resize() cannot be called statically in src\addons\ELF\ELFMandoo\Admin\Controller\mandoo.php at line 147
ELF\ELFMandoo\Admin\Controller\mandoo->actionSave() in src\XF\Mvc\Dispatcher.php at line 352
XF\Mvc\Dispatcher->dispatchClass() in src\XF\Mvc\Dispatcher.php at line 258
XF\Mvc\Dispatcher->dispatchFromMatch() in src\XF\Mvc\Dispatcher.php at line 115
XF\Mvc\Dispatcher->dispatchLoop() in src\XF\Mvc\Dispatcher.php at line 57
XF\Mvc\Dispatcher->run() in src\XF\App.php at line 2485
XF\App->run() in src\XF.php at line 524
XF::runApp() in admin.php at line 13
XF/Http/Upload::transformImage for an example of use
 
XF/Http/Upload::transformImage for an example of use
I used your method, looks like it was saved but it doesn't upload the image successfully also doesn't give an error and console browser error


Here I tried to use this,
Code:
$image->XF/Image/AbstractDriver::resize($image, $targetWidth, $targetHeight);

Code:
$image->XF/Http/Upload::transformImage($image, $targetWidth, $targetHeight);
 
This is not valid PHP. $image->resize should be your call with the same arguments.
If I use this and won't resize to 320x320, the image was just resized to random like 480x510 or 620x290

The image original was 1200x1274

XenForo Admin Template elf_admin_mandoo_edit:

Code:
                        <div class="contentRow-main">
                            <xf:upload name="image_icons"
                                accept=".jpg,.jpeg,.png"
                                maxsize="{{ $xf.options.attachmentMaxFileSize }}" />
                            <ul class="listInline">
                                <li>
                                    <xf:button href="{{ link('elfmandoo/mandoo/delete-icon', $mandoo) }}"
                                        class="button--link"
                                        data-xf-click="overlay"
                                        data-cache="false">
                                        {{ phrase('delete') }}
                                    </xf:button>
                                </li>
                            </ul>
                        </div>
 
Last edited:
Example,


PHP:
                    if ($image) {

                        $targetWidth = 480;
                        $targetHeight = 820;
                       
                        $image->resize($targetWidth, $targetHeight);

This was resized to 480x640
480x640.webp

PHP:
                    if ($image) {

                        $targetWidth = 480;
                        $targetHeight = 820;
                       
                        $image->resizeAndCrop($targetWidth, $targetHeight);

This was resized to 480x820 but with a maintain the aspect ratio / cropped:
480x820.webp


Original 1200x1600:
1733757564107.webp
 
Last edited:
Ah sorry, looking at the code this actually isn't supported. We maintain the aspect ratio in all cases, and the difference is whether we recalculate or crop the longer edge to do so. Admittedly for our use cases, changing the aspect ratio is undesirable as it distorts the image whereas cropping usually produces nicer results when an exact size is required.
 
Ah sorry, looking at the code this actually isn't supported. We maintain the aspect ratio in all cases, and the difference is whether we recalculate or crop the longer edge to do so. Admittedly for our use cases, changing the aspect ratio is undesirable as it distorts the image whereas cropping usually produces nicer results when an exact size is required.
That's unfortunate. I have to research another PHP source that can allow resizing without maintaining the aspect ratio.

Thank you for your reply back again
 
Back
Top Bottom