Magento2: How to remove extra white background from uploaded image from admin
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?
image-upload magento-2.1.3
add a comment |
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?
image-upload magento-2.1.3
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
add a comment |
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?
image-upload magento-2.1.3
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?
image-upload magento-2.1.3
image-upload magento-2.1.3
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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);?>"/>
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
|
show 3 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);?>"/>
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
|
show 3 more comments
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);?>"/>
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
|
show 3 more comments
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);?>"/>
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);?>"/>
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
|
show 3 more comments
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
|
show 3 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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