如何在Prestashop 1.7中将图像添加到产品对象

如何在Prestashop 1.7中将图像添加到产品对象,prestashop,prestashop-1.7,Prestashop,Prestashop 1.7,我写了一个自定义模块来创建产品,导入产品。一切都很好,但我有一个与产品形象链接的问题。当我在源代码视图中选中指向产品图像的链接时,当我看到商店正面时,链接是: http://example.com/30-home_default/Array.jpg URL中存在一个数组,因为prestashop从以下位置获取: $product->link_rewrite = $link_rewrite; $link_rewrite有两个索引(两种语言)。如何解决这个问题 public functio

我写了一个自定义模块来创建产品,导入产品。一切都很好,但我有一个与产品形象链接的问题。当我在源代码视图中选中指向产品图像的链接时,当我看到商店正面时,链接是:

http://example.com/30-home_default/Array.jpg
URL中存在一个数组,因为prestashop从以下位置获取:

$product->link_rewrite = $link_rewrite;
$link_rewrite有两个索引(两种语言)。如何解决这个问题

public function createProductsObject($productID, $productName, $price, $weight, $category, $description, $link_rewrite, $singleStock, $getCategoryID, $getImages, $attrs) {
        $product = new Product;
        $product->name = $productName;
        $product->ean13 = '';
        $product->reference = '';
        $product->id_category_default = $getCategoryID;
        $product->category = $getCategoryID;
        $product->indexed = 1;
        $product->description = $description;
        $product->condition = 'new';
        $product->redirect_type = '404';
        $product->visibility = 'both';
        $product->id_supplier = 1;
        $product->link_rewrite = $link_rewrite;
        $product->quantity = $singleStock;
        $product->price = round($price - (18.69 / 100) * $price, 2);
        $product->active = 1;
        $product->psoft_hurtobergamo_id = $productID;
        $product->add();


        $product->addToCategories($getCategoryID);

        $shops = 1;
        $count = 0;


        foreach ($getImages->children() AS $image) {
            $url = $image->attributes()->url->__toString();

            $id_product = $product->id;
            $image = new Image();
            $image->id_product = $id_product;
            $image->position = Image::getHighestPosition($id_product) + 1;

            if ($count == 0) {
                $image->cover = true;
                $count = 1;
            } else {
                $image->cover = false;
            }

            if (($image->validateFields(false, true)) === true &&
                    ($image->validateFieldsLang(false, true)) === true && $image->add()) {
                if (Configuration::get('PSOFT_HURTO_BERGAMO_THUMB') == '0') {
                    $productThumb = false;
                } else {
                    $productThumb = true;
                }
                $image->associateTo($shops);
                if (!self::copyImg($id_product, $image->id, $url, 'products', $productThumb)) {
                    $image->delete();
                }
            }
        }



    protected static function copyImg($id_entity, $id_image = null, $url, $entity = 'products', $regenerate = false) {
        $tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
        $watermark_types = explode(',', Configuration::get('WATERMARK_TYPES'));

        switch ($entity) {
            default:
            case 'products':
                $image_obj = new Image($id_image);
                $path = $image_obj->getPathForCreation();
                break;
        }

        $url = urldecode(trim($url));
        $parced_url = parse_url($url);

        if (isset($parced_url['path'])) {
            $uri = ltrim($parced_url['path'], '/');
            $parts = explode('/', $uri);
            foreach ($parts as &$part) {
                $part = rawurlencode($part);
            }
            unset($part);
            $parced_url['path'] = '/' . implode('/', $parts);
        }

        if (isset($parced_url['query'])) {
            $query_parts = array();
            parse_str($parced_url['query'], $query_parts);
            $parced_url['query'] = http_build_query($query_parts);
        }

        if (!function_exists('http_build_url')) {
            require_once(_PS_TOOL_DIR_ . 'http_build_url/http_build_url.php');
        }

        $url = http_build_url('', $parced_url);

        $orig_tmpfile = $tmpfile;

        if (Tools::copy($url, $tmpfile)) {
// Evaluate the memory required to resize the image: if it's too much, you can't resize it.
            if (!ImageManager::checkImageMemoryLimit($tmpfile)) {
                @unlink($tmpfile);
                return false;
            }

            $tgt_width = $tgt_height = 0;
            $src_width = $src_height = 0;
            $error = 0;
            ImageManager::resize($tmpfile, $path . '.jpg', null, null, 'jpg', false, $error, $tgt_width, $tgt_height, 5, $src_width, $src_height);
            $images_types = ImageType::getImagesTypes($entity, true);

            if ($regenerate) {
                $previous_path = null;
                $path_infos = array();
                $path_infos[] = array($tgt_width, $tgt_height, $path . '.jpg');
                foreach ($images_types as $image_type) {
                    $tmpfile = self::get_best_path($image_type['width'], $image_type['height'], $path_infos);

                    if (ImageManager::resize($tmpfile, $path . '-' . stripslashes($image_type['name']) . '.jpg', $image_type['width'], $image_type['height'], 'jpg', false, $error, $tgt_width, $tgt_height, 5, $src_width, $src_height)) {
// the last image should not be added in the candidate list if it's bigger than the original image
                        if ($tgt_width <= $src_width && $tgt_height <= $src_height) {
                            $path_infos[] = array($tgt_width, $tgt_height, $path . '-' . stripslashes($image_type['name']) . '.jpg');
                        }
                        if ($entity == 'products') {
                            if (is_file(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int) $id_entity . '.jpg')) {
                                unlink(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int) $id_entity . '.jpg');
                            }
                            if (is_file(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int) $id_entity . '_' . (int) Context::getContext()->shop->id . '.jpg')) {
                                unlink(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int) $id_entity . '_' . (int) Context::getContext()->shop->id . '.jpg');
                            }
                        }
                    }
                    if (in_array($image_type['id_image_type'], $watermark_types)) {
                        Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity));
                    }
                }
            }
        } else {
            @unlink($orig_tmpfile);
            return false;
        }
        unlink($orig_tmpfile);
        return true;
    }

    protected static function get_best_path($tgt_width, $tgt_height, $path_infos) {
        $path_infos = array_reverse($path_infos);
        $path = '';
        foreach ($path_infos as $path_info) {
            list($width, $height, $path) = $path_info;
            if ($width >= $tgt_width && $height >= $tgt_height) {
                return $path;
            }
        }
        return $path;
    }
公共函数createProductsObject($productID、$productName、$price、$weight、$category、$description、$link\u rewrite、$singleStock、$getCategoryID、$getImages、$attrs){
$product=新产品;
$product->name=$productName;
$product->ean13='';
$product->reference='';
$product->id\u category\u default=$getCategoryID;
$product->category=$getCategoryID;
$product->index=1;
$product->description=$description;
$product->条件='新';
$product->redirect_type='404';
$product->VISITY='两者';
$product->id\u供应商=1;
$product->link\U rewrite=$link\U rewrite;
$product->QUOTE=$singleStock;
$product->price=整数($price-(18.69/100)*$price,2);
$product->active=1;
$product->psoft\U hurtobergamo\U id=$productID;
$product->add();
$product->addToCategories($getCategoryID);
$shops=1;
$count=0;
foreach($getImages->children()作为$image){
$url=$image->attributes()->url->;
$id\u product=$product->id;
$image=新图像();
$image->id\u product=$id\u product;
$image->position=image::getHighestPosition($id\u product)+1;
如果($count==0){
$image->cover=true;
$count=1;
}否则{
$image->cover=false;
}
如果(($image->validateFields(false,true))==true&&
($image->validateFieldsLang(false,true))==true&&&$image->add()){
if(配置::get('PSOFT\u HURTO\u BERGAMO\u THUMB')=='0'){
$productThumb=false;
}否则{
$productThumb=true;
}
$image->associateTo($shops);
如果(!self::copyImg($id\u product,$image->id,$url,'products',$productThumb)){
$image->delete();
}
}
}
受保护的静态函数copyImg($id\u entity,$id\u image=null,$url,$entity='products',$REGENATE=false){
$tmpfile=tempnam(_PS_TMP_IMG_DIR,'PS_import');
$watermark_types=explode(',',Configuration::get('watermark_types');
交换机($实体){
违约:
“产品”案例:
$image\u obj=新图像($id\u图像);
$path=$image_obj->getPathForCreation();
打破
}
$url=urldecode(trim($url));
$parced\u url=parse\u url($url);
如果(isset($parced_url['path'])){
$uri=ltrim($parced_url['path'],'/');
$parts=分解('/',$uri);
foreach($parts as&$part){
$part=rawurlencode($part);
}
未结算(部分);
$parced_url['path']='/'。内爆('/',$parts);
}
如果(isset($parced_url['query'])){
$query_parts=array();
parse_str($parced_url['query'],$query_parts);
$parced_url['query']=http_build_query($query_parts);
}
如果(!function_存在('http_build_url')){
需要一次(_PS_TOOL_DIR.'http_build_url/http_build_url.php');
}
$url=http\u build\u url(“”,$parced\u url);
$orig_tmpfile=$tmpfile;
if(工具::复制($url,$tmpfile)){
//评估调整图像大小所需的内存:如果内存太大,则无法调整图像大小。
如果(!ImageManager::checkImageMemoryLimit($tmpfile)){
@取消链接($tmpfile);
返回false;
}
$tgt_宽度=$tgt_高度=0;
$src_width=$src_height=0;
$error=0;
ImageManager::resize($tmpfile,$path.'.jpg',null,null,'jpg',false,$error,$tgt_-width,$tgt_-height,5,$src_-width,$src_-height);
$images\u types=ImageType::getImagesTypes($entity,true);
如果($重新生成){
$previous_path=null;
$path_infos=array();
$path_infos[]=数组($tgt_width,$tgt_height,$path.'.jpg');
foreach($images\u type作为$images\u type){
$tmpfile=self::get_best_path($image_type['width'],$image_type['height'],$path_infos);
if(ImageManager::resize($tmpfile,$path'.-'.stripslashes($image_-type['name'])。.jpg',$image_-type['width'],$image_-type['height'],'jpg',false,$error,$tgt_-width,$tgt_-height,5,$src_-width,$src_-height)){
//如果最后一张图像大于原始图像,则不应将其添加到候选列表中
如果($tgt_width id.'.jpg')){
取消链接(_PS_TMP_IMG_DIR.'product_mini_..(int)$id_entity.'...(int)Context::getContext()->shop->id..jpg');
}
}
}
if(在数组中($image\u type['id\u image\u type'],$watermark\u types)){
Hook::exec('actionWatermark',array('id\u image'=>$id\u image,'id\u product'=>$id\u entity));
}
}
}
}否则{
@取消链接($orig_tmpfile);
返回false;
}
取消链接($orig_tmpfile);
返回true;
}
受保护的静态函数获取最佳路径($tgt\U宽度、$tgt\U高度、$path\U信息){
$path\u infos=数组\u reverse($path\u infos);
$path='';
foreach($path_infos as)
<?php
//after you saved a product and you have the id (eg. product id: 1) for language 1
$p = new Product(1,false,1);
$productImages = $p->getImages(1);
<?php
//we need to create a new image ObjectModel
$image = new Image();
$image->id_product = 1; //associate
$image->add(false);
<?php

class Product extends ProductCore{

}