Skip to content

Instantly share code, notes, and snippets.

@RobsonAutomator
Last active March 11, 2020 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobsonAutomator/ce64075fb0d20a34ad7fb20f722e5bf0 to your computer and use it in GitHub Desktop.
Save RobsonAutomator/ce64075fb0d20a34ad7fb20f722e5bf0 to your computer and use it in GitHub Desktop.
Update value in the Size field in Sitecore media items.
#
# Fix value '123 x 123' in Size field to the proper image size (bytes).
# Author: Robert Senktas
#
$sitecorePath = "master:/sitecore/media library"
$processedItems = Get-ChildItem -Path $sitecorePath -Recurse | ForEach-Object {
if ( $_.Size -match "x" ) {
$mediaItem = New-Object "Sitecore.Data.Items.MediaItem" $_
[Sitecore.Resources.Media.Media] $media = [Sitecore.Resources.Media.MediaManager]::GetMedia($mediaItem);
$stream = $media.GetStream()
$reportItem = [PSCustomObject]@{
ID = $_.ID
Path = $_.Paths.Path
Size = $_.Size
Length = $stream.Length
}
$_.Editing.BeginEdit()
$_["Size"] = $stream.Length
$_.Editing.EndEdit()
$_ | Publish-Item -PublishMode SingleItem -Verbose
return $reportItem
}
}
# Generate a report
Import-Function -Name ConvertTo-Xlsx
[byte[]]$outobject = $processedItems |
Select-Object -Property ID, Path, Size, Length |
ConvertTo-Xlsx
Out-Download -Name "report-change-image-size-.xlsx" -InputObject $outobject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment