VM 2.x Модификации для создания квадратной миниатюры с обрезанием по центру

red_heads

Знаток
Регистрация
30 Янв 2007
Сообщения
175
Реакции
28
Есть ли готовый код , плагин? Чтоб при создании миниатюры изображение обрезалось , получалось квадратное превью. Прошу поделится.
 
Два решения

1. нашла данную функцию в модуле News Show Pro GK5, теперь можно вывести красиво товары

2. Нашла решение, у меня работает,
файл virtuemart img2thumb.php,

Код:
private function NewImgResize($orig_img,$newxsize,$newysize,$filename)
    {
        //getimagesize returns array
        // [0] = width in pixels
        // [1] = height in pixels
        // [2] = type
        // [3] = img tag "width=xx height=xx" values


        $orig_size = getimagesize($filename);

        $newxsize = (int)$newxsize;
        $newysize = (int)$newysize;
        if(empty($newxsize) and empty($newysize)){
            vmWarn('NewImgResize failed x,y = 0','NewImgResize failed x,y = 0');
            return false;
        }
        $maxX = $newxsize;
        $maxY = $newysize;

        if ($orig_size[0]<$orig_size[1])
        {
            $newxsize = (int)$newysize * ($orig_size[0]/$orig_size[1]);
            $adjustX = (int)($maxX - $newxsize)/2;
            $adjustY = 0;
        }
        else
        {
            $newysize = (int) $newxsize / ($orig_size[0]/$orig_size[1]);
            $adjustX = 0;
            $adjustY = (int)($maxY - $newysize)/2;
        }

        /* Original code removed to allow for maxSize thumbnails
        $im_out = ImageCreateTrueColor($newxsize,$newysize);
        ImageCopyResampled($im_out, $orig_img, 0, 0, 0, 0,
            $newxsize, $newysize,$orig_size[0], $orig_size[1]);
        */

        //    New modification - creates new image at maxSize
        if( $this->maxSize )
        {
            if( function_exists("imagecreatetruecolor") )
              $im_out = imagecreatetruecolor($maxX,$maxY);
            else
              $im_out = imagecreate($maxX,$maxY);

            // Need to image fill just in case image is transparent, don't always want black background
            $bgfill = imagecolorallocate( $im_out, $this->bg_red, $this->bg_green, $this->bg_blue );

            if( function_exists( "imageAntiAlias" )) {
                imageAntiAlias($im_out,true);
            }
             imagealphablending($im_out, false);
            if( function_exists( "imagesavealpha")) {
                imagesavealpha($im_out,true);
            }
            if( function_exists( "imagecolorallocatealpha")) {
                $transparent = imagecolorallocatealpha($im_out, 255, 255, 255, 127);
            }

            //imagefill( $im_out, 0,0, $bgfill );
            if( function_exists("imagecopyresampled") ){
                ImageCopyResampled($im_out, $orig_img, $adjustX, $adjustY, 0, 0, $newxsize, $newysize,$orig_size[0], $orig_size[1]);
            }
            else {
                ImageCopyResized($im_out, $orig_img, $adjustX, $adjustY, 0, 0, $newxsize, $newysize,$orig_size[0], $orig_size[1]);
            }

        }
        else
        {

            if( function_exists("imagecreatetruecolor") )
              $im_out = ImageCreateTrueColor($newxsize,$newysize);
            else
              $im_out = imagecreate($newxsize,$newysize);

            if( function_exists( "imageAntiAlias" ))
              imageAntiAlias($im_out,true);
             imagealphablending($im_out, false);
            if( function_exists( "imagesavealpha"))
              imagesavealpha($im_out,true);
            if( function_exists( "imagecolorallocatealpha"))
              $transparent = imagecolorallocatealpha($im_out, 255, 255, 255, 127);

            if( function_exists("imagecopyresampled") )
              ImageCopyResampled($im_out, $orig_img, 0, 0, 0, 0, $newxsize, $newysize,$orig_size[0], $orig_size[1]);
            else
              ImageCopyResized($im_out, $orig_img, 0, 0, 0, 0, $newxsize, $newysize,$orig_size[0], $orig_size[1]);
        }


        return $im_out;
    }



заменить на

Код:
function NewImgResize($orig_img,$newxsize,$newysize,$filename)
    {
    //$newxsize; //new width
    //$newysize; //new height
    $orig_size = getimagesize($filename);
    $heightRatio = $orig_size[1]/$newysize;
    $widthRatio = $orig_size[0]/$newxsize;
    if($heightRatio < $widthRatio){
        $optimalRatio = $heightRatio;
    } else {
        $optimalRatio = $widthRatio;
    }
    $optimalHeight = $orig_size[1]/$optimalRatio;
    $optimalWidth = $orig_size[0]/$optimalRatio;
    $imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);
    $bgfill = imagecolorallocate( $imageResized, $this->bg_red, $this->bg_green, $this->bg_blue );
    imagealphablending($imageResized, false);
    imagesavealpha($imageResized,true);
    $transparent = imagecolorallocatealpha($imageResized, 255, 255, 255, 127);
    imagecopyresampled($imageResized, $orig_img, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $orig_size[0], $orig_size[1]);
    $cropStartX = ( $optimalWidth / 2) - ( $newxsize /2 );
    $cropStartY = ( $optimalHeight/ 2) - ( $newysize/2 );
    $crop = $imageResized;
    $imageResized = imagecreatetruecolor($newxsize , $newysize);
    $bgfill = imagecolorallocate( $imageResized, $this->bg_red, $this->bg_green, $this->bg_blue );
    imagealphablending($imageResized, false);
    imagesavealpha($imageResized,true);
    $transparent = imagecolorallocatealpha($imageResized, 255, 255, 255, 127);
    imagecopyresampled($imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newxsize, $newysize , $newxsize, $newysize);
    return $imageResized;
    }
 
Последнее редактирование:
Спасибо! Работает и на Vm3.
Правда есть ньюанс - после ресайза добавляет границу черного цвета сверху\справа и т.д.
da37e-clip-203kb.jpg

В цветах стоит $transparent = imagecolorallocatealpha($imageResized, 255, 255, 255, 127);.
Может кто-то поборол такую проблему?)
 
а в настройках вирта не то? Там же есть параметры "размер превью" и как делать превью... обрезать или ресайзить
 
а в настройках вирта не то? Там же есть параметры "размер превью" и как делать превью... обрезать или ресайзить
Так может ты нам всем подскажешь (с скриншотом) где изменять эту опцию? %) Спасибо!
 
Назад
Сверху