<?
function BXIBlockAfterSave($arFields)
{
// Вначале ПОЛНОСТЬЮ очищаем свойство PHOTO_SM
$db_props = CIBlockElement::GetProperty($arFields["IBLOCK_ID"], $arFields['ID'], "sort", "asc", Array("CODE"=>"PHOTO_SM"));
//print_r ($db_props);die();
while($ar_props = $db_props->Fetch())
{
if ($ar_props["VALUE"])
{
$ar_val = $ar_props["VALUE"];
$ar_val_id = $ar_props["PROPERTY_VALUE_ID"];
// формируем путь к файлу картинки путем сложения адреса сайта и внутреннего пути к картинке
$img_path2 = $_SERVER['DOCUMENT_ROOT'].CFile::GetPath($ar_props["VALUE"]);
$new_make_file = CFile::MakeFileArray($img_path2);
$new_make_file["del"]="Y";
CIBlockElement::SetPropertyValueCode($arFields["ID"], "PHOTO_SM", Array ($ar_val_id => Array("VALUE"=>$new_make_file) ) );
@unlink($new_make_file);
}
}
// Стандартный пример - создание изображения для PRIVIEW_PICTURE из DETAIL_PICTURE
if(1==1 || $_POST['MAKE_PREVIEW_PICTURE']=='Y')
{
$dbr = CIBlockElement::GetByID($arFields['ID']);
if(($ar = $dbr->Fetch()) && $ar['DETAIL_PICTURE']>0)
{
$img_path = $_SERVER['DOCUMENT_ROOT'].CFile::GetPath($ar['DETAIL_PICTURE']);
$width = 200;
$height = 200;
list($width_orig, $height_orig) = getimagesize($img_path);
if($width && ($width_orig < $height_orig))
$width = ($height / $height_orig) * $width_orig;
else
$height = ($width / $width_orig) * $height_orig;
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($img_path);
imagecopyresized($image_p, $image, 0, 0, 0, 0,
$width, $height, $width_orig, $height_orig);
$tmpnm = mt_rand();
$new_img_path = tempnam("/tmp", "FOO").$tmpnm.".jpg";
imagejpeg($image_p, $new_img_path);
$be = new CIBlockElement();
$be->Update($arFields['ID'],
Array('PREVIEW_PICTURE' => CFile::MakeFileArray($new_img_path)),
false);
@unlink($new_img_path);
}
}
// Самое интересное - берем элементы из MORE_PHOTO, ресайзим и добавляем в PHOTO_SM
$db_props = CIBlockElement::GetProperty($arFields["IBLOCK_ID"], $arFields['ID'], "sort", "asc", Array("CODE"=>"MORE_PHOTO"));
$arFils=array();
while($ar_prop1 = $db_props->Fetch())
{
$ar_prop=$ar_prop1["VALUE"];
$k=CFile::GetPath($ar_prop);
$img_path2 = $_SERVER['DOCUMENT_ROOT'].$k;
$width = 200;
$height = 150;
list($width_orig, $height_orig) = getimagesize($img_path2);
if ($width_orig > $width || $height_orig > $height)
{
if($width && ($width_orig < $height_orig))
$width = ($height / $height_orig) * $width_orig;
else
$height = ($width / $width_orig) * $height_orig;
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($img_path2);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
$tmpnm = mt_rand();
$new_img_path = tempnam("/tmp", "FOO").$tmpnm.".jpg";;
imagejpeg($image_p, $new_img_path);
$new_make_file = CFile::MakeFileArray($new_img_path);
echo "New path:".$new_img_path."<br>";
$arFils=array(
'VALUE' => $new_make_file);
CIBlockElement::SetPropertyValues( $arFields['ID'], $arFields["IBLOCK_ID"], $arFils, "PHOTO_SM");
@unlink($new_img_path);
}
}
}?>
|