/**
  * Returns an image with a resized canvas
  * 
  * The image is filled with $color. Use $scale to determine, when to resize.
  *
  * @param WideImage_Image $img
  * @param smart_coordinate $width
  * @param smart_coordinate $height
  * @param smart_coordinate $left
  * @param smart_coordinate $top
  * @param int $color
  * @param string $scale 'up', 'down', 'any'
  * @param boolean $merge
  * @return WideImage_Image
  */
 function execute($img, $width, $height, $left, $top, $color, $scale, $merge)
 {
     $new_width = WideImage_Coordinate::fix($width, $img->getWidth());
     $new_height = WideImage_Coordinate::fix($height, $img->getHeight());
     if ($scale == 'down') {
         $new_width = min($new_width, $img->getWidth());
         $new_height = min($new_height, $img->getHeight());
     } elseif ($scale == 'up') {
         $new_width = max($new_width, $img->getWidth());
         $new_height = max($new_height, $img->getHeight());
     }
     $new = WideImage::createTrueColorImage($new_width, $new_height);
     if ($img->isTrueColor()) {
         if ($color === null) {
             $color = $new->allocateColorAlpha(0, 0, 0, 127);
         }
     } else {
         imagepalettecopy($new->getHandle(), $img->getHandle());
         if ($img->isTransparent()) {
             $new->copyTransparencyFrom($img);
             $tc_rgb = $img->getTransparentColorRGB();
             $t_color = $new->allocateColorAlpha($tc_rgb);
         }
         if ($color === null) {
             if ($img->isTransparent()) {
                 $color = $t_color;
             } else {
                 $color = $new->allocateColorAlpha(255, 0, 127, 127);
             }
             imagecolortransparent($new->getHandle(), $color);
         }
     }
     $new->fill(0, 0, $color);
     $x = WideImage_Coordinate::fix($left, $new->getWidth(), $img->getWidth());
     $y = WideImage_Coordinate::fix($top, $new->getHeight(), $img->getHeight());
     // blending for truecolor images
     if ($img->isTrueColor()) {
         $new->alphaBlending($merge);
     }
     // not-blending for palette images
     if (!$merge && !$img->isTrueColor() && isset($t_color)) {
         $new->getCanvas()->filledRectangle($x, $y, $x + $img->getWidth(), $y + $img->getHeight(), $t_color);
     }
     $img->copyTo($new, $x, $y);
     return $new;
 }
Example #2
0
 /**
  * Restores an image from serialization. Called automatically upon unserialize().
  */
 function __wakeup()
 {
     $temp_image = WideImage::loadFromString($this->sdata);
     $temp_image->releaseHandle();
     $this->handle = $temp_image->handle;
     $temp_image = null;
     $this->sdata = null;
 }
Example #3
0
 public function userUploadPhotoAction()
 {
     $check_auth = new CheckAuthorization($this->getQemyDb());
     $check_auth->check();
     $user = new User($this->getQemyDb(), $check_auth->getUserRow());
     $user->setAuthChecker($check_auth);
     if (!$user->isAuth()) {
         Application::denied();
         $this->setData(array('result' => false));
         return $this;
     }
     $file_name = date("mdy") . '_' . sha1($user->getId() . $user->getEmail() . time() . rand(1, 1000000000.0));
     $storage_host = Application::$config['user_files_opt']['host'];
     $image_url = Application::$config['user_files_opt']['protocol'] . '://' . $storage_host . '/img/profile/' . $file_name . '.png';
     try {
         $mWideImage = WideImage::loadFromUpload('image');
         $resizedImage = $mWideImage->resize(200, 200, 'outside', 'down')->crop('center', 'center', 200, 200);
         $resizedImage->saveToFile('../' . $storage_host . '/img/profile/' . $file_name . '.png');
         $user->setPhoto($image_url);
         $result = true;
     } catch (WideImage_Exception $err) {
         $result = false;
     }
     $this->setData(array('result' => $result));
     return $this;
 }
 /**
  * @param WideImage_Image $image
  * @param int $radius
  * @param int $color
  * @param int $smoothness
  * @return WideImage_Image
  */
 function execute($image, $radius, $color, $smoothness, $corners)
 {
     if ($smoothness < 1) {
         $sample_ratio = 1;
     } elseif ($smoothness > 16) {
         $sample_ratio = 16;
     } else {
         $sample_ratio = $smoothness;
     }
     $corner = WideImage::createTrueColorImage($radius * $sample_ratio, $radius * $sample_ratio);
     if ($color === null) {
         imagepalettecopy($corner->getHandle(), $image->getHandle());
         $bg_color = $corner->allocateColor(0, 0, 0);
         $corner->fill(0, 0, $bg_color);
         $fg_color = $corner->allocateColor(255, 255, 255);
         $corner->getCanvas()->filledEllipse($radius * $sample_ratio, $radius * $sample_ratio, $radius * 2 * $sample_ratio, $radius * 2 * $sample_ratio, $fg_color);
         $corner = $corner->resize($radius, $radius);
         $result = $image->asTrueColor();
         $tc = $result->getTransparentColor();
         if ($tc == -1) {
             $tc = $result->allocateColorAlpha(255, 255, 255, 127);
             imagecolortransparent($result->getHandle(), $tc);
             $result->setTransparentColor($tc);
         }
         if ($corners & WideImage::SIDE_TOP_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_TOP) {
             $result = $result->applyMask($corner, -1, -1);
         }
         $corner = $corner->rotate(90);
         if ($corners & WideImage::SIDE_TOP_RIGHT || $corners & WideImage::SIDE_TOP || $corners & WideImage::SIDE_RIGHT) {
             $result = $result->applyMask($corner, $result->getWidth() - $corner->getWidth() + 1, -1, 100);
         }
         $corner = $corner->rotate(90);
         if ($corners & WideImage::SIDE_BOTTOM_RIGHT || $corners & WideImage::SIDE_RIGHT || $corners & WideImage::SIDE_BOTTOM) {
             $result = $result->applyMask($corner, $result->getWidth() - $corner->getWidth() + 1, $result->getHeight() - $corner->getHeight() + 1, 100);
         }
         $corner = $corner->rotate(90);
         if ($corners & WideImage::SIDE_BOTTOM_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_BOTTOM) {
             $result = $result->applyMask($corner, -1, $result->getHeight() - $corner->getHeight() + 1, 100);
         }
         return $result;
     } else {
         $bg_color = $color;
         $corner->fill(0, 0, $bg_color);
         $fg_color = $corner->allocateColorAlpha(127, 127, 127, 127);
         $corner->getCanvas()->filledEllipse($radius * $sample_ratio, $radius * $sample_ratio, $radius * 2 * $sample_ratio, $radius * 2 * $sample_ratio, $fg_color);
         $corner = $corner->resize($radius, $radius);
         $result = $image->copy();
         if ($corners & WideImage::SIDE_TOP_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_TOP) {
             $result = $result->merge($corner, -1, -1, 100);
         }
         $corner = $corner->rotate(90);
         if ($corners & WideImage::SIDE_TOP_RIGHT || $corners & WideImage::SIDE_TOP || $corners & WideImage::SIDE_RIGHT) {
             $result = $result->merge($corner, $result->getWidth() - $corner->getWidth() + 1, -1, 100);
         }
         $corner = $corner->rotate(90);
         if ($corners & WideImage::SIDE_BOTTOM_RIGHT || $corners & WideImage::SIDE_RIGHT || $corners & WideImage::SIDE_BOTTOM) {
             $result = $result->merge($corner, $result->getWidth() - $corner->getWidth() + 1, $result->getHeight() - $corner->getHeight() + 1, 100);
         }
         $corner = $corner->rotate(90);
         if ($corners & WideImage::SIDE_BOTTOM_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_BOTTOM) {
             $result = $result->merge($corner, -1, $result->getHeight() - $corner->getHeight() + 1, 100);
         }
         return $result;
     }
 }
Example #5
0
     */
    static function createTrueColorImage($width, $height)
    {
        return WideImage_TrueColorImage::create($width, $height);
    }
    /**
     * Check whether the given handle is a valid GD resource
     * 
     * @param mixed $handle The variable to check
     * @return bool
     */
    static function isValidImageHandle($handle)
    {
        return is_resource($handle) && get_resource_type($handle) == 'gd';
    }
    /**
     * Throws exception if the handle isn't a valid GD resource
     * 
     * @param mixed $handle The variable to check
     */
    static function assertValidImageHandle($handle)
    {
        if (!self::isValidImageHandle($handle)) {
            throw new WideImage_InvalidImageHandleException("{$handle} is not a valid image handle.");
        }
    }
}
WideImage::checkGD();
WideImage::registerCustomMapper('WideImage_Mapper_BMP', 'image/bmp', 'bmp');
WideImage::registerCustomMapper('WideImage_Mapper_TGA', 'image/tga', 'tga');
 /**
  * (non-PHPdoc)
  * @see WideImage_Image#asTrueColor()
  */
 function asTrueColor()
 {
     $width = $this->getWidth();
     $height = $this->getHeight();
     $new = WideImage::createTrueColorImage($width, $height);
     if ($this->isTransparent()) {
         $new->copyTransparencyFrom($this);
     }
     if (!imageCopy($new->getHandle(), $this->handle, 0, 0, 0, 0, $width, $height)) {
         throw new WideImage_GDFunctionResultException("imagecopy() returned false");
     }
     return $new;
 }