本文整理汇总了PHP中Magento\Catalog\Helper\Image类的典型用法代码示例。如果您正苦于以下问题:PHP Image类的具体用法?PHP Image怎么用?PHP Image使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Image类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getCartItems
/**
* @return array
*/
public function getCartItems()
{
if (!isset(self::$quoteItemsData)) {
self::$quoteItemsData = [];
$displayPriceWithTax = $this->rejoinerHelper->getTrackPriceWithTax();
if ($quote = $this->getQuote()) {
$categories = [];
/** @var \Magento\Quote\Model\Quote $quote */
/** @var \Magento\Quote\Model\Quote\Item $item */
foreach ($quote->getAllItems() as $item) {
$categories = array_merge($categories, $item->getProduct()->getCategoryIds());
}
/** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection */
$categoryCollection = $this->categoryCollectionFactory->create();
$categoriesArray = $categoryCollection->addAttributeToSelect('name')->addFieldToFilter('entity_id', ['in' => $categories])->load()->getItems();
$imageWidth = $this->rejoinerHelper->getImageWidth();
$imageHeight = $this->rejoinerHelper->getImageHeight();
foreach ($quote->getAllVisibleItems() as $item) {
$product = $item->getProduct();
$productCategories = $this->rejoinerHelper->getProductCategories($product, $categoriesArray);
$imageUrl = $this->imageHelper->init($product, 'category_page_grid')->resize($imageWidth, $imageHeight)->getUrl();
if ($displayPriceWithTax) {
$productPrice = $item->getPriceInclTax();
$rowTotal = $item->getRowTotalInclTax();
} else {
$productPrice = $item->getPrice();
$rowTotal = $item->getRowTotal();
}
$newItem = ['name' => $item->getName(), 'image_url' => $imageUrl, 'price' => (string) $this->rejoinerHelper->convertPriceToCents($productPrice), 'product_id' => (string) $item->getSku(), 'item_qty' => (string) $item->getQty(), 'qty_price' => (string) $this->rejoinerHelper->convertPriceToCents($rowTotal), 'product_url' => (string) $product->getProductUrl(), 'category' => $productCategories];
self::$quoteItemsData[] = $newItem;
}
}
}
return self::$quoteItemsData;
}
示例2: testGetOptions
/**
* @param array $expected
* @param array $data
* @dataProvider getOptionsDataProvider
*/
public function testGetOptions(array $expected, array $data)
{
if (count($data['allowed_products'])) {
$imageHelper1 = $this->getMockBuilder('Magento\\Catalog\\Helper\\Image')->disableOriginalConstructor()->getMock();
$imageHelper1->expects($this->any())->method('getUrl')->willReturn('http://example.com/base_img_url');
$imageHelper2 = $this->getMockBuilder('Magento\\Catalog\\Helper\\Image')->disableOriginalConstructor()->getMock();
$imageHelper2->expects($this->any())->method('getUrl')->willReturn('http://example.com/base_img_url_2');
$this->_imageHelperMock->expects($this->any())->method('init')->willReturnMap([[$data['current_product_mock'], 'product_page_image_large', [], $imageHelper1], [$data['allowed_products'][0], 'product_page_image_large', [], $imageHelper1], [$data['allowed_products'][1], 'product_page_image_large', [], $imageHelper2]]);
}
$this->assertEquals($expected, $this->_model->getOptions($data['current_product_mock'], $data['allowed_products']));
}
示例3: getGalleryImages
/**
* Retrieve collection of gallery images
*
* @param \Magento\Catalog\Api\Data\ProductInterface $product
* @return \Magento\Catalog\Model\Product\Image[]|null
*/
public function getGalleryImages(\Magento\Catalog\Api\Data\ProductInterface $product)
{
$images = $product->getMediaGalleryImages();
if ($images instanceof \Magento\Framework\Data\Collection) {
foreach ($images as $image) {
/** @var $image \Magento\Catalog\Model\Product\Image */
$image->setData('small_image_url', $this->imageHelper->init($product, 'product_page_image_small')->setImageFile($image->getFile())->getUrl());
$image->setData('medium_image_url', $this->imageHelper->init($product, 'product_page_image_medium')->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)->setImageFile($image->getFile())->getUrl());
$image->setData('large_image_url', $this->imageHelper->init($product, 'product_page_image_large')->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)->setImageFile($image->getFile())->getUrl());
}
}
return $images;
}
示例4: testGenerate
public function testGenerate()
{
$imageFile = 'image.jpg';
$imageItem = $this->objectManager->getObject('Magento\\Framework\\Object', ['data' => ['file' => $imageFile]]);
$this->mediaGalleryCollection->expects($this->once())->method('getIterator')->willReturn(new \ArrayIterator([$imageItem]));
$this->product->expects($this->any())->method('getMediaGalleryImages')->willReturn($this->mediaGalleryCollection);
$this->config->expects($this->once())->method('getVars')->with('Magento_Catalog')->willReturn($this->getTestData());
$this->viewConfig->expects($this->once())->method('getViewConfig')->with(['area' => Area::AREA_FRONTEND, 'themeModel' => 'Magento\\theme'])->willReturn($this->config);
$this->themeCollection->expects($this->once())->method('loadRegisteredThemes')->willReturn(['Magento\\theme']);
$this->imageHelper->expects($this->exactly(3))->method('init')->will($this->returnValueMap([[$this->product, 'image', $imageFile, $this->imageHelper], [$this->product, 'small_image', $imageFile, $this->imageHelper], [$this->product, 'thumbnail', $imageFile, $this->imageHelper]]));
$this->imageHelper->expects($this->exactly(3))->method('resize')->will($this->returnValueMap([[300, 300, $this->imageHelper], [200, 200, $this->imageHelper], [100, 100, $this->imageHelper]]));
$this->imageHelper->expects($this->exactly(3))->method('save')->will($this->returnSelf());
$this->model->generate($this->product);
}
示例5: getItems
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function getItems()
{
Profiler::start(__METHOD__);
$items = [];
/** @var \Magento\Catalog\Model\Product $product */
foreach ($this->collection as $product) {
$item = ['sku' => $product->getSku(), 'name' => $product->getName(), 'url' => $product->getProductUrl(), 'description' => null, 'image' => null, 'price' => null, 'rating' => null];
if ($this->config->isShowShortDescription()) {
$item['description'] = html_entity_decode(strip_tags($product->getDataUsingMethod('description')));
}
$image = false;
if ($product->getImage() && $product->getImage() != 'no_selection') {
$image = $product->getImage();
} elseif ($product->getSmallImage() && $product->getSmallImage() != 'no_selection') {
$image = $product->getSmallImage();
}
if ($this->config->isShowImage() && $image) {
$item['image'] = $this->imageHelper->init($product, false)->setImageFile($image)->resize(65 * 2, 80 * 2)->getUrl();
}
if ($this->config->isShowPrice()) {
$item['price'] = $this->catalogHelper->getTaxPrice($product, $product->getFinalPrice());
$item['price'] = $this->pricingHelper->currency($item['price'], false, false);
}
if ($this->config->isShowRating()) {
$item['rating'] = $this->reviewRenderer->getReviewsSummaryHtml($product, ReviewRendererInterface::SHORT_VIEW);
}
$items[] = $item;
}
Profiler::stop(__METHOD__);
return $items;
}
示例6: getOptions
/**
* Get Options for Configurable Product Options
*
* @param \Magento\Catalog\Model\Product $currentProduct
* @param array $allowedProducts
* @return array
*/
public function getOptions($currentProduct, $allowedProducts)
{
$options = [];
$baseImageUrl = (string) $this->imageHelper->init($currentProduct, 'image');
foreach ($allowedProducts as $product) {
$productId = $product->getId();
$image = (string) $this->imageHelper->init($product, 'image');
foreach ($this->getAllowAttributes($currentProduct) as $attribute) {
$productAttribute = $attribute->getProductAttribute();
$productAttributeId = $productAttribute->getId();
$attributeValue = $product->getData($productAttribute->getAttributeCode());
$options[$productAttributeId][$attributeValue][] = $productId;
$imageUrl = !$product->getImage() || $product->getImage() === 'no_selection' ? $baseImageUrl : (string) $image;
$options['images'][$productAttributeId][$attributeValue][$productId] = $imageUrl;
}
}
return $options;
}
示例7: getItems
/**
* Return array with data of items of order
*
* @param $order
*
* @return array
*/
protected function getItems($order)
{
$items = [];
foreach ($order->getAllVisibleItems() as $item) {
$product = $item->getProduct();
$image = $this->_helperImage->init($product, 'image');
$items[] = ["id" => $item->getSku(), "title" => $product->getName(), "description" => $product->getName(), "picture_url" => $image->getUrl(), "category_id" => $this->_scopeConfig->getValue('payment/mercadopago/category_id', \Magento\Store\Model\ScopeInterface::SCOPE_STORE), "quantity" => (int) number_format($item->getQtyOrdered(), 0, '.', ''), "unit_price" => (double) number_format($item->getPrice(), 2, '.', '')];
}
return $items;
}
示例8: createModel
/**
* {@inheritdoc}
*/
protected function createModel()
{
$this->currencyMock = $this->getMockBuilder(CurrencyInterface::class)->setMethods(['getCurrency', 'toCurrency'])->getMockForAbstractClass();
$this->currencyMock->expects($this->any())->method('getCurrency')->willReturn($this->currencyMock);
$this->imageHelperMock = $this->getMockBuilder(ImageHelper::class)->setMethods(['init', 'getUrl'])->disableOriginalConstructor()->getMock();
$this->imageHelperMock->expects($this->any())->method('init')->willReturn($this->imageHelperMock);
$this->attributeSetRepositoryMock = $this->getMockBuilder(AttributeSetRepositoryInterface::class)->setMethods(['get'])->getMockForAbstractClass();
$attributeSetMock = $this->getMockBuilder(AttributeSetInterface::class)->setMethods(['getAttributeSetName'])->getMockForAbstractClass();
$this->attributeSetRepositoryMock->expects($this->any())->method('get')->willReturn($attributeSetMock);
return $this->objectManager->getObject(Grouped::class, ['locator' => $this->locatorMock, 'productLinkRepository' => $this->linkRepositoryMock, 'productRepository' => $this->productRepositoryMock, 'localeCurrency' => $this->currencyMock, 'imageHelper' => $this->imageHelperMock, 'attributeSetRepository' => $this->attributeSetRepositoryMock]);
}
示例9: generate
/**
* Resize product images and save results to image cache
*
* @param Product $product
* @return $this
*/
public function generate(Product $product)
{
$galleryImages = $product->getMediaGalleryImages();
if ($galleryImages) {
foreach ($galleryImages as $image) {
foreach ($this->getData() as $params) {
$this->imageHelper->init($product, $params['type'], $image->getFile())->resize($params['width'], $params['height'])->save();
}
}
}
return $this;
}
示例10: execute
/**
* Trigger pusher event with latest order information
*
* @param \Magento\Framework\Event\Observer $observer
* @return \Jahvi\NewOrderNotification\Observer\DisplayNotification
*/
public function execute(Observer $observer)
{
if ($this->globalConfig->getValue('checkout/newordernotification/enabled')) {
$appId = $this->globalConfig->getValue('checkout/newordernotification/app_id');
$appKey = $this->globalConfig->getValue('checkout/newordernotification/app_key');
$appSecret = $this->globalConfig->getValue('checkout/newordernotification/app_secret');
$pusher = new \Pusher($appKey, $appSecret, $appId, ['encrypted' => true]);
// Get latest order
$orderId = $observer->getEvent()->getOrderIds()[0];
$order = $this->orderFactory->create()->load($orderId);
// Get last product in order data
$product = $order->getAllVisibleItems()[0]->getProduct();
$shippingCity = $order->getShippingAddress()->getCity();
$productImage = $this->imageHelper->init($product, 'product_thumbnail_image');
// Get shipping city and country
$shippingCountryCode = $order->getShippingAddress()->getCountryId();
$shippingCountry = $this->countryFactory->create()->loadByCode($shippingCountryCode);
// Trigger pusher event with collected data
$pusher->trigger('non_channel', 'new_order', ['product_name' => $product->getName(), 'product_image' => $productImage->getUrl(), 'product_url' => $product->getProductUrl(), 'shipping_city' => $shippingCity, 'shipping_country' => $shippingCountry->getName()]);
}
return $this;
}
示例11: testGetJsonSwatchConfigWithoutVisualImageType
public function testGetJsonSwatchConfigWithoutVisualImageType()
{
$this->configurable->setProduct($this->product);
$this->swatchHelper->expects($this->once())->method('getSwatchAttributesAsArray')->with($this->product)->willReturn([1 => ['options' => [1 => 'testA', 3 => 'testB'], 'use_product_image_for_swatch' => true, 'attribute_code' => 'code']]);
$this->swatchHelper->expects($this->once())->method('getSwatchesByOptionsId')->with([1, 3])->willReturn([3 => ['type' => Swatch::SWATCH_TYPE_VISUAL_IMAGE, 'value' => 'hello']]);
$this->swatchHelper->expects($this->once())->method('loadFirstVariationWithSwatchImage')->with($this->product, ['code' => 3])->willReturn($this->product);
$this->swatchMediaHelper->expects($this->exactly(2))->method('getSwatchAttributeImage')->withConsecutive(['swatch_image', 'hello'], ['swatch_thumb', 'hello'])->willReturn('/path');
$this->product->expects($this->exactly(4))->method('getData')->withConsecutive(['swatch_image'], ['image'], ['swatch_image'], ['image'])->will($this->onConsecutiveCalls(null, null, null, null));
$this->imageHelper->expects($this->never())->method('init');
$this->imageHelper->expects($this->never())->method('resize');
$this->jsonEncoder->expects($this->once())->method('encode');
$this->configurable->getJsonSwatchConfig();
}
示例12: getListValues
/**
* @param $products
* @return array
*/
public function getListValues($ids)
{
$values = [];
$searchCriteria = $this->_criteriaBuilder->addFilter('entity_id', $ids, 'in')->create();
$products = $this->_productRepository->getList($searchCriteria);
foreach ($products->getItems() as $product) {
$image = $this->_imageHelper->init($product, 'product_page_image_small')->getUrl();
$price = $product->getFinalPrice();
if ($price == 0 && $product->getTypeId() == 'grouped') {
$children = $product->getTypeInstance()->getAssociatedProducts($product);
foreach ($children as $child) {
if ($child->getPrice() < $price || $price == 0) {
$price = $child->getPrice();
}
}
}
$value = array();
$value['escape_name'] = $this->escapeHtml($product->getName());
$value['name'] = $product->getName();
$value['url'] = $product->getProductUrl();
$value['price'] = $this->_priceCurrency->format($price, false);
$value['image'] = $image;
$values[] = $value;
}
return $values;
}
示例13: testGetOptions
/**
* @param array $expected
* @param array $data
* @dataProvider getOptionsDataProvider
*/
public function testGetOptions(array $expected, array $data)
{
$this->_imageHelperMock->expects($this->at(0))->method('init')->will($this->returnValue('http://example.com/base_img_url'));
for ($i = 1; $i <= count($data['allowed_products']); $i++) {
$this->_imageHelperMock->expects($this->at($i))->method('init')->will($this->returnValue('http://example.com/base_img_url_' . $i));
}
$this->assertEquals($expected, $this->_model->getOptions($data['current_product_mock'], $data['allowed_products']));
}
示例14: testGetRssData
public function testGetRssData()
{
$category = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Category')->setMethods(['__sleep', '__wakeup', 'load', 'getId', 'getUrl', 'getName'])->disableOriginalConstructor()->getMock();
$category->expects($this->once())->method('getName')->will($this->returnValue('Category Name'));
$category->expects($this->once())->method('getUrl')->will($this->returnValue('http://magento.com/category-name.html'));
$this->categoryRepository->expects($this->once())->method('get')->will($this->returnValue($category));
$product = $this->getMockBuilder('\\Magento\\catalog\\Model\\Product')->setMethods(['__sleep', '__wakeup', 'getName', 'getAllowedInRss', 'getProductUrl', 'getDescription', 'getAllowedPriceInRss'])->disableOriginalConstructor()->getMock();
$product->expects($this->once())->method('getName')->will($this->returnValue('Product Name'));
$product->expects($this->once())->method('getAllowedInRss')->will($this->returnValue(true));
$product->expects($this->exactly(2))->method('getProductUrl')->will($this->returnValue('http://magento.com/product.html'));
$product->expects($this->once())->method('getDescription')->will($this->returnValue('Product Description'));
$product->expects($this->once())->method('getAllowedPriceInRss')->will($this->returnValue(true));
$this->rssModel->expects($this->once())->method('getProductCollection')->will($this->returnValue([$product]));
$this->imageHelper->expects($this->once())->method('init')->with($product, 'thumbnail')->will($this->returnSelf());
$this->imageHelper->expects($this->once())->method('resize')->with(75, 75)->will($this->returnValue('image_link'));
$data = $this->block->getRssData();
$this->assertEquals($this->rssFeed['link'], $data['link']);
$this->assertEquals($this->rssFeed['title'], $data['title']);
$this->assertEquals($this->rssFeed['description'], $data['description']);
$this->assertEquals($this->rssFeed['entries'][0]['title'], $data['entries'][0]['title']);
$this->assertEquals($this->rssFeed['entries'][0]['link'], $data['entries'][0]['link']);
$this->assertContains('<a href="http://magento.com/product.html">', $data['entries'][0]['description']);
$this->assertContains('<img src="image_link" border="0" align="left" height="75" width="75">', $data['entries'][0]['description']);
$this->assertContains('<td style="text-decoration:none;">Product Description </td>', $data['entries'][0]['description']);
}
示例15: getRssData
/**
* Get RSS feed items
*
* @return array
*/
public function getRssData()
{
$wishlist = $this->getWishlist();
if ($wishlist->getId()) {
$data = $this->getHeader();
/** @var $wishlistItem \Magento\Wishlist\Model\Item */
foreach ($wishlist->getItemCollection() as $wishlistItem) {
/* @var $product \Magento\Catalog\Model\Product */
$product = $wishlistItem->getProduct();
$productUrl = $this->wishlistBlock->getProductUrl($product, ['_rss' => true]);
$product->setAllowedInRss(true);
$product->setAllowedPriceInRss(true);
$product->setProductUrl($productUrl);
$args = ['product' => $product];
$this->eventManager->dispatch('rss_wishlist_xml_callback', $args);
if (!$product->getAllowedInRss()) {
continue;
}
$description = '<table><tr><td><a href="' . $productUrl . '"><img src="' . $this->imageHelper->init($product, 'rss_thumbnail')->getUrl() . '" border="0" align="left" height="75" width="75"></a></td>' . '<td style="text-decoration:none;">' . $this->outputHelper->productAttribute($product, $product->getShortDescription(), 'short_description') . '<p>';
if ($product->getAllowedPriceInRss()) {
$description .= $this->getProductPriceHtml($product);
}
$description .= '</p>';
if (trim($product->getDescription()) != '') {
$description .= '<p>' . __('Comment:') . ' ' . $this->outputHelper->productAttribute($product, $product->getDescription(), 'description') . '<p>';
}
$description .= '</td></tr></table>';
$data['entries'][] = ['title' => $product->getName(), 'link' => $productUrl, 'description' => $description];
}
} else {
$data = ['title' => __('We cannot retrieve the Wish List.'), 'description' => __('We cannot retrieve the Wish List.'), 'link' => $this->urlBuilder->getUrl(), 'charset' => 'UTF-8'];
}
return $data;
}