/** * @param string $file * @param int $width * @param int $height * @param string $fileType * @param int $quality on percentage scale from 0 to 100 (only for JPEG and PNG) * * @return string Thumbnail image path */ public static function thumbnalize($file, $width = null, $height = null, $fileType = self::TYPE_PNG, $quality = self::QUALITY_MEDIUM) { $file = static::$wwwDir . '/' . $file; if (!is_file($file)) { return; } switch ($fileType) { case self::TYPE_JPEG: $type = Image::JPEG; break; case self::TYPE_PNG: $type = Image::PNG; break; case self::TYPE_GIF: $type = Image::GIF; break; default: $type = Image::PNG; break; } $fileName = self::getFileName($file, $fileType); $destinationDir = $width . '_' . $height . '/' . $fileType . '/' . $quality; $thumbDir = static::$wwwDir . static::$thumbDir; if (file_exists($thumbDir . '/' . $destinationDir . '/' . $fileName)) { return static::$thumbDir . '/' . $destinationDir . '/' . $fileName; } static::$image = Image::fromFile($file); if (static::isWidthSet($width) && static::isHeightSet($height)) { static::resizeImageExactly($width, $height); } elseif (static::isWidthSet($width)) { static::resizeImageProportionally($width, $height); } else { static::resizeImageProportionally(static::$image->getWidth(), $height); } FileSystem::createDir($thumbDir . '/' . $destinationDir); static::$image->save($thumbDir . '/' . $destinationDir . '/' . $fileName, $quality, $type); return static::$thumbDir . '/' . $destinationDir . '/' . $fileName; }
/** * Puts another image into this image. * @param Image * @param mixed x-coordinate in pixels or percent * @param mixed y-coordinate in pixels or percent * @param int opacity 0..100 * @return self */ public function place(Image $image, $left = 0, $top = 0, $opacity = 100) { $opacity = max(0, min(100, (int) $opacity)); if (substr($left, -1) === '%') { $left = round(($this->getWidth() - $image->getWidth()) / 100 * $left); } if (substr($top, -1) === '%') { $top = round(($this->getHeight() - $image->getHeight()) / 100 * $top); } if ($opacity === 100) { imagecopy($this->image, $image->getImageResource(), $left, $top, 0, 0, $image->getWidth(), $image->getHeight()); } elseif ($opacity != 0) { imagecopymerge($this->image, $image->getImageResource(), $left, $top, 0, 0, $image->getWidth(), $image->getHeight(), $opacity); } return $this; }
/** * Puts another image into this image. * @param Image * @param mixed x-coordinate in pixels or percent * @param mixed y-coordinate in pixels or percent * @param int opacity 0..100 * @return self */ public function place(Image $image, $left = 0, $top = 0, $opacity = 100) { $opacity = max(0, min(100, (int) $opacity)); if (is_string($left) && substr($left, -1) === '%') { $left = (int) round(($this->getWidth() - $image->getWidth()) / 100 * $left); } if (is_string($top) && substr($top, -1) === '%') { $top = (int) round(($this->getHeight() - $image->getHeight()) / 100 * $top); } if ($opacity === 100) { imagecopy($this->image, $image->getImageResource(), $left, $top, 0, 0, $image->getWidth(), $image->getHeight()); } elseif ($opacity != 0) { $cutting = imagecreatetruecolor($image->getWidth(), $image->getHeight()); imagecopy($cutting, $this->image, 0, 0, $left, $top, $image->getWidth(), $image->getHeight()); imagecopy($cutting, $image->getImageResource(), 0, 0, 0, 0, $image->getWidth(), $image->getHeight()); imagecopymerge($this->image, $cutting, $left, $top, 0, 0, $image->getWidth(), $image->getHeight(), $opacity); } return $this; }