Exemplo n.º 1
0
 /**
  * расчёт парметров выравнивания двух картинок по ВЫСОТЕ
  * $margin - поле сверху и снизу конечной картинки
  * $offset - смещение картинки 1
  * возвращает массив параметров, какие надо установить у картинок
  */
 function image_align($file1, $file2, $pos1, $pos2, $margin = 0, $offset = 0)
 {
     $array = array();
     $info1 = ImageUtils::info($file1);
     $info2 = ImageUtils::info($file2);
     $h1 = $info1['height'];
     $h2 = $info2['height'];
     //echo "$h1<br>$h2<br>";
     $h_max = $h1 >= $h2 ? $h1 : $h2;
     $h_min = $h1 >= $h2 ? $h2 : $h1;
     $h_delta = $h_max - $h_min;
     if ($pos1 == 'top' && $pos2 == 'top') {
         $array['y1'] = $margin + $offset;
         $array['y2'] = $margin;
         $array['h'] = $h_max + $margin * 2 + $offset;
     }
     if ($pos1 == 'top' && $pos2 == 'down') {
         $array['y1'] = $margin + $offset + $h2;
         $array['y2'] = $margin;
         $array['h'] = $h_max + $margin * 2 + $offset + $h2;
     }
     if ($pos1 == 'down' && $pos2 == 'down') {
         $array['y1'] = $margin + $offset + $h2 - $h_min;
         $array['y2'] = $margin + $h1 - $h_min;
         $array['h'] = $h_max + $margin * 2 + $offset;
     }
     if ($pos1 == 'down' && $pos2 == 'top') {
         $array['y1'] = $margin;
         $array['y2'] = $margin + $offset + $h1;
         $array['h'] = $h_max + $margin * 2 + $offset + $h1;
     }
     return $array;
 }
Exemplo n.º 2
0
 public function resizeImage()
 {
     if ($this->getFileType() != self::FILE_IMAGE) {
         return false;
     }
     // узнаем ширину и высоту
     $w = self::$_maxWidth;
     $h = self::$_maxHeight;
     if (!self::$_initSize) {
         $w = Yii::app()->params['upload_image_width'];
         $h = Yii::app()->params['upload_image_height'];
         if ($w == null || !is_numeric($w) || $w < 0) {
             $w = 0;
         }
         if ($h == null || !is_numeric($h) || $h < 0) {
             $h = 0;
         }
         self::$_maxWidth = intval($w);
         self::$_maxHeight = intval($h);
         self::$_initSize = true;
     }
     if ($w == 0 && $h == 0) {
         return false;
     }
     $path = $this->getFilePath(true);
     if ($path == null || !file_exists($path)) {
         return false;
     }
     // если размеры меньше
     $img = new ImageUtils();
     $a = $img->info($path);
     if ($a['width'] <= $w) {
         $w = 0;
     }
     if ($a['height'] <= $h) {
         $h = 0;
     }
     if ($w == 0 && $h == 0) {
         return false;
     }
     if (!$img->open($path)) {
         return false;
     }
     // определяем по ширине или высоте делать ресайз:
     $newW = $w;
     $newH = $h;
     if ($w == 0 && $h != 0) {
         $newW = round($h * $img->width / $img->height);
     } else {
         if ($h == 0 && $w != 0) {
             $newH = round($w * $img->height / $img->width);
         } else {
             $tmpH = round($w * $img->height / $img->width);
             if ($tmpH > $h) {
                 $newW = round($h * $img->width / $img->height);
             } else {
                 $newH = round($w * $img->height / $img->width);
             }
         }
     }
     $img->resize($newW, $newH);
     $img->save($path, 94);
     return true;
 }