Magento2: How to remove extra white background from uploaded image from admin












0















Extra white background appears when the image uploaded is not the same dimension where it is requested.



I used resize code for image resizing:



if (!file_exists($imageResized)) { $imageResize = $this->_imageFactory->create(); $imageResize->open($absolutePath); $imageResize->constrainOnly(true); $imageResize->keepTransparency(true); $imageResize->keepFrame(true); $imageResize->keepAspectRatio(false); $imageResize->resize($width, $height); $destination = $imageResized; $imageResize->save($destination); }


For example, the image uploaded is 200x200 and it is called somewhere at a proportion of 300x200. So the image will resize to fit for the position without being stretched out rather resizing it to the proportion with extra white space. so How remove white background?
enter image description here










share|improve this question

























  • R u using any custom module for image resize or u have code? If have code then paste it

    – Ankit Shah
    Jan 13 '17 at 4:11











  • yes..i have resize code.

    – Magento2 Devloper
    Jan 13 '17 at 4:12











  • if (!file_exists($imageResized)) { $imageResize = $this->_imageFactory->create(); $imageResize->open($absolutePath); $imageResize->constrainOnly(true); $imageResize->keepTransparency(true); $imageResize->keepFrame(true); $imageResize->keepAspectRatio(false); $imageResize->resize($width, $height); $destination = $imageResized; $imageResize->save($destination); }

    – Magento2 Devloper
    Jan 13 '17 at 4:13
















0















Extra white background appears when the image uploaded is not the same dimension where it is requested.



I used resize code for image resizing:



if (!file_exists($imageResized)) { $imageResize = $this->_imageFactory->create(); $imageResize->open($absolutePath); $imageResize->constrainOnly(true); $imageResize->keepTransparency(true); $imageResize->keepFrame(true); $imageResize->keepAspectRatio(false); $imageResize->resize($width, $height); $destination = $imageResized; $imageResize->save($destination); }


For example, the image uploaded is 200x200 and it is called somewhere at a proportion of 300x200. So the image will resize to fit for the position without being stretched out rather resizing it to the proportion with extra white space. so How remove white background?
enter image description here










share|improve this question

























  • R u using any custom module for image resize or u have code? If have code then paste it

    – Ankit Shah
    Jan 13 '17 at 4:11











  • yes..i have resize code.

    – Magento2 Devloper
    Jan 13 '17 at 4:12











  • if (!file_exists($imageResized)) { $imageResize = $this->_imageFactory->create(); $imageResize->open($absolutePath); $imageResize->constrainOnly(true); $imageResize->keepTransparency(true); $imageResize->keepFrame(true); $imageResize->keepAspectRatio(false); $imageResize->resize($width, $height); $destination = $imageResized; $imageResize->save($destination); }

    – Magento2 Devloper
    Jan 13 '17 at 4:13














0












0








0








Extra white background appears when the image uploaded is not the same dimension where it is requested.



I used resize code for image resizing:



if (!file_exists($imageResized)) { $imageResize = $this->_imageFactory->create(); $imageResize->open($absolutePath); $imageResize->constrainOnly(true); $imageResize->keepTransparency(true); $imageResize->keepFrame(true); $imageResize->keepAspectRatio(false); $imageResize->resize($width, $height); $destination = $imageResized; $imageResize->save($destination); }


For example, the image uploaded is 200x200 and it is called somewhere at a proportion of 300x200. So the image will resize to fit for the position without being stretched out rather resizing it to the proportion with extra white space. so How remove white background?
enter image description here










share|improve this question
















Extra white background appears when the image uploaded is not the same dimension where it is requested.



I used resize code for image resizing:



if (!file_exists($imageResized)) { $imageResize = $this->_imageFactory->create(); $imageResize->open($absolutePath); $imageResize->constrainOnly(true); $imageResize->keepTransparency(true); $imageResize->keepFrame(true); $imageResize->keepAspectRatio(false); $imageResize->resize($width, $height); $destination = $imageResized; $imageResize->save($destination); }


For example, the image uploaded is 200x200 and it is called somewhere at a proportion of 300x200. So the image will resize to fit for the position without being stretched out rather resizing it to the proportion with extra white space. so How remove white background?
enter image description here







image-upload magento-2.1.3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 21 '17 at 6:56









Teja Bhagavan Kollepara

2,94841847




2,94841847










asked Jan 13 '17 at 4:10









Magento2 DevloperMagento2 Devloper

8371033




8371033













  • R u using any custom module for image resize or u have code? If have code then paste it

    – Ankit Shah
    Jan 13 '17 at 4:11











  • yes..i have resize code.

    – Magento2 Devloper
    Jan 13 '17 at 4:12











  • if (!file_exists($imageResized)) { $imageResize = $this->_imageFactory->create(); $imageResize->open($absolutePath); $imageResize->constrainOnly(true); $imageResize->keepTransparency(true); $imageResize->keepFrame(true); $imageResize->keepAspectRatio(false); $imageResize->resize($width, $height); $destination = $imageResized; $imageResize->save($destination); }

    – Magento2 Devloper
    Jan 13 '17 at 4:13



















  • R u using any custom module for image resize or u have code? If have code then paste it

    – Ankit Shah
    Jan 13 '17 at 4:11











  • yes..i have resize code.

    – Magento2 Devloper
    Jan 13 '17 at 4:12











  • if (!file_exists($imageResized)) { $imageResize = $this->_imageFactory->create(); $imageResize->open($absolutePath); $imageResize->constrainOnly(true); $imageResize->keepTransparency(true); $imageResize->keepFrame(true); $imageResize->keepAspectRatio(false); $imageResize->resize($width, $height); $destination = $imageResized; $imageResize->save($destination); }

    – Magento2 Devloper
    Jan 13 '17 at 4:13

















R u using any custom module for image resize or u have code? If have code then paste it

– Ankit Shah
Jan 13 '17 at 4:11





R u using any custom module for image resize or u have code? If have code then paste it

– Ankit Shah
Jan 13 '17 at 4:11













yes..i have resize code.

– Magento2 Devloper
Jan 13 '17 at 4:12





yes..i have resize code.

– Magento2 Devloper
Jan 13 '17 at 4:12













if (!file_exists($imageResized)) { $imageResize = $this->_imageFactory->create(); $imageResize->open($absolutePath); $imageResize->constrainOnly(true); $imageResize->keepTransparency(true); $imageResize->keepFrame(true); $imageResize->keepAspectRatio(false); $imageResize->resize($width, $height); $destination = $imageResized; $imageResize->save($destination); }

– Magento2 Devloper
Jan 13 '17 at 4:13





if (!file_exists($imageResized)) { $imageResize = $this->_imageFactory->create(); $imageResize->open($absolutePath); $imageResize->constrainOnly(true); $imageResize->keepTransparency(true); $imageResize->keepFrame(true); $imageResize->keepAspectRatio(false); $imageResize->resize($width, $height); $destination = $imageResized; $imageResize->save($destination); }

– Magento2 Devloper
Jan 13 '17 at 4:13










1 Answer
1






active

oldest

votes


















0














You need to replace your line,



$imageResize->keepFrame(true)



with



$imageResize->keepFrame(false)


This is my block file,



class YOURCLASS extends MagentoFrameworkAppActionAction
{
protected $_productloader;
protected $productimghelper;

public function __construct(Context $context,
MagentoCatalogModelProduct $productloader,
MagentoCatalogHelperImage $producthelper)
{

$this->_productloader = $productloader;
$this->productimghelper = $producthelper;
parent::__construct($context);
}

public function ProductImageData($Pid)
{
$product = $this->_productloader->load($Pid);
$imgURL = $this->productimghelper->init($product, 'category_page_list')->constrainOnly(FALSE)->keepAspectRatio(FALSE)->keepFrame(FALSE)->resize(300,200)->getUrl();
}
}


In my phtml file, I put code like this,



<img src="<?php echo $block->ProductImageData(123);?>"/>





share|improve this answer


























  • Thanx for your answer but its not working..,

    – Magento2 Devloper
    Jan 13 '17 at 4:38













  • you are trying to save new image in place of old image with your code?

    – Dhiren Vasoya
    Jan 13 '17 at 4:40











  • yes..i have save new image.

    – Magento2 Devloper
    Jan 13 '17 at 4:45











  • you need to used above code to show images, and apply above things there.

    – Dhiren Vasoya
    Jan 13 '17 at 4:46











  • I have used above code but not working..if you have code can you paste here?

    – Magento2 Devloper
    Jan 13 '17 at 5:16











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f154560%2fmagento2-how-to-remove-extra-white-background-from-uploaded-image-from-admin%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You need to replace your line,



$imageResize->keepFrame(true)



with



$imageResize->keepFrame(false)


This is my block file,



class YOURCLASS extends MagentoFrameworkAppActionAction
{
protected $_productloader;
protected $productimghelper;

public function __construct(Context $context,
MagentoCatalogModelProduct $productloader,
MagentoCatalogHelperImage $producthelper)
{

$this->_productloader = $productloader;
$this->productimghelper = $producthelper;
parent::__construct($context);
}

public function ProductImageData($Pid)
{
$product = $this->_productloader->load($Pid);
$imgURL = $this->productimghelper->init($product, 'category_page_list')->constrainOnly(FALSE)->keepAspectRatio(FALSE)->keepFrame(FALSE)->resize(300,200)->getUrl();
}
}


In my phtml file, I put code like this,



<img src="<?php echo $block->ProductImageData(123);?>"/>





share|improve this answer


























  • Thanx for your answer but its not working..,

    – Magento2 Devloper
    Jan 13 '17 at 4:38













  • you are trying to save new image in place of old image with your code?

    – Dhiren Vasoya
    Jan 13 '17 at 4:40











  • yes..i have save new image.

    – Magento2 Devloper
    Jan 13 '17 at 4:45











  • you need to used above code to show images, and apply above things there.

    – Dhiren Vasoya
    Jan 13 '17 at 4:46











  • I have used above code but not working..if you have code can you paste here?

    – Magento2 Devloper
    Jan 13 '17 at 5:16
















0














You need to replace your line,



$imageResize->keepFrame(true)



with



$imageResize->keepFrame(false)


This is my block file,



class YOURCLASS extends MagentoFrameworkAppActionAction
{
protected $_productloader;
protected $productimghelper;

public function __construct(Context $context,
MagentoCatalogModelProduct $productloader,
MagentoCatalogHelperImage $producthelper)
{

$this->_productloader = $productloader;
$this->productimghelper = $producthelper;
parent::__construct($context);
}

public function ProductImageData($Pid)
{
$product = $this->_productloader->load($Pid);
$imgURL = $this->productimghelper->init($product, 'category_page_list')->constrainOnly(FALSE)->keepAspectRatio(FALSE)->keepFrame(FALSE)->resize(300,200)->getUrl();
}
}


In my phtml file, I put code like this,



<img src="<?php echo $block->ProductImageData(123);?>"/>





share|improve this answer


























  • Thanx for your answer but its not working..,

    – Magento2 Devloper
    Jan 13 '17 at 4:38













  • you are trying to save new image in place of old image with your code?

    – Dhiren Vasoya
    Jan 13 '17 at 4:40











  • yes..i have save new image.

    – Magento2 Devloper
    Jan 13 '17 at 4:45











  • you need to used above code to show images, and apply above things there.

    – Dhiren Vasoya
    Jan 13 '17 at 4:46











  • I have used above code but not working..if you have code can you paste here?

    – Magento2 Devloper
    Jan 13 '17 at 5:16














0












0








0







You need to replace your line,



$imageResize->keepFrame(true)



with



$imageResize->keepFrame(false)


This is my block file,



class YOURCLASS extends MagentoFrameworkAppActionAction
{
protected $_productloader;
protected $productimghelper;

public function __construct(Context $context,
MagentoCatalogModelProduct $productloader,
MagentoCatalogHelperImage $producthelper)
{

$this->_productloader = $productloader;
$this->productimghelper = $producthelper;
parent::__construct($context);
}

public function ProductImageData($Pid)
{
$product = $this->_productloader->load($Pid);
$imgURL = $this->productimghelper->init($product, 'category_page_list')->constrainOnly(FALSE)->keepAspectRatio(FALSE)->keepFrame(FALSE)->resize(300,200)->getUrl();
}
}


In my phtml file, I put code like this,



<img src="<?php echo $block->ProductImageData(123);?>"/>





share|improve this answer















You need to replace your line,



$imageResize->keepFrame(true)



with



$imageResize->keepFrame(false)


This is my block file,



class YOURCLASS extends MagentoFrameworkAppActionAction
{
protected $_productloader;
protected $productimghelper;

public function __construct(Context $context,
MagentoCatalogModelProduct $productloader,
MagentoCatalogHelperImage $producthelper)
{

$this->_productloader = $productloader;
$this->productimghelper = $producthelper;
parent::__construct($context);
}

public function ProductImageData($Pid)
{
$product = $this->_productloader->load($Pid);
$imgURL = $this->productimghelper->init($product, 'category_page_list')->constrainOnly(FALSE)->keepAspectRatio(FALSE)->keepFrame(FALSE)->resize(300,200)->getUrl();
}
}


In my phtml file, I put code like this,



<img src="<?php echo $block->ProductImageData(123);?>"/>






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 13 '17 at 5:21

























answered Jan 13 '17 at 4:35









Dhiren VasoyaDhiren Vasoya

4,25551742




4,25551742













  • Thanx for your answer but its not working..,

    – Magento2 Devloper
    Jan 13 '17 at 4:38













  • you are trying to save new image in place of old image with your code?

    – Dhiren Vasoya
    Jan 13 '17 at 4:40











  • yes..i have save new image.

    – Magento2 Devloper
    Jan 13 '17 at 4:45











  • you need to used above code to show images, and apply above things there.

    – Dhiren Vasoya
    Jan 13 '17 at 4:46











  • I have used above code but not working..if you have code can you paste here?

    – Magento2 Devloper
    Jan 13 '17 at 5:16



















  • Thanx for your answer but its not working..,

    – Magento2 Devloper
    Jan 13 '17 at 4:38













  • you are trying to save new image in place of old image with your code?

    – Dhiren Vasoya
    Jan 13 '17 at 4:40











  • yes..i have save new image.

    – Magento2 Devloper
    Jan 13 '17 at 4:45











  • you need to used above code to show images, and apply above things there.

    – Dhiren Vasoya
    Jan 13 '17 at 4:46











  • I have used above code but not working..if you have code can you paste here?

    – Magento2 Devloper
    Jan 13 '17 at 5:16

















Thanx for your answer but its not working..,

– Magento2 Devloper
Jan 13 '17 at 4:38







Thanx for your answer but its not working..,

– Magento2 Devloper
Jan 13 '17 at 4:38















you are trying to save new image in place of old image with your code?

– Dhiren Vasoya
Jan 13 '17 at 4:40





you are trying to save new image in place of old image with your code?

– Dhiren Vasoya
Jan 13 '17 at 4:40













yes..i have save new image.

– Magento2 Devloper
Jan 13 '17 at 4:45





yes..i have save new image.

– Magento2 Devloper
Jan 13 '17 at 4:45













you need to used above code to show images, and apply above things there.

– Dhiren Vasoya
Jan 13 '17 at 4:46





you need to used above code to show images, and apply above things there.

– Dhiren Vasoya
Jan 13 '17 at 4:46













I have used above code but not working..if you have code can you paste here?

– Magento2 Devloper
Jan 13 '17 at 5:16





I have used above code but not working..if you have code can you paste here?

– Magento2 Devloper
Jan 13 '17 at 5:16


















draft saved

draft discarded




















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f154560%2fmagento2-how-to-remove-extra-white-background-from-uploaded-image-from-admin%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

An IMO inspired problem

Management

Has there ever been an instance of an active nuclear power plant within or near a war zone?