Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions application/cmdbabstract.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -4313,24 +4313,15 @@ protected function PrepareValueFromPostedForm($sFormPrefix, $sAttCode, $sClass =

case 'Image':
$value = null;
$aDimensions = null;
$oImage = utils::ReadPostedDocument("attr_{$sFormPrefix}{$sAttCode}", 'fcontents');
if (!is_null($oImage->GetData()))
{
$aSize = utils::GetImageSize($oImage->GetData());
if (is_array($aSize) && $aSize[0] > 0 && $aSize[1] > 0)
{
$oImage = utils::ResizeImageToFit(
$oImage,
$aSize[0],
$aSize[1],
$oAttDef->Get('storage_max_width'),
$oAttDef->Get('storage_max_height')
);
}
else
{
IssueLog::Warning($sClass . ':' . $this->GetKey() . '/' . $sAttCode . ': Image could not be resized. Mimetype: ' . $oImage->GetMimeType() . ', filename: ' . $oImage->GetFileName());
}
$oImage = $oImage->ResizeImageToFit(
$oAttDef->Get('storage_max_width'),
$oAttDef->Get('storage_max_height'),
$aDimensions
);
if (is_null($aDimensions)) {
IssueLog::Warning($sClass . ':' . $this->GetKey() . '/' . $sAttCode . ': Image could not be resized. Mimetype: ' . $oImage->GetMimeType() . ', filename: ' . $oImage->GetFileName());
}
$aOtherData = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null, 'raw_data');
if (is_array($aOtherData))
Expand Down
91 changes: 0 additions & 91 deletions application/utils.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -2315,97 +2315,6 @@ public static function GetImageSize($sImageData)
return @getimagesizefromstring($sImageData);
}

/**
* Resize an image attachment so that it fits in the given dimensions
* @param ormDocument $oImage The original image stored as an ormDocument
* @param int $iWidth Image's original width
* @param int $iHeight Image's original height
* @param int $iMaxImageWidth Maximum width for the resized image
* @param int $iMaxImageHeight Maximum height for the resized image
* @return ormDocument The resampled image
*/
public static function ResizeImageToFit(ormDocument $oImage, $iWidth, $iHeight, $iMaxImageWidth, $iMaxImageHeight)
{
// If image size smaller than maximums, we do nothing
if (($iWidth <= $iMaxImageWidth) && ($iHeight <= $iMaxImageHeight))
{
return $oImage;
}


// If gd extension is not loaded, we put a warning in the log and return the image as is
if (extension_loaded('gd') === false)
{
IssueLog::Warning('Image could not be resized as the "gd" extension does not seem to be loaded. It will remain as ' . $iWidth . 'x' . $iHeight . ' instead of ' . $iMaxImageWidth . 'x' . $iMaxImageHeight);
return $oImage;
}


switch($oImage->GetMimeType())
{
case 'image/gif':
case 'image/jpeg':
case 'image/png':
$img = @imagecreatefromstring($oImage->GetData());
break;

default:
// Unsupported image type, return the image as-is
//throw new Exception("Unsupported image type: '".$oImage->GetMimeType()."'. Cannot resize the image, original image will be used.");
return $oImage;
}
if ($img === false)
{
//throw new Exception("Warning: corrupted image: '".$oImage->GetFileName()." / ".$oImage->GetMimeType()."'. Cannot resize the image, original image will be used.");
return $oImage;
}
else
{
// Let's scale the image, preserving the transparency for GIFs and PNGs

$fScale = min($iMaxImageWidth / $iWidth, $iMaxImageHeight / $iHeight);

$iNewWidth = $iWidth * $fScale;
$iNewHeight = $iHeight * $fScale;

$new = imagecreatetruecolor($iNewWidth, $iNewHeight);

// Preserve transparency
if(($oImage->GetMimeType() == "image/gif") || ($oImage->GetMimeType() == "image/png"))
{
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
imagealphablending($new, false);
imagesavealpha($new, true);
}

imagecopyresampled($new, $img, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iWidth, $iHeight);

ob_start();
switch ($oImage->GetMimeType())
{
case 'image/gif':
imagegif($new); // send image to output buffer
break;

case 'image/jpeg':
imagejpeg($new, null, 80); // null = send image to output buffer, 80 = good quality
break;

case 'image/png':
imagepng($new, null, 5); // null = send image to output buffer, 5 = medium compression
break;
}
$oResampledImage = new ormDocument(ob_get_contents(), $oImage->GetMimeType(), $oImage->GetFileName());
@ob_end_clean();

imagedestroy($img);
imagedestroy($new);

return $oResampledImage;
}

}

/**
* Create a 128 bit UUID in the format: {########-####-####-####-############}
*
Expand Down
80 changes: 3 additions & 77 deletions core/inlineimage.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,86 +407,12 @@ public static function IsImage($sMimeType)
* Resize an image so that it fits the maximum width/height defined in the config file
* @param ormDocument $oImage The original image stored as an array (content / mimetype / filename)
* @return ormDocument The resampled image (or the original one if it already fit)
* @deprecated Replaced by ormDocument::ResizeImageToFit
*/
public static function ResizeImageToFit(ormDocument $oImage, &$aDimensions = null)
{
$img = false;
switch($oImage->GetMimeType())
{
case 'image/gif':
case 'image/jpeg':
case 'image/png':
$img = @imagecreatefromstring($oImage->GetData());
break;

default:
// Unsupported image type, return the image as-is
$aDimensions = null;
return $oImage;
}
if ($img === false)
{
$aDimensions = null;
return $oImage;
}
else
{
// Let's scale the image, preserving the transparency for GIFs and PNGs
$iWidth = imagesx($img);
$iHeight = imagesy($img);
$aDimensions = array('width' => $iWidth, 'height' => $iHeight);
$iMaxImageSize = (int)MetaModel::GetConfig()->Get('inline_image_max_storage_width', 0);

if (($iMaxImageSize > 0) && ($iWidth <= $iMaxImageSize) && ($iHeight <= $iMaxImageSize))
{
// No need to resize
return $oImage;
}

$fScale = min($iMaxImageSize / $iWidth, $iMaxImageSize / $iHeight);

$iNewWidth = (int) ($iWidth * $fScale);
$iNewHeight = (int) ($iHeight * $fScale);

$aDimensions['width'] = $iNewWidth;
$aDimensions['height'] = $iNewHeight;

$new = imagecreatetruecolor($iNewWidth, $iNewHeight);

// Preserve transparency
if(($oImage->GetMimeType() == "image/gif") || ($oImage->GetMimeType() == "image/png"))
{
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
imagealphablending($new, false);
imagesavealpha($new, true);
}

imagecopyresampled($new, $img, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iWidth, $iHeight);

ob_start();
switch ($oImage->GetMimeType())
{
case 'image/gif':
imagegif($new); // send image to output buffer
break;

case 'image/jpeg':
imagejpeg($new, null, 80); // null = send image to output buffer, 80 = good quality
break;

case 'image/png':
imagepng($new, null, 5); // null = send image to output buffer, 5 = medium compression
break;
}
$oNewImage = new ormDocument(ob_get_contents(), $oImage->GetMimeType(), $oImage->GetFileName());
@ob_end_clean();

imagedestroy($img);
imagedestroy($new);

return $oNewImage;
}

$iMaxImageSize = (int)MetaModel::GetConfig()->Get('inline_image_max_storage_width', 0);
return $oImage->ResizeImageToFit($iMaxImageSize, $iMaxImageSize, $aDimensions);
}

/**
Expand Down
97 changes: 97 additions & 0 deletions core/ormdocument.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,108 @@ public static function DownloadDocument(WebPage $oPage, $sClass, $id, $sAttCode,
}
}

/**
* Resize an image so that it fits in the given dimensions
* @param int $iMaxImageWidth Maximum width for the resized image
* @param int $iMaxImageHeight Maximum height for the resized image
* @param array|null $aFinalDimensions Image dimensions after resizing or null if unable to read the image
* @return ormDocument The resampled image
*
*/
public function ResizeImageToFit(int $iMaxWidth, int $iMaxHeight, array|null &$aFinalDimensions = null) : static
{
$aFinalDimensions = null;
// If gd extension is not loaded, we put a warning in the log and return the image as is
if (extension_loaded('gd') === false) {
IssueLog::Warning('Image could not be resized as the "gd" extension does not seem to be loaded. Its dimensions will remain the same instead of ' . $iMaxWidth . 'x' . $iMaxHeight);
return $this;
}
$oGdImage = false;
switch($this->GetMimeType()) {
case 'image/gif':
case 'image/jpeg':
case 'image/png':
$oGdImage = @imagecreatefromstring($this->GetData());
break;
default:
// Unsupported image type, return the image as-is
return $this;
}

if ($oGdImage === false) {
IssueLog::Warning('Image could not be resized as . It will remain as imagecreatefromstring could not read its data.Its dimensions will remain the same instead of ' . $iMaxWidth . 'x' . $iMaxHeight);
return $this;
}

$iWidth = imagesx($oGdImage);
$iHeight = imagesy($oGdImage);

if ( ($iMaxWidth === 0 || $iWidth <= $iMaxWidth) && ($iMaxHeight === 0 || $iHeight <= $iMaxHeight)) {
// No need to resize
$aFinalDimensions = [
'width' => $iWidth,
'height' =>$iHeight
];
return $this;
}

$fScale = 1.0;
if ($iMaxWidth > 0) {
$fScale = min($fScale, $iMaxWidth / $iWidth);
}
if ($iMaxHeight > 0) {
$fScale = min($fScale, $iMaxHeight / $iHeight);
}
$iNewWidth = (int)($iWidth * $fScale);
$iNewHeight = (int)($iHeight * $fScale);

$oNewGdImage = imagecreatetruecolor($iNewWidth, $iNewHeight);


$aFinalDimensions = [
'width' => $iNewWidth,
'height' =>$iNewHeight
];

// Preserve transparency
if($this->GetMimeType() == "image/gif" || $this->GetMimeType() == "image/png") {
imagecolortransparent($oNewGdImage, imagecolorallocatealpha($oNewGdImage, 0, 0, 0, 127));
imagealphablending($oNewGdImage, false);
imagesavealpha($oNewGdImage, true);
}
imagecopyresampled($oNewGdImage, $oGdImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iWidth, $iHeight);

ob_start();
switch ($this->GetMimeType()) {
case 'image/gif':
imagegif($oNewGdImage); // send image to output buffer
break;

case 'image/jpeg':
imagejpeg($oNewGdImage, null, 80); // null = send image to output buffer, 80 = good quality
break;

case 'image/png':
imagepng($oNewGdImage, null, 5); // null = send image to output buffer, 5 = medium compression
break;
}
$oResampledImage = new ormDocument(ob_get_contents(), $this->GetMimeType(), $this->GetFileName());
@ob_end_clean();

imagedestroy($oGdImage);
imagedestroy($oNewGdImage);

return $oResampledImage;

}

/**
* @return string
*/
public function GetSignature(): string
{
return md5($this->GetData() ?? '');
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,10 @@ public function HandlePictureForm(Request $oRequest)
$oCurContact = UserRights::GetContactObject();
// Resizing image
$oAttDef = MetaModel::GetAttributeDef(get_class($oCurContact), $sPictureAttCode);
$aSize = utils::GetImageSize($oImage->GetData());
$oImage = utils::ResizeImageToFit($oImage, $aSize[0], $aSize[1], $oAttDef->Get('storage_max_width'),
$oAttDef->Get('storage_max_height'));
$oImage = $oImage->ResizeImageToFit(
$oAttDef->Get('storage_max_width'),
$oAttDef->Get('storage_max_height')
);
// Setting it to the contact
$oCurContact->Set($sPictureAttCode, $oImage);
// Forcing allowed writing on the object if necessary.
Expand Down
6 changes: 4 additions & 2 deletions pages/ajax.render.php
Original file line number Diff line number Diff line change
Expand Up @@ -2002,7 +2002,8 @@ function(data){
$oDoc = utils::ReadPostedDocument('upload');
if (InlineImage::IsImage($oDoc->GetMimeType())) {
$aDimensions = null;
$oDoc = InlineImage::ResizeImageToFit($oDoc, $aDimensions);
$iMaxImageSize = (int)MetaModel::GetConfig()->Get('inline_image_max_storage_width', 0);
$oDoc = $oDoc->ResizeImageToFit($iMaxImageSize, $iMaxImageSize, $aDimensions);
/** @var InlineImage $oAttachment */
$oAttachment = MetaModel::NewObject('InlineImage');
$oAttachment->Set('expire', time() + MetaModel::GetConfig()->Get('draft_attachments_lifetime'));
Expand Down Expand Up @@ -2059,7 +2060,8 @@ function(data){
));
} else {
$aDimensions = null;
$oDoc = InlineImage::ResizeImageToFit($oDoc, $aDimensions);
$iMaxImageSize = (int)MetaModel::GetConfig()->Get('inline_image_max_storage_width', 0);
$oDoc = $oDoc->ResizeImageToFit($iMaxImageSize, $iMaxImageSize, $aDimensions);
/** @var InlineImage $oAttachment */
$oAttachment = MetaModel::NewObject('InlineImage');
$oAttachment->Set('expire', time() + MetaModel::GetConfig()->Get('draft_attachments_lifetime'));
Expand Down
Loading