【问题标题】:How to resize image and create thumbnail in silvestripe 4.4如何在 silvestripe 4.4 中调整图像大小并创建缩略图
【发布时间】:2020-02-29 04:47:20
【问题描述】:

谁能告诉我我做错了什么?

我想用 silvestripe 4.4 创建画廊

我创建了模型和模板,我能够在整个循环中获取图像,但无法调整图像大小来为图像创建缩略图

<?php

namespace SilverStripe\Lessons;

use SilverStripe\Forms\DateField;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\TextField;
use SilverStripe\Assets\Image;
use SilverStripe\Assets\File;
use SilverStripe\AssetAdmin\Forms\UploadField;
use Page;

class GalleryPage extends Page
{
    private static $table_name = 'GalleryPage';

    private static $can_be_root = false;

    private static $db = [
        'Date' => 'Date',
        'Author' => 'Varchar',
    ];

    private static $has_one = [
        'PhotoCover' => Image::class,
    ];

    private static $many_many = [
        'Gallery' => Image::class,
    ];

    private static $owns = [
        'PhotoCover',
        'Gallery',
    ];

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Main', DateField::create('Date','Date of article'), 'Content');
        $fields->addFieldToTab(
            'Root.Main',
            $PhotoCover = UploadField::create('PhotoCover'), 'Author');
        $fields->addFieldToTab('Root.Main', TextField::create('Author','Author of article'),'Content');


        $PhotoCover->setFolderName('PhotoCovers');


        $fields->addFieldToTab(
            'Root.Gallery',
            $Gallery = UploadField::create('Gallery')->setDescription('Selektuj Photos')
        );



        $Gallery->setFolderName('Gallery');

        return $fields;
    }

}

这是我的模板 GalleryPage.ss

<% if $Gallery %>
    <% loop $Gallery %>
     <a class="thumb" href="$URL" data-lightgallery="group-item">
        <img  src="$URL" alt="" width="371" height="276"/>
    </a>
    <% end_loop %>
<% end_if %>

这是我可以通过循环获取图像的唯一方法 我尝试了以下选项:

<img class="my-custom-class" src="$Gallery.ScaleWidth(750).URL" alt="" width="$Gallery.ScaleWidth(750).Width" height="$Gallery.ScaleWidth(750).Height" />

or

<% with $Gallery.ScaleWidth(750) %>
    <img class="my-custom-class" src="$URL" alt="" width="$Width" height="$Height" />
<% end_with %>

直到现在都没有成功尝试 olny...

我在这里做错了什么?

【问题讨论】:

    标签: php image templates url resize


    【解决方案1】:

    根据 SS documentation

    $Me 输出范围内的当前对象。这将调用对象的 forTemplate。

    在您的情况下,它是 '$Me.ScaleWidth(750).URL'

    <% if $Gallery %>
        <% loop $Gallery %>
         <a class="thumb" href="{$Me.ScaleWidth(750).URL}" data-lightgallery="group-item">
            <img  src="$URL" alt="" width="371" height="276"/>
        </a>
        <% end_loop %>
    <% end_if %>
    

    【讨论】:

      猜你喜欢
      • 2013-06-24
      • 2012-07-15
      • 2013-06-22
      • 2013-07-21
      • 2012-01-06
      • 1970-01-01
      • 2011-09-03
      • 2023-03-28
      • 1970-01-01
      相关资源
      最近更新 更多