0

I am trying to save base64 strings images from my custom module.

I have checked product image upload function:

            $fieldName = 'image_base64_field';
            $path = Mage::getBaseDir('media');
            $uploader = new Varien_File_Uploader($fieldName);
            $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
            //$uploader->addValidateCallback('catalog_product_image', Mage::helper('catalog/image'), 'validateUploadFile');
            $uploader->setAllowRenameFiles(true);
            $uploader->setFilesDispersion(true);
            $uploader->addValidateCallback(
                Mage_Core_Model_File_Validator_Image::NAME,
                Mage::getModel('core/file_validator_image'),
                'validate'
            );

            //base64 string
            $imgBase64 = '/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABkAfQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0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wrjh468Y6nCzaJ8Or8A52SandR22PQlDyfpn8aueHfDni5vEMPiHxP4jVpBE0Y0mxjK20Yb1JPzEepGcgc4oA0Ph4mr2/gfTrLXLQ21/ZIbVlJBDrGdqMMdioFdRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAVx3xB8U3GhadbaZpC+b4g1eT7Np8ePusfvSN6KoOfy7ZrpdVtLi/wBJurS1vpbGeaMolzEoLRE/xAH/AD9Otc34Z+H1roGrHWbzVtS1nVzB5H2q/l37FzkhF/hB+p7+poA0vB/he18IeG7fSrZjIy/vLiZus0p+85+p/QCt6iigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigCpfaVp2qKi6hYWt2qHKC4hWQKfbIOKwLv4a+Cb1SsvhfS1B/5424iP5piiigDMb4NeCAxe10yezkP8dtezIenH8WP0qNfg74eUEHUNdbJJydSf8ApRRQAn/CnPDvmF/t+u8gDH9pP/8Aro/4U54d8wv9v13kAY/tJ/8A9dFFADk+DXhMzh7walfxjGILu+kdAR3wCP1OK6zSfDeh6CMaTpFlZErtLQQKjMPcgZP40UUAalFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAf/Z';
            $image = $path . DS . 'name.png';
            //$result = $uploader->save($image);

            ///////////////////////////
            //I don't know how to save above $imgBase64

Above $imgBase64 is coming from post, based on user's input/draw.

How should I achieve this?

UPDATE

I know I can do this from php, but I believe for better security, I should do it Magento way.

$data = 'data:image/png;base64,'. $imgBase64;

$source = fopen($data, 'w');

file_put_contents('image5.png', $source);

fclose($source);

1 Answer 1

0

Well I am in shock you ask this type of question,

but let me draw you to some path,

create a file in media directory with fopen

$myfile = fopen("$filepath/testfile.png", "w");

and put your content with file_put_contents ()

file_put_contents ( string $filename , mixed $data );

Update

I don't think so there is some class in magento1 like m2 because magento itself uses file_put_content many places directly

like below app/code/core/Mage/Adminhtml/Model/Extension.php file

3
  • Sorry, didn't mention, I wanted to do magento way for better security. May 29, 2018 at 4:36
  • @AdarshKhatri need to check in m1 but for m2 you can do this via this dependency Magento\Framework\Filesystem\DriverInterface , I will check and let you know May 29, 2018 at 4:41
  • please update me if you find it magento way in magento 1. May 29, 2018 at 4:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.