在以编程方式添加prestashop时,图像不会调整大小

时间:2015-02-11 13:04:40

标签: prestashop

我很开心以编程方式添加图片。但图像没有调整大小。换句话说,不会生成调整大小的图像。附上我使用的代码:

$image = new Image();
$image_url = 'http://i.imgur.com/0zSw2gl.jpg';
            $id_image = Product::getCover($id_product);
            $shops = Shop::getShops(true, null, true);
            $image->id_product = $id_product;
            $image->position = Image::getHighestPosition($id_product) + 1;
            $image->cover = true; // or false;
            if (($image->validateFields(false, true)) === true &&
                ($image->validateFieldsLang(false, true)) === true && $image->add())
            {
                $image->associateTo($shops);
                if (!AdminImportControllerExtended::copyImgCustom($id_product, $image->id, $image_url, 'products', false))
                {
                    $image->delete();
                }
            }

1 个答案:

答案 0 :(得分:0)

图像应以编程方式调整大小

我使用此代码

$image = new Image();
$image->id_product = (int)$product->id;
$image->position = Image::getHighestPosition($product->id) + 1;
$image->cover = 1;
if (!$image->add())
    exit(Tools::jsonEncode(array('error' => Tools::displayError('Error while creating additional image'))));
else
{
    $new_path = $image->getPathForCreation();

    ImageManager::resize($image_path, $new_path.'.'.$image->image_format);
    $images_types = ImageType::getImagesTypes('products');
    foreach ($images_types as $image_type)
    {
        if (!ImageManager::resize($image_path, $new_path.'-'.Tools::stripslashes($image_type['name']).'.'.
            $image->image_format, $image_type['width'], $image_type['height'], $image->image_format))
            exit(Tools::jsonEncode(array('error' => Tools::displayError('An error occurred while copying image:').
                ' '.Tools::stripslashes($image_type['name']))));
    }
    $image->update();
}