Showing posts with label Image. Show all posts
Showing posts with label Image. Show all posts

Tuesday 2 October 2018

Product image Still zoom if I remove mouse cursor from image in magento 2

You need to replace the code of lib/web/magnifier/magnifier.js in your theme as below.

Find

$(document).on('mousemove', onMousemove);
_init($box, gOptions);

Replace

$box.on('mousemove', onMousemove);
       
/*Code By Jaydip*/
$box.on('mouseleave', mouseleave);
    function mouseleave(e) {
         onThumbLeave();
         isOverThumb = false;
         $magnifierPreview.addClass(MagnifyCls.magnifyHidden);
    }
/*Code By Jaydip*/
       
 _init($box, customUserOptions);

Saturday 23 June 2018

How to Import / Insert / Add product image from URL in Magento 2

use Magento\Framework\App\Action\Context;

class Createproduct extends \Magento\Framework\App\Action\Action {

    protected $directoryList;
    protected $file;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \Magento\Framework\App\Filesystem\DirectoryList $directoryList,
        \Magento\Framework\Filesystem\Io\File $file) {

        $this->_resultpageFactory = $pageFactory;
        $this->directoryList = $directoryList;
        $this->file = $file;
        return parent::__construct($context);
    }


    protected function getMediaDirTmpDir()
    {
        return $this->directoryList->getPath($this->directoryList::MEDIA) . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR;
    }

    public function setImage($product, $imageUrl, $visible = false, $imageType = [])
    {
        $tmpDir = $this->getMediaDirTmpDir();

        $this->file->checkAndCreateFolder($tmpDir);

        $newFileName = $tmpDir . baseName($imageUrl);


        $result = $this->file->read($imageUrl, $newFileName);
        if ($result) {
            $product->addImageToMediaGallery($newFileName, $imageType, true, $visible);
        }
        return $result;
    }

    public function execute() {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->create('\Magento\Catalog\Model\Product')->load(1);
        $imagePath = "https://domain.com/image.jpg"; // path of the image
            $this->setImage($product, $imagePath, false, $imageType = ['image', 'small_image', 'thumbnail']);
            $product->save();
    }
}

How to check image is valid or not in php from URL in PHP

$imagePath = "https://jaydip.kansagra.com/image.jpg";
if (@getimagesize($imagePath)) {
   echo 'Image exists';
} else {
      echo 'Image not exists';
}

Tuesday 23 January 2018

How to set background color into transparent image Imagick PHP

ini_set('display_errors',1);
header('Content-type: image/png');
$strInputFile = 'front.png'; // transparent image
$im = new Imagick($strInputFile);
$im->setImageBackgroundColor('green');
$im = $im->flattenImages();
echo $im;exit;

Wednesday 27 December 2017

How to upload image / file in magento


<input type="file" name="image">

Select path of media directory

$path = Mage::getBaseDir() . DS . 'media' . DS . 'customerimages' . DS;
$imagename = $this->uploadImageFile($_FILES, $path);

Create functions

function uploadImageFile($filedata, $path){
   
    if(isset($filedata['image']) && $filedata['image']['error'] == 0){
        try{
           
            $uploader = new Varien_File_Uploader('image');
            $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png'));
            $uploader->setAllowRenameFiles(true);
           
            if(!is_dir($path)){
                mkdir($path, 0755, true);
            }
            $ext = pathinfo($filedata['image']['name'], PATHINFO_EXTENSION);
            $uploader->save($path, date('Ymd') . "_" . date('his') . '.' . $ext);
            $uploadFile = $uploader->getUploadedFileName();
            if($uploadFile){
                return $uploadFile;
            } else {
                return 'File not uploaded!';
            }
        } catch (Exception $e) {
            return $e->getMessage();
        }
    } else {
        return 'Please select image file!';
    }
}

Tuesday 13 September 2016

Show thumbnail image / file in custom module admin form Magento




Just create one file and extended it with magento core abstract class. See example

Just add code into Your Form.php

class Kanasagra_Jaydip_Block_Adminhtml_{{Entity}}_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form {
    protected function _prepareForm() {
       
        $form = new Varien_Data_Form(array(
            'id' => 'edit_form',
            'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
            'method' => 'post',
            'enctype' => 'multipart/form-data'
                )
        );
        $fieldset = $form->addFieldset('{{Entity}}_form', array('legend' => Mage::helper('{{Entity}}')->__('Product information')));
        $fieldset->addType('file', Mage::getConfig()                                                                                              ->getBlockClassName('jaydip/Adminhtml_{{Entity}}_Helper_File'));        $fieldset->addField('svgimage', 'file', array(            'label' => Mage::helper('jaydip')->__('Upload SVG'),            'required' => false,            'name' => 'svgimage',        ));     ...
     ...
     ...
    }
}

Create New file in Kanasagra/Jaydip/Block/Adminhtml/{{Entity}}/Helper/File.php

<?php
class Kanasagra_Jaydip_Block_Adminhtml_{{Entity}}_Helper_File extends Varien_Data_Form_Element_Abstract{
   
    public function __construct($data){
        parent::__construct($data);
        $this->setType('file');
    }
    public function getElementHtml(){
        $html = '';
        $this->addClass('input-file');
        $html.= parent::getElementHtml();
        if ($this->getValue()) {
            $url = $this->_getUrl();
            if( !preg_match("/^http\:\/\/|https\:\/\//", $url) ) {
                $url = Mage::getBaseUrl('media').'photo_image/images/' . $url; //replace this with the path to the file if you upload it somewhere else
            }
            $html .= '<br /><img width="100px" src="'.$url.'">';
        }
        return $html;
    }
    protected function _getUrl(){
        return $this->getValue();
    }
}

Saturday 26 March 2016

Remove specific color/white/Black from any type of image

//header('Content-type: image/png');
$strInputFile = '20160312105210_girls.jpg';
$target = 'car_transparent.png';
$im = new Imagick($strInputFile);
//$im->paintTransparentImage($im->getImageBackgroundColor(), 0, 10000);
$im->paintTransparentImage('#000', 0, 10000); //Which color you need to remove #000
$im->setImageFormat('png');
//$im->writeImage($target);
$im->destroy();
//echo $im;