Image::watermark PHP Метод

watermark() публичный Метод

public watermark ( $watermark, $position = 'bottomright' )
    public function watermark($watermark, $position = 'bottomright')
    {
        switch ($position) {
            case 'topleft':
                $watermark_pos_x = 0;
                $watermark_pos_y = 0;
                break;
            case 'topcenter':
                $watermark_pos_x = intval(($this->width - $watermark->getWidth()) / 2);
                $watermark_pos_y = 0;
                break;
            case 'topright':
                $watermark_pos_x = $this->width - $watermark->getWidth();
                $watermark_pos_y = 0;
                break;
            case 'middleleft':
                $watermark_pos_x = 0;
                $watermark_pos_y = intval(($this->height - $watermark->getHeight()) / 2);
                break;
            case 'middlecenter':
                $watermark_pos_x = intval(($this->width - $watermark->getWidth()) / 2);
                $watermark_pos_y = intval(($this->height - $watermark->getHeight()) / 2);
                break;
            case 'middleright':
                $watermark_pos_x = $this->width - $watermark->getWidth();
                $watermark_pos_y = intval(($this->height - $watermark->getHeight()) / 2);
                break;
            case 'bottomleft':
                $watermark_pos_x = 0;
                $watermark_pos_y = $this->height - $watermark->getHeight();
                break;
            case 'bottomcenter':
                $watermark_pos_x = intval(($this->width - $watermark->getWidth()) / 2);
                $watermark_pos_y = $this->height - $watermark->getHeight();
                break;
            case 'bottomright':
                $watermark_pos_x = $this->width - $watermark->getWidth();
                $watermark_pos_y = $this->height - $watermark->getHeight();
                break;
        }
        imagealphablending($this->image, true);
        imagesavealpha($this->image, true);
        imagecopy($this->image, $watermark->getImage(), $watermark_pos_x, $watermark_pos_y, 0, 0, $watermark->getWidth(), $watermark->getHeight());
        imagedestroy($watermark->getImage());
    }

Usage Example

Пример #1
0
 /**
  * Image caching
  *
  * Resize & cache image into the file system. Returns the template image if product image is not exists
  * At this time supports JPG images only
  *
  * @param mixed $name
  * @param int $user_id
  * @param int $width Resizing width
  * @param int $height Resizing height
  * @param bool $watermarked
  * @param bool $overwrite
  * @return string Cached Image URL
  */
 public function image($name, $user_id, $width, $height, $watermarked = false, $overwrite = false)
 {
     $storage = DIR_STORAGE . $user_id . DIR_SEPARATOR . $name . '.' . ALLOWED_IMAGE_EXTENSION;
     $cache = DIR_IMAGE . 'cache' . DIR_SEPARATOR . $user_id . DIR_SEPARATOR . $name . '-' . $width . '-' . $height . '.' . ALLOWED_IMAGE_EXTENSION;
     $watermark = DIR_IMAGE . 'common' . DIR_SEPARATOR . 'watermark.png';
     $cached_url = ($this->_request->getHttps() ? HTTPS_IMAGE_SERVER : HTTP_IMAGE_SERVER) . 'cache' . DIR_SEPARATOR . $user_id . DIR_SEPARATOR . $name . '-' . $width . '-' . $height . '.' . ALLOWED_IMAGE_EXTENSION;
     // Force reset
     if ($overwrite) {
         unlink($cache);
     }
     // If image is cached
     if (file_exists($cache)) {
         return $cached_url;
         // If image not cached
     } else {
         // Create directories by path if not exists
         $directories = explode(DIR_SEPARATOR, $cache);
         $path = '';
         foreach ($directories as $directory) {
             $path .= DIR_SEPARATOR . $directory;
             if (!is_dir($path) && false === strpos($directory, '.')) {
                 mkdir($path, 0755);
             }
         }
         // Prepare new image
         $image = new Image($storage);
         $image->resize($width, $height);
         if ($watermarked) {
             $image->watermark($watermark);
         }
         $image->save($cache);
     }
     return $cached_url;
 }
All Usage Examples Of Image::watermark