function testFix()
 {
     $this->assertIdentical(10, wiDimension::fix(100, '10%'));
     $this->assertIdentical(10, wiDimension::fix(100, '10'));
     $this->assertIdentical(300, wiDimension::fix(200, '150%'));
     $this->assertIdentical(150, wiDimension::fix(200, '150'));
 }
示例#2
0
 protected function prepareDimensions($img, $width, $height, $fit)
 {
     if ($width === null) {
         $width = $height;
     }
     if ($height === null) {
         $height = $width;
     }
     $width = wiDimension::fix($img->getWidth(), $width);
     $height = wiDimension::fix($img->getHeight(), $height);
     $dim = array();
     if ($fit == 'fill') {
         $dim['width'] = $width;
         $dim['height'] = $height;
     } elseif ($fit == 'inside' || $fit == 'outside') {
         $rx = $img->getWidth() / $width;
         $ry = $img->getHeight() / $height;
         if ($fit == 'inside') {
             $ratio = $rx > $ry ? $rx : $ry;
         } else {
             $ratio = $rx < $ry ? $rx : $ry;
         }
         $dim['width'] = round($img->getWidth() / $ratio);
         $dim['height'] = round($img->getHeight() / $ratio);
     } else {
         throw new wiInvalidFitMethodException("{$fit} is not a valid resize-fit method.");
     }
     return $dim;
 }
 protected function prepareDimensions($img, $width, $height, $fit)
 {
     list($width, $height) = wiDimension::fixForResize($img, $width, $height);
     //echo "w={$img->getWidth()}, h={$img->getHeight()} ... rw=$width, rh=$height ";
     if ($width === 0 || $height === 0) {
         return array('width' => 0, 'height' => 0);
     }
     if ($fit == null) {
         $fit = 'inside';
     }
     $dim = array();
     if ($fit == 'fill') {
         $dim['width'] = $width;
         $dim['height'] = $height;
     } elseif ($fit == 'inside' || $fit == 'outside') {
         $rx = $img->getWidth() / $width;
         $ry = $img->getHeight() / $height;
         if ($fit == 'inside') {
             $ratio = $rx > $ry ? $rx : $ry;
         } else {
             $ratio = $rx < $ry ? $rx : $ry;
         }
         $dim['width'] = round($img->getWidth() / $ratio);
         $dim['height'] = round($img->getHeight() / $ratio);
     } else {
         throw new wiInvalidFitMethodException("{$fit} is not a valid resize-fit method.");
     }
     return $dim;
 }
 function execute($image, $mask, $left = 0, $top = 0)
 {
     $left = wiDimension::fix($image->getWidth(), $left);
     $top = wiDimension::fix($image->getHeight(), $top);
     $width = $image->getWidth();
     if ($width > $mask->getWidth()) {
         $width = $mask->getWidth();
     }
     $height = $image->getHeight();
     if ($height > $mask->getHeight()) {
         $height = $mask->getHeight();
     }
     $result = $image->asTrueColor();
     $result->alphaBlending(false);
     $result->saveAlpha(true);
     $srcTransparentColor = $image->getTransparentColor();
     if ($srcTransparentColor >= 0) {
         $trgb = $image->getColorRGB($srcTransparentColor);
         $trgb['alpha'] = 127;
         $destTransparentColor = $result->allocateColorAlpha($trgb);
         $result->setTransparentColor($destTransparentColor);
     } else {
         $destTransparentColor = $result->allocateColorAlpha(255, 255, 255, 127);
     }
     for ($x = 0; $x < $width; $x++) {
         for ($y = 0; $y < $height; $y++) {
             if ($left + $x < $image->getWidth() && $top + $y < $image->getHeight()) {
                 $srcColor = $image->getColorAt($left + $x, $top + $y);
                 if ($srcColor == $srcTransparentColor) {
                     $destColor = $destTransparentColor;
                 } else {
                     $maskRGB = $mask->getRGBAt($x, $y);
                     if ($maskRGB['red'] == 0) {
                         $destColor = $destTransparentColor;
                     } elseif ($srcColor >= 0) {
                         $imageRGB = $image->getRGBAt($left + $x, $top + $y);
                         $level = $maskRGB['red'] / 255 * (1 - $imageRGB['alpha'] / 127);
                         $imageRGB['alpha'] = 127 - round($level * 127);
                         if ($imageRGB['alpha'] == 127) {
                             $destColor = $destTransparentColor;
                         } else {
                             $destColor = $result->allocateColorAlpha($imageRGB);
                         }
                     } else {
                         $destColor = $destTransparentColor;
                     }
                 }
                 $result->setColorAt($left + $x, $top + $y, $destColor);
             }
         }
     }
     return $result;
 }
示例#5
0
 function execute($img, $left, $top, $width, $height)
 {
     $width = wiDimension::fix($img->getWidth(), $width);
     $height = wiDimension::fix($img->getHeight(), $height);
     $new = wiTrueColorImage::create($width, $height);
     if ($img->isTransparent()) {
         $new->copyTransparencyFrom($img);
         imagecopyresized($new->getHandle(), $img->getHandle(), 0, 0, $left, $top, $width, $height, $width, $height);
     } else {
         $new->alphaBlending(false);
         $new->saveAlpha(true);
         imagecopyresampled($new->getHandle(), $img->getHandle(), 0, 0, $left, $top, $width, $height, $width, $height);
     }
     return $new;
 }
 function execute($base, $overlay, $left, $top, $pct)
 {
     $x = wiDimension::fix($base->getWidth(), $left);
     $y = wiDimension::fix($base->getHeight(), $top);
     $result = $base->asTrueColor();
     $result->alphaBlending(true);
     $result->saveAlpha(true);
     if ($pct == 0) {
         return $result;
     }
     if ($pct < 100) {
         imagecopymerge($result->getHandle(), $overlay->getHandle(), $x, $y, 0, 0, $overlay->getWidth(), $overlay->getHeight(), $pct);
     } else {
         imagecopy($result->getHandle(), $overlay->getHandle(), $x, $y, 0, 0, $overlay->getWidth(), $overlay->getHeight());
     }
     return $result;
 }
 function execute($img, $left, $top, $width, $height)
 {
     $left = wiDimension::fix($img->getWidth(), $left);
     $top = wiDimension::fix($img->getHeight(), $top);
     $width = wiDimension::fix($img->getWidth(), $width);
     if ($width > $img->getWidth() - $left) {
         $width = $img->getWidth() - $left;
     }
     $height = wiDimension::fix($img->getHeight(), $height);
     if ($height > $img->getHeight() - $top) {
         $height = $img->getHeight() - $top;
     }
     if ($left < 0) {
         $width = $left + $width;
         $left = 0;
     }
     if ($left + $width > $img->getWidth()) {
         $width = $img->getWidth() - $left;
     }
     if ($width < 0) {
         $width = 0;
     }
     if ($top < 0) {
         $height = $top + $height;
         $top = 0;
     }
     if ($top + $height > $img->getHeight()) {
         $top = $img->getHeight() - $top;
     }
     if ($height < 0) {
         $height = 0;
     }
     $new = wiTrueColorImage::create($width, $height);
     if ($img->isTransparent()) {
         $new->copyTransparencyFrom($img);
         imagecopyresized($new->getHandle(), $img->getHandle(), 0, 0, $left, $top, $width, $height, $width, $height);
     } else {
         $new->alphaBlending(false);
         $new->saveAlpha(true);
         imagecopyresampled($new->getHandle(), $img->getHandle(), 0, 0, $left, $top, $width, $height, $width, $height);
     }
     return $new;
 }
 static function fixForResize($img, $width, $height)
 {
     if ($width === null && $height === null) {
         return array($img->getWidth(), $img->getHeight());
     }
     if ($width !== null) {
         $width = wiDimension::fix($img->getWidth(), $width);
     }
     if ($height !== null) {
         $height = wiDimension::fix($img->getHeight(), $height);
     }
     if ($width === null) {
         $width = round($img->getWidth() * $height / $img->getHeight());
     }
     if ($height === null) {
         $height = round($img->getHeight() * $width / $img->getWidth());
     }
     return array($width, $height);
 }