You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TYPO3 8中TypoScript配置裁剪图片及降级方案

TYPO3 TypoScript: Fallback to Fixed Dimensions When No Crop Exists

Got it, let's tackle this common TYPO3 upgrade scenario—you need to honor existing cropped images while falling back to fixed 600x400px dimensions for images that don't have crop data yet. The key here is using conditional logic in TypoScript to detect whether crop data exists, then applying the appropriate configuration.

Cleanest Solution: TypoScript Conditional Blocks

This approach uses TYPO3's built-in conditional syntax to check for crop data, then switches between crop mode and fixed dimensions:

10 = FILES
10 {
  required = 1
  references {
    table = tt_content
    fieldName = image
  }
  renderObj = IMAGE
  renderObj {
    wrap = <div class="teaser-image">|</div>
    file.import.data = file:current:originalUid // file:current:uid
    
    // Check if crop data exists for the current file
    [file:current:crop]
      // Use the saved crop configuration if available
      file.crop.data = file:current:crop
    [else]
      // Fallback to fixed cropped dimensions when no crop exists
      file.width = 600c
      file.height = 400c
    [end]
  }
}

How This Works:

  • The [file:current:crop] condition checks if the current image has valid crop data stored.
  • If true, we apply the saved crop settings (just like your original crop-only setup).
  • If false (no crop data), we enforce the 600x400px cropped size using the c modifier—this ensures the image is cropped to fit the exact dimensions without stretching, maintaining aspect ratio.

Alternative: Inline Conditional with stdWrap

If you prefer a more compact, inline approach (useful if you need to keep all config in one block), you can use stdWrap.ifEmpty to trigger fallback dimensions:

10 = FILES
10 {
  required = 1
  references {
    table = tt_content
    fieldName = image
  }
  renderObj = IMAGE
  renderObj {
    wrap = <div class="teaser-image">|</div>
    file.import.data = file:current:originalUid // file:current:uid
    
    // Attempt to use crop data first
    file.crop.data = file:current:crop
    // If no crop exists, flag this in a temporary register
    file.crop.stdWrap.ifEmpty.cObject = TEXT
    file.crop.stdWrap.ifEmpty.cObject.stdWrap.postUserFunc = function($c, $conf) {
      $GLOBALS['TSFE']->register['noCropFound'] = true;
      return '';
    }
    
    // Apply fixed dimensions ONLY if no crop was found
    file.width = 600c
    file.width.stdWrap.if.isTrue.data = register:noCropFound
    file.height = 400c
    file.height.stdWrap.if.isTrue.data = register:noCropFound
  }
}

Key Notes:

  • The c modifier is important here—it tells TYPO3 to crop the image to the specified dimensions, rather than just scaling it (which would use m or leave off the modifier). This matches your original behavior.
  • Both solutions maintain backward compatibility: existing cropped images will render as they did before, while new/uncropped images get the fixed size treatment.

内容的提问来源于stack exchange,提问作者Urs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:20:33