Example #1
0
/**
 * Smarty {image} function plugin
 *
 */
function smarty_function_image($params, &$smarty)
{
    if (!isset($params['src'])) {
        return '<!-- image not set -->';
    } else {
        $baseDir = dirname(__FILE__) . '/../../../../';
        $filename = $baseDir . $params['src'];
        if (!file_exists($filename)) {
            $params['src'] = 'images/default.gif';
            $filename = $baseDir . $params['src'];
        }
        $newImageBase = $baseDir . 'tmp/media/' . $params['width'] . '-' . $params['height'] . '/';
        $folders = explode('/', $params['src']);
        array_pop($folders);
        $folder = $newImageBase . implode('/', $folders);
        if (!file_exists($folder)) {
            mkdir($folder, 0777, TRUE);
        }
        $newPath = $newImageBase . $params['src'];
        if (!file_exists($newPath)) {
            $image = new MonoImage($filename);
            $image->transform(new TrimTransformFactory($params['width'], $params['height']), $newPath);
        }
        $img = '<img src="' . str_replace($baseDir, '', $newPath) . '"';
        if ($params['alt']) {
            $img .= ' alt="' . $params['alt'] . '"';
        } else {
            $img .= ' alt=""';
        }
        if ($params['class']) {
            $img .= ' class="' . $params['class'] . '"';
        }
        if ($params['width']) {
            $img .= ' width="' . $params['width'] . '"';
        }
        if ($params['height']) {
            $img .= ' height="' . $params['height'] . '"';
        }
        $img .= ' />';
        return $img;
    }
}
 /**
  * Merge two resources.
  *
  * @param resource $newimg		orginal image resource
  * @param resource $new_tr		watermark image resource
  * @return resource				new orginal image resource width watermark
  */
 private function mergeResources(&$newimg, &$new_tr)
 {
     $t_width = imagesx($new_tr);
     $t_height = imagesy($new_tr);
     imagesavealpha($new_tr, 1);
     imagesavealpha($newimg, 1);
     imagealphablending($new_tr, true);
     $posx = 0;
     $posy = 0;
     // set x position
     if ($this->position == WatermarkTransformFactory::TOP_RIGHT || $this->position == WatermarkTransformFactory::CENTER_RIGHT || $this->position == WatermarkTransformFactory::BOTTOM_RIGHT) {
         $posx = $this->width - $t_width;
     }
     if ($this->position == WatermarkTransformFactory::TOP_CENTER || $this->position == WatermarkTransformFactory::CENTER_CENTER || $this->position == WatermarkTransformFactory::BOTTOM_CENTER) {
         $posx = $this->width / 2 - $t_width / 2;
     }
     // set y position
     if ($this->position == WatermarkTransformFactory::TOP_CENTER || $this->position == WatermarkTransformFactory::CENTER_CENTER || $this->position == WatermarkTransformFactory::BOTTOM_CENTER) {
         $posy = $this->height / 2 - $t_height / 2;
     }
     if ($this->position == WatermarkTransformFactory::TOP_RIGHT || $this->position == WatermarkTransformFactory::CENTER_RIGHT || $this->position == WatermarkTransformFactory::BOTTOM_RIGHT) {
         $posy = $this->height - $t_height;
     }
     if ($this->alpha == 100) {
         imagecopy($newimg, $new_tr, $posx, $posy, 0, 0, $t_width, $t_height);
     } else {
         if ($this->transparent_image->getExtension() == 'png') {
             imagecopy($newimg, $new_tr, $posx, $posy, 0, 0, $t_width, $t_height);
             imagealphablending($newimg, false);
             imagesavealpha($newimg, true);
         } else {
             if ($this->transparent_image->getExtension() == 'gif') {
                 imagecopymerge($newimg, $new_tr, $posx, $posy, 0, 0, $this->width, $this->height, $this->alpha);
             } else {
                 throw new Exception('Transprent not supported for file ' . $this->transparent_image->getExtension());
             }
         }
     }
     return $new_tr;
 }