Jump to content

Add Images to a product base64 format


Recommended Posts

Hello, I'm trying to add an image that i get via api in base 64 to a product import with php but I'm stuck, I use the generic image function like this:

function copyImg($id_entity, $id_image = null, $blob, $entity = 'products', $regenerate = true)
{
    $tempfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
    $watermark_types = explode(', ', Configuration::get('WATERMARK_TYPES'));

    $tmp_f = dirname( __FILE__ ).'/'.$string64[1];

    $f = fopen($tmp_f, 'wb');
    fwrite($f, base64_decode($string64[0]));
    fclose($f);

    switch($entity)
    {
        default:
        case 'products':
            $image_obj = new Image($id_image);
            $path = $image_obj->getPathForCreation();
        break;
        case 'categories':
            $path = _PS_CAT_IMG_DIR_.(int)$id_entity;
        break;
        case 'manufacturers':
            $path = _PS_MANU_IMG_DIR_.(int)$id_entity;
        break;
        case 'suppliers':
            $path = _PS_SUPP_IMG_DIR_.(int)$id_entity;
        break;
    }
    //$url = str_replace(' ', '%20', trim($url));
    // Evaluate the memory required to resize the image: if it's too much, you can't resize it.
    if (!ImageManager::checkImageMemoryLimit($tmp_f))
    {
        return false;
    }

   if (Tools::copy($tmp_f, $tempfile))
   {
        $ext = explode('.', $string64[1]);
        ImageManager::resize($tempfile, $path.'.'.$ext[1]);
        $image_types = ImageType::getImagesTypes($entity);

        if ($regenerate)
        {
            foreach($image_types as $image_type)
            {
                ImageManager::resize($tmp_f, $path.'-'.stripslashes($image_type['name']).'.'.$ext[1], $image_type['width'], $image_type['height']);
                if (in_array($image_type['id_image_type'], $watermark_types))
                {
                    Hook::exec('actionWatermark', array('id_image'=> $id_image, 'id_product'=> $id_entity));
                }
            }
        }
   }
    else
    {
        unlink($tmp_f);
        unlink($tempfile);
        return false;
    }
    unlink($tmp_f);
    unlink($tempfile);
    return true;

But this is not working.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...