function recalcSizeArray($arr, $max_width = null, $max_height = null) { if ($arr === false) { return array(1, 1); } return recalcSize($arr['width'] ? $arr['width'] : $arr[0], $arr['height'] ? $arr['height'] : $arr[0], $max_width, $max_height); }
/** * Method description * * More detailed method description * @param void * @return void */ function preRender() { $this->checkAndSetData(); if (!$this->file instanceof iFile && isset($this->src) && isset($this->subst_src)) { $this->setSrc(sprintf($this->getSubstSrc(), $this->getSrc())); } if (empty($this->alt)) { $this->setAlt(basename($this->getSrc())); } if (empty($this->src)) { $this->setVisible(0); parent::preRender(); return; } if (($this->getMaxWidth() || $this->getMaxHeight()) && !is_null($this->file)) { $iDeco = new ImageDecorator($this->file); $a = recalcSize($iDeco->getWidth(), $iDeco->getHeight(), $this->getMaxWidth(), $this->getMaxHeight()); $this->setWidth($a['0']); $this->setHeight($a['1']); } if ($this->getWithPreview()) { $js = <<<EOD \$(document).ready(function(){ \t\$('#{$this->getId()}').css('cursor','pointer').click(function(){ \t\tvar iot = \$("#{$this->getId()}").offset().top; \t\t\$('<div id="{$this->getId()}_preview" class="image_preview"><img src="{$this->getSrc()}" /></div>').appendTo('body'); \t\tvar i_w = \$('#{$this->getId()}_preview > img').width(); \t\tvar i_h = \$('#{$this->getId()}_preview > img').height(); \t\tvar p = p1 = p2= 1; \t\tif(i_w > 800) p1 = 800 / i_w; \t\tif(i_h > 600) p2 = 600 / i_h; \t\tp = Math.min(p1,p2); \t\ti_w = i_w*p;i_h = i_h*p; \t\tvar o_l = \$("#{$this->getId()}").offset().left; \t\tif(o_l < 20) o_l = 20; \t\tif(o_l + i_w > \$(window).width()) \t\t\to_l = \$(window).width()-i_w-40; \t\t\$('#{$this->getId()}_preview').width(i_w).height(i_h).css('left',o_l).css('top',iot); \t\t\$('#{$this->getId()}_preview > img').width(i_w).height(i_h).click(function() { \t\t\t\$("#{$this->getId()}_preview").remove(); \t\t}); \t}); }); EOD; $this->javascript->addBeforeWidget($js); } parent::preRender(); }
/** * Изменения размеров, качества изображения. * * Изменяет размеры изображения так, чтобы они не првышали переданные $maxHeight и $maxWidth. * Качество устанавливается переменной $quality, которая принимет значения от 1 до 100; * * Вновь созданное(конвертированное) изображение сохраняется в объект $targetFile. * Если указанно $targetFile = null изображение сохраняется вместо оригинала. * * Функция возвращает измененное изображение. * * @throws DecoratorException * @param string $targetFile * @param int $maxWidth * @param int $maxHeight * @paran int $quality * @return iFile */ public function resize($targetFile = null, $maxWidth, $maxHeight, $quality = 100) { if ($quality < 1 || $quality > 100) { throw new DecoratorException('Quality must be integer from 1 to 100, but ' . $quality . ' given.'); } if (is_null($targetFile)) { $targetFile = $this; } $this->getImageSize(); $proportion = $this->width / $this->height; /*if($this->width >= $this->height) { $tw = $maxWidth; $th = (int)round($tw / $proportion); } else{ $th = $maxHeight; $tw = (int)round( $proportion * $th); }*/ list($tw, $th) = recalcSize($this->width, $this->height, $maxWidth, $maxHeight); $image = $this->createImage(); $image_p = imagecreatetruecolor($tw, $th); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $tw, $th, $this->width, $this->height); $tmpFile = new TempFile(); imagejpeg($image_p, $tmpFile, $quality); imagedestroy($image_p); $tmpFile->move($targetFile); if ($targetFile instanceof ImageDecorator) { $targetFile->width = $tw; $targetFile->height = $th; $targetFile->type = IMAGETYPE_JPEG; } return $targetFile; }