Jump to content

XML Add image to product problem


thralik

Recommended Posts

Dear Prestashopers,

 

I am developing xml module for my Prestashop and I am having problem with add image to product in my code. Product is succesfully created, image appers as created for product, but is blank and path dosnt exist (it even dont create image in folder /img/p/...)

 

I store images on hosting and then want this images give to product.

Code is: 

    //Store in the filesystem.
    $url = "$item->ID_PRODUCT".rand(1, 100000000000000).".jpg";
    $ch = curl_init($item->IMGURL);
    $fp = fopen($url, 'wb');
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);

    $id_product = $product->id;
    $image = new Image();
    $image->id_product = $id_product;
    $image->position = Image::getHighestPosition($id_product) + 1;
    $image->cover = true; // or false;
    $image->add();
    copyImg($id_product, $image->id, $url, 'products', false);

And copyImg() is:

function copyImg($id_entity, $id_image = null, $data, $entity = 'products')
{

    $tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
    $watermark_types = explode(',', Configuration::get('WATERMARK_TYPES'));

    switch ($entity) {
        default:
        case 'products':
            $imageObj = new Image($id_image);
            $path = $imageObj->getPathForCreation();
            break;
        case 'categories':
            $path = _PS_CAT_IMG_DIR_ . (int) ($id_entity);
            break;
    }
    if($data == ''){return false;}
    if (@file_put_contents($tmpfile,$data)) {//@copy($url_source_file, $tmpfile)
        imageResize($tmpfile, $path . '.jpg');
        $imagesTypes = ImageType::getImagesTypes($entity);
        foreach ($imagesTypes AS $k => $imageType)
            imageResize($tmpfile, $path . '-' . stripslashes($imageType['name']) . '.jpg', $imageType['width'], $imageType['height']);
        if (in_array($imageType['id_image_type'], $watermark_types))
            Module::hookExec('watermark', array('id_image' => $id_image, 'id_product' => $id_entity));
    }
    else {
        unlink($tmpfile);
        
    }
    unlink($tmpfile);
   
}

I guess it is going to be something silly, thank  you for help.

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...