Example #1
0
 public function setOptions($options = array())
 {
     require_once AK_CONTRIB_DIR . DS . 'pear' . DS . 'Image' . DS . 'Tools.php';
     $this->Image->Transform =& Image_Tools::factory('Watermark');
     $default_options = array('image' => $this->Image->Transform->createImage($this->Image->image_path));
     $this->options = array_merge($default_options, $options);
     if (empty($this->options['mark']) || !is_file($this->options['mark'])) {
         trigger_error(Ak::t('Option "mark" does not contain a valid Watermark image path'), E_USER_ERROR);
     }
     $this->_variablizeOptions_($this->options);
     $this->Image->Transform->set($this->options);
 }
 /**
  * Draw a line around an image
  *
  * Draw a line around an image. If $offset is 0, the line is just around the image.
  * If $offset is positive, the line is outside the image, and the gad is filled with background color.
  * If $offset is -width / 2, the line is centered on the border of the image.
  * If $offset is -$width, the line in just inside the image.
  * If $offset is negative, the line is inside the image.
  *
  * @param   int $width Line width
  * @param   mixed $color Line color
  * @param   int $offset Distance between line and image.
  *
  * @return  bool|PEAR_Error TRUE on success or PEAR_Error on failure.
  * @access  private
  * @author  Charles Brunet <*****@*****.**>
  */
 function _line($width = 2, $color = '000000', $offset = 0, $background = 'FFFFFF')
 {
     // Agrandir l'image si nécessaire
     $mag = $width + $offset;
     if ($mag > 0) {
         $w = $this->_iWidth + 2 * $mag;
         $h = $this->_iHeight + 2 * $mag;
         // Create the target image
         if (function_exists('imagecreatetruecolor')) {
             $target = imagecreatetruecolor($w, $h);
         } else {
             $target = imagecreate($w, $h);
         }
         if (!Image_Tools::isGDImageResource($target)) {
             return PEAR::raiseError('Cannot initialize new GD image stream');
         }
         $background = Image_Tools_Utils::colorToRGBA($background);
         $background = imagecolorallocate($target, $background['r'], $background['g'], $background['b']);
         imagefilledrectangle($target, 0, 0, $w - 1, $h - 1, $background);
         imagecopy($target, $this->resultImage, $mag, $mag, 0, 0, $this->_iWidth, $this->_iHeight);
         $this->_iWidth = $w;
         $this->_iHeight = $h;
         imagedestroy($this->resultImage);
         $this->resultImage = $target;
     }
     if (!Image_Tools::isGDImageResource($this->resultImage)) {
         return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_resultImage');
     }
     $color = Image_Tools_Utils::colorToRGBA($color);
     $color = imagecolorallocate($this->resultImage, $color['r'], $color['g'], $color['b']);
     $a = $mag < 0 ? 0 - $mag : 0;
     for ($i = $a; $i < $a + $width; ++$i) {
         imagerectangle($this->resultImage, $i, $i, $this->_iWidth - $i - 1, $this->_iHeight - $i - 1, $color);
     }
     return true;
 }
Example #3
0
 /**
  * Get the API version of the common base
  *
  * This methods can be called statically using Image_Tools::getAPIVersion() or
  * from the subclass e.g Image_Tools_Border::getAPIVersion() or
  * $border->getAPIVersion()
  *
  * @return string Image_Tools base class api-version
  * @access protected
  */
 function getAPIVersion()
 {
     if (isset($this)) {
         return $this->apiVersion;
     } else {
         $obj = new Image_Tools();
         return $obj->getAPIVersion();
     }
 }
 /**
  * Draw thumbnail result to resource.
  *
  * @return  bool|PEAR_Error TRUE on success or PEAR_Error on failure.
  * @access  public
  */
 function render()
 {
     if (!Image_Tools::isGDImageResource($this->resultImage)) {
         return PEAR::raiseError('Invalid image resource');
     }
     // Estimate a rectangular portion of the source image and a size of the target image
     if ($this->options['method'] == IMAGE_TOOLS_THUMBNAIL_METHOD_CROP) {
         if ($this->options['percent']) {
             $W = floor($this->options['percent'] / 100 * $this->imageInfo['width']);
             $H = floor($this->options['percent'] / 100 * $this->imageInfo['height']);
         } else {
             $W = $this->options['width'];
             $H = $this->options['height'];
         }
         $width = $W;
         $height = $H;
         $Y = $this->_coord('valign', 'height', $H);
         $X = $this->_coord('halign', 'width', $W);
     } else {
         $W = $this->imageInfo['width'];
         $H = $this->imageInfo['height'];
         $X = 0;
         $Y = 0;
         if ($this->options['percent']) {
             $width = floor($this->options['percent'] / 100 * $W);
             $height = floor($this->options['percent'] / 100 * $H);
         } else {
             $width = $this->options['width'];
             $height = $this->options['height'];
             if ($this->options['method'] == IMAGE_TOOLS_THUMBNAIL_METHOD_SCALE_MIN) {
                 $Ww = $W / $width;
                 $Hh = $H / $height;
                 if ($Ww > $Hh) {
                     $W = floor($width * $Hh);
                     $X = $this->_coord('halign', 'width', $W);
                 } else {
                     $H = floor($height * $Ww);
                     $Y = $this->_coord('valign', 'height', $H);
                 }
             } else {
                 if ($H > $W) {
                     $width = floor($height / $H * $W);
                 } else {
                     $height = floor($width / $W * $H);
                 }
             }
         }
     }
     // Create the target image
     if (function_exists('imagecreatetruecolor')) {
         $target = imagecreatetruecolor($width, $height);
     } else {
         $target = imagecreate($width, $height);
     }
     if (!Image_Tools::isGDImageResource($target)) {
         return PEAR::raiseError('Cannot initialize new GD image stream');
     }
     // enable transparency if supported
     if (Image_Tools_Utils::compareGDVersion('2.0.28', '>=')) {
         // imagealphablending() and imagesavealpha() requires GD 2.0.38
         imagealphablending($target, false);
         imagesavealpha($target, true);
     }
     // Copy the source image to the target image
     if ($this->options['method'] == IMAGE_TOOLS_THUMBNAIL_METHOD_CROP) {
         $result = imagecopy($target, $this->resultImage, 0, 0, $X, $Y, $W, $H);
     } elseif (function_exists('imagecopyresampled')) {
         $result = imagecopyresampled($target, $this->resultImage, 0, 0, $X, $Y, $width, $height, $W, $H);
     } else {
         $result = imagecopyresized($target, $this->resultImage, 0, 0, $X, $Y, $width, $height, $W, $H);
     }
     if (!$result) {
         return PEAR::raiseError('Cannot resize image');
     }
     // Free a memory from the source image and save the resulting thumbnail
     imagedestroy($this->resultImage);
     $this->resultImage = $target;
     return true;
 }
 /**
  * Apply tools to image.
  *
  * This function scan for mask color and closes colors position, grab color
  * at found the position on sample image, then set the pixel color at the same
  * position on destination image.
  *
  * @return  bool|PEAR_Error TRUE on success or PEAR_Error on failure.
  * @access  private
  * @see     Image_Tools_Mask::_getNearestColors()
  */
 function render()
 {
     if (!Image_Tools::isGDImageResource($this->_maskImage)) {
         return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_maskImage');
     }
     if (!Image_Tools::isGDImageResource($this->_sampleImage)) {
         return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_sampleImage');
     }
     if (!Image_Tools::isGDImageResource($this->resultImage)) {
         return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_resultImage');
     }
     $maskWidth = imagesx($this->_maskImage);
     $maskHeight = imagesy($this->_maskImage);
     $sampleWidth = imagesx($this->_sampleImage);
     $sampleHeight = imagesy($this->_sampleImage);
     if ($this->options['antialias']) {
         $closesColors = $this->_getNearestColors();
     } else {
         $closesColors = array($this->options['maskColor']);
     }
     imagealphablending($this->resultImage, true);
     // scan for mask color or closes colors position
     for ($x = 0; $x < $maskWidth; $x++) {
         for ($y = 0; $y < $maskHeight; $y++) {
             if ($x >= $sampleWidth || $y >= $sampleHeight) {
                 continue;
             }
             // grab color at x, y and convert to hex color format
             $index = imagecolorat($this->_maskImage, $x, $y);
             $maskRGBA = imagecolorsforindex($this->_maskImage, $index);
             $maskColor = Image_Color::rgb2hex(array_values($maskRGBA));
             // check color in closes colors collection
             if (in_array($maskColor, $closesColors)) {
                 // grab color at x, y from sample image
                 $index = imagecolorat($this->_sampleImage, $x, $y);
                 $sampleRGBA = imagecolorsforindex($this->_sampleImage, $index);
                 // allocate color on destination image
                 $color = imagecolorresolvealpha($this->resultImage, $sampleRGBA['red'], $sampleRGBA['green'], $sampleRGBA['blue'], $sampleRGBA['alpha']);
                 // set a pixel color at destination image
                 imagesetpixel($this->resultImage, $x, $y, $color);
             }
         }
     }
     return true;
 }
 /**
  * Apply swap color to image and output result.
  *
  * This function swap channel color 'R', 'G', 'B' to set format.
  *
  * @return  bool|PEAR_Error TRUE on success or PEAR_Error on failure.
  * @access  protected
  */
 function render()
 {
     if (!Image_Tools::isGDImageResource($this->resultImage)) {
         return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_resultImage');
     }
     if (!preg_match('@RBG|BGR|BRG|GRB|GBR$@i', $this->options['format'])) {
         return PEAR::raiseError('Invalid swap format');
     }
     $width = imagesx($this->resultImage);
     $height = imagesy($this->resultImage);
     $destImg = imagecreatetruecolor($width, $height);
     for ($x = 0; $x < $width; $x++) {
         for ($y = 0; $y < $height; $y++) {
             $index = imagecolorat($this->resultImage, $x, $y);
             $rgb = imagecolorsforindex($this->resultImage, $index);
             $rgb = Image_Tools_Swap::swapColor($this->options['format'], $rgb);
             $color = imagecolorallocate($destImg, $rgb['r'], $rgb['g'], $rgb['b']);
             imagesetpixel($destImg, $x, $y, $color);
         }
     }
     $this->resultImage = $destImg;
     return true;
 }
Example #7
0
function smarty_function_image($params, &$smarty)
{
    require_once $smarty->_get_plugin_filepath('modifier', 'curlang');
    $src = "";
    $width = 0;
    $height = 0;
    $bevel = A_MODE == 1 ? 4 : 0;
    $attr = "";
    $DOMAIN = !empty($params['domain']) ? preg_replace("/[^a-zA-Z0-9]+/i", "", $params['domain']) : A::$DOMAIN;
    if (!empty($params['data']) && is_array($params['data'])) {
        if (array_key_exists('path', $params['data'])) {
            $row = $params['data'];
            unset($params['data']);
        } elseif (isset($params['data'][0]) && array_key_exists('path', $params['data'][0])) {
            $row = array_shift($params['data']);
        }
        if (!empty($row['path'])) {
            $src = $row['path'];
        }
    } elseif (!empty($params['id'])) {
        if ($row = A::$DB->getRowById($params['id'], $DOMAIN . "_images")) {
            $src = $row['path'];
        }
    }
    $alt = !empty($row['caption']) ? $row['caption'] : "";
    foreach ($params as $_key => $_val) {
        switch ($_key) {
            case "id":
            case "data":
            case "domain":
            case "lightbox":
            case "popup":
            case "noimgheight":
            case "empty":
                break;
            case "src":
                $src = $_val;
                break;
            case "width":
                $width = $_val;
                if (!isset($row['width'])) {
                    $attr .= " {$_key}=\"{$width}\"";
                } elseif ($width <= $row['width']) {
                    if (!empty($scale)) {
                        $_width = ceil($row['width'] / $scale);
                        if ($_width < $width) {
                            $width = (int) $_width;
                        }
                        //$attr.=" $_key=\"$width\"";
                        $width = 0;
                    } else {
                        $scale = $row['width'] / $width;
                        $attr .= " {$_key}=\"{$width}\"";
                    }
                } elseif ($width > $row['width'] && !empty($scale)) {
                    $_width = ceil($row['width'] / $scale);
                    if ($_width < $width) {
                        $width = (int) $_width;
                    }
                    $attr .= " {$_key}=\"{$width}\"";
                    $width = 0;
                }
                break;
            case "height":
                $height = $_val;
                if (!isset($row['height'])) {
                    $attr .= " {$_key}=\"{$height}\"";
                } elseif ($height <= $row['height']) {
                    if (!empty($scale)) {
                        $_height = ceil($row['height'] / $scale);
                        if ($_height < $height) {
                            $height = (int) $_height;
                        }
                        //$attr.=" $_key=\"$height\"";
                    } elseif ($row['height'] > $row['width']) {
                        $scale = $row['height'] / $height;
                        $attr .= " {$_key}=\"{$height}\"";
                    }
                }
                break;
            case "bevel":
                $bevel = (int) $_val;
                break;
            case "imgid":
                $attr .= " id=\"{$_val}\"";
                break;
            case "alt":
                $alt = $_val;
                break;
            default:
                $attr .= " {$_key}=\"{$_val}\"";
        }
    }
    $attr .= " alt=\"" . htmlspecialchars($alt) . "\"";
    if (!empty($src)) {
        $src = preg_replace("/^\\//i", "", $src);
    } elseif (!empty($params['empty'])) {
        $src = "templates/" . $DOMAIN . "/images/" . preg_replace("/[.]{2..}/i", "", $params['empty']);
        if (is_file($src)) {
            return "<img src=\"/{$src}\"{$attr}/>";
        }
    } else {
        return "";
    }
    if ($width > 0 && (!isset($row['width']) || $width < $row['width']) || $height > 0 && (!isset($row['height']) || $height < $row['height'])) {
        $path = pathinfo($src);
        if (preg_match("/^[.\\/]*(files|images)\\/([a-zA-Z0-9-_]+)\\//i", $src, $matches)) {
            $cachename = 'cache/images/' . $matches[2] . '/' . $path['filename'];
        } else {
            $cachename = 'cache/images/' . $path['filename'];
        }
        if ($width > 0) {
            $cachename .= '_w' . $width;
        }
        if ($height > 0) {
            $cachename .= '_h' . $height;
        }
        if ($bevel > 0) {
            $cachename .= '_b' . $bevel;
        }
        $cachename .= '.' . mb_strtolower($path['extension']);
        if (!is_file($cachename) && is_file($src)) {
            require_once "Image/Transform.php";
            $it = Image_Transform::factory('GD');
            $it->load($src);
            if ($width > 0 && $height > 0 && ($it->img_x > $width || $it->img_y > $height)) {
                if ($width < 0 || $height < 0) {
                    return "";
                }
                if ($it->img_x > $width) {
                    $it->scaleByX($width);
                }
                if ($it->new_y > $height) {
                    $it->crop($width, $height, 0, 0);
                }
            } elseif ($width > 0 && $it->img_x > $width) {
                $it->scaleByX($width);
            } elseif ($height > 0 && $it->img_y > $height) {
                $it->scaleByY((int) $height);
            }
            if (!empty($matches[2])) {
                if (!is_dir('cache/images/' . $matches[2])) {
                    mkdir('cache/images/' . $matches[2]);
                }
            }
            $it->save($cachename, '', 100);
            if ($bevel > 0 && $bevel <= 5) {
                require_once "Image/Tools.php";
                $border = Image_Tools::factory('border');
                $border->set('image', $cachename);
                $border->set('style', 'bevel');
                $border->set('params', array($bevel, '#ffffff', '#000000'));
                switch ($it->type) {
                    case 'jpg':
                    case 'jpeg':
                        $border->save($cachename, IMAGETYPE_JPEG);
                        break;
                    case 'gif':
                        $border->save($cachename, IMAGETYPE_GIF);
                        break;
                    case 'png':
                        $border->save($cachename, IMAGETYPE_PNG);
                        break;
                }
            }
        }
        if (is_file($cachename)) {
            $image = "<img src=\"/{$cachename}\"{$attr}/>";
        } else {
            $image = "<img src=\"/image.php?src=" . urlencode($src) . "&x={$width}&y={$height}&b={$bevel}\"{$attr}/>";
        }
    } elseif (isset($row)) {
        $attr = str_replace(" width=\"{$row['width']}\"", "", $attr);
        $attr = str_replace(" height=\"{$row['height']}\"", "", $attr);
        $image = "<img src=\"/{$src}\" width=\"{$row['width']}\" height=\"{$row['height']}\"{$attr}/>";
        $params['popup'] = false;
        if (empty($params['data'])) {
            $params['lightbox'] = false;
        }
    } else {
        $image = "<img src=\"/{$src}\"{$attr}/>";
        $params['popup'] = false;
        if (empty($params['data'])) {
            $params['lightbox'] = false;
        }
    }
    if (!empty($params['popup']) && !empty($row)) {
        if ($params['popup'] === true) {
            $params['popup'] = "Увеличить";
        }
        $caption = !empty($params['alt']) ? $params['alt'] : $row['caption'];
        $caption = preg_replace("/[^a-zA-Zа-яА-Я0-9 ]+/iu", " ", smarty_modifier_curlang($caption));
        if (A_MODE == A_MODE_FRONT && is_file($css = "templates/" . A::$DOMAIN . "/imagewin.css")) {
            $image = "<a href=\"javascript:open_imgwindow('/{$src}','{$caption}',{$row['width']},{$row['height']},'/{$css}')\" title=\"{$params['popup']}\">{$image}</a>";
        } else {
            $image = "<a href=\"javascript:open_imgwindow('/{$src}','{$caption}',{$row['width']},{$row['height']})\" title=\"{$params['popup']}\">{$image}</a>";
        }
    } elseif (!empty($params['lightbox']) && !empty($row)) {
        $caption = !empty($params['alt']) ? $params['alt'] : $row['caption'];
        $caption = smarty_modifier_curlang($caption);
        $group = !empty($params['group']) ? "[{$params['group']}]" : "[default]";
        $image = "<a href=\"/{$src}\" rel=\"lightbox{$group}\" title=\"{$caption}\">{$image}</a>";
        if (!empty($params['data'])) {
            while ($row = array_shift($params['data'])) {
                $row['caption'] = smarty_modifier_curlang($row['caption']);
                $image .= "<a href=\"/{$row['path']}\" rel=\"lightbox{$group}\" title=\"{$row['caption']}\" style=\"display:none\"></a>";
            }
        }
    }
    return $image;
}
 /**
  * Function which called before render.
  *
  * @return  bool|PEAR_Error TRUE on success or PEAR_Error on failure.
  * @access  protected
  * @see     Image_Tools::createImage()
  */
 function preRender()
 {
     $res = Image_Tools::createImage($this->options['image1']);
     if (PEAR::isError($res)) {
         return $res;
     }
     $this->_image1 = $res;
     $res = Image_Tools::createImage($this->options['image2']);
     if (PEAR::isError($res)) {
         return $res;
     }
     $this->_image2 = $res;
     // initialize an array if only its interpolation mode
     if ($this->options['mode'] == 'interpolation') {
         for ($i = 0; $i < 256; $i++) {
             $this->cosineTab[] = (int) round(64 - cos($i * M_PI / 255) * 64);
         }
     }
     return true;
 }
 /**
  * Draw extraction result to resource.
  *
  * @return bool|PEAR_Error TRUE on success or PEAR_Error on failure.
  * @access public
  * @see Image_Tools_Marquee::_extractRectangle(),
  *      Image_Tools_Marquee::_extractSingleCol(),
  *      Image_Tools_Marquee::_extractSingleRow()
  */
 function render()
 {
     if (!Image_Tools::isGDImageResource($this->resultImage)) {
         return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_resultImage');
     }
     $x = $this->options['x'];
     $y = $this->options['y'];
     switch ($this->_marquee['type']) {
         case IMAGE_TOOLS_MARQUEE_TYPE_RECTANGLE:
             $this->_extractRectangle($this->resultImage, $x, $y);
             break;
         case IMAGE_TOOLS_MARQUEE_TYPE_SINGLECOL:
             $this->_extractSingleColumn($this->resultImage, $x, $y);
             break;
         case IMAGE_TOOLS_MARQUEE_TYPE_SINGLEROW:
             $this->_extractSingleRow($this->resultImage, $x, $y);
             break;
     }
     return true;
 }
Example #10
0
 /**
  * Draw image with logo result to resource.
  *
  * @return  bool|PEAR_Error TRUE on success or PEAR_Error on failure.
  * @access  public
  */
 function render()
 {
     $W = imagesx($this->_source);
     $H = imagesy($this->_source);
     $MW = imagesx($this->_mark);
     $MH = imagesy($this->_mark);
     // resize the logo image
     if ($this->options['width'] == 0 && $this->options['height'] == 0) {
         $this->options['width'] = $MW;
         $this->options['height'] = $MH;
     } else {
         if ($this->options['width'] == 0) {
             $this->options['width'] = ceil($this->options['height'] * $MW / $MH);
         } else {
             if ($this->options['height'] == 0) {
                 $this->options['height'] = ceil($this->options['width'] * $MH / $MW);
             }
         }
     }
     // calculates the x position
     switch ($this->options['horipos']) {
         case IMAGE_TOOLS_WATERMARK_POSITION_LEFT:
             $posx = $this->options['marginx'];
             break;
         case IMAGE_TOOLS_WATERMARK_POSITION_CENTER:
             $posx = round(($W - $this->options['width']) / 2);
             break;
         case IMAGE_TOOLS_WATERMARK_POSITION_RIGHT:
         default:
             $posx = $W - $this->options['width'] - $this->options['marginx'];
             break;
     }
     // calculates the y position
     switch ($this->options['vertpos']) {
         case IMAGE_TOOLS_WATERMARK_POSITION_TOP:
             $posy = $this->options['marginy'];
             break;
         case IMAGE_TOOLS_WATERMARK_POSITION_MIDDLE:
             $posy = round(($H - $this->options['height']) / 2);
             break;
         case IMAGE_TOOLS_WATERMARK_POSITION_BOTTOM:
         default:
             $posy = $H - $this->options['height'] - $this->options['marginy'];
             break;
     }
     settype($posx, 'integer');
     settype($posy, 'integer');
     // Create the target image
     imagealphablending($this->_mark, false);
     // resize the mark image
     if ($MH != $this->options['height'] && $MW != $this->options['width']) {
         if (function_exists('imagecreatetruecolor')) {
             $mark = imagecreatetruecolor($this->options['width'], $this->options['height']);
         } else {
             $mark = imagecreate($this->options['width'], $this->options['height']);
         }
         imagealphablending($mark, false);
         if (function_exists('imagecoypresampled')) {
             $result = imagecopyresampled($mark, $this->_mark, 0, 0, 0, 0, $this->options['width'], $this->options['height'], $MW, $MH);
         } else {
             $result = imagecopyresampled($mark, $this->_mark, 0, 0, 0, 0, $this->options['width'], $this->options['height'], $MW, $MH);
         }
         $this->_mark = $mark;
     }
     if ($this->options['blend'] != 'none') {
         $blend = Image_Tools::factory('blend');
         $blend->set('image1', $this->_source);
         $blend->set('image2', $this->_mark);
         $blend->set('x', $posx);
         $blend->set('y', $posy);
         $blend->set('mode', $this->options['blend']);
         // applies the blending mode.
         $this->_source = $blend->getResultImage();
     } else {
         $result = imagecopy($this->_source, $this->_mark, $posx, $posy, 0, 0, $MW, $MH);
         if (!$result) {
             return PEAR::raiseError('Cannot copy logo image');
         }
     }
     $this->resultImage = $this->_source;
     unset($this->_mark);
     return true;
 }