ramzes, да то старый код
ramzes, да то старый код
$a = new Ki2('15.jpg');
$b = new Ki2('https://yt3.ggpht.com/-GzNJzEZE4Vk/AAAAAAAAAAI/AAAAAAAAAAA/MV79wKQaWDg/s88-c-k-no/photo.jpg');
$c = new Ki2('http://seclub.org/img/touch/logo.png');
$data = 'iVBORw0KGgoAAAANSUhEUgAAAMgAAACzCAIAAABD38TLAAAgAElEQVR42ny9a5ckSW4ldgGYuXs8
MjKzul79mJ4ZckRyyOFyXqR49Belc/SDtDxHq0/7aXePJM7ysUuK5JBD9rOqMiPC3c0A6APMPSKr
eja7ujszKzPC3QwGXFxcwOl//d/+97/4i79gdtNKcCbAHQQ3AIA6ETPYYIb41uWDIASOz4h4+ba3
--- здесь было выпилено около 1000 строк
V6vjxTl2mqagupOu/UHnzllGnfRXjSRctfhMFEN5N486AWl8ucGXjFo93nwIXAWGIiyGYw5MzGCo
oZEVM1EYOsrhOWhjdwgSkoxEzWoUuWDWmxKRh6/FEC9VX7LhOkDuZpfLeZ7b+XR+eno+Hs+Xy2Wu
DQDevn2HiP/f//9vHz/+st1toztawRHXNcm/mwet1+z/OjkjFS7peZylJUnDXyWLsGRdS8LNZns4
HLzbN998c3d39/DwcHNz8/DwUEphYRYJGkIoFNRaGfB4PLprLpxJzLR1ZUocKKUIMO5McK1yHigG
Xi/OeEuBKAAWUW2cRBCHfR6AiKTk1xj5dX+31jddu6mt+PGU0nyZY09Se3d8fQPk9IpNGPFkFHwI
QBBlScQCDO/3hWXqHtU9U4x61UeDpr0jUgrdDiQgcEQzBVVidIQBRRyF4+Dox++EZf6oXTXUXBZP
zXVEHNOK4aYEEJvc+XyK+7NK36aUtpsNEe12ttvtbm5uPn3+/fPnT0/Pzy8vL9E+B89nqQ3sGsK/
5pJIbiMLXkWl/wNID0pozGaO6gAAAABJRU5ErkJggg==';
$d = new Ki2($data);
$e = new Ki2('http://annimon.com/forum/avatars/1314.png?1301840184');
$a->roundAngle();
$b->rotate(45);
$c->rotate(45);
$e->roundAngle();
$d->rotate(45);
$a->watermark($b, 100, 100)->watermark($c, 200, 200)->watermark($d, 100, 600)->watermark($e, 300, 300);
echo '<div style="background-color: rgb(125, 100, 34);">'
. '<img src="data:image/png;base64,' . $a->base64() . '" alt="base64_image" style="background-color: rgb(90, 187, 200);" />'
. '</div>';с прозрачностью прям замучился, еще не все, предсоит еще кое чего перепилить
how i can put articlestitle on article's img look uztim.ru
на самом деле потестил и убедился что Imagick в разы круче
нарисовал класс с тем же функционалом, плюс стандартные (300+) меттоды доступны
<?php
class Kimagick {
private $image, $width, $height = false;
const UR = 1;
const UL = 2;
const DL = 3;
const DR = 4;
public function check() {
if (!extension_loaded('imagick')) {
throw new ErrorException('Imagick is not installed');
}
}
public function __construct($file) {
$this->check();
$this->tmp($file);
}
public function save($filename, $quality = 100) {
$this->image->setImageCompressionQuality($quality);
$this->image->writeImage($filename);
}
public function tmp($image) {
$this->image = new Imagick;
if (file_exists($image)) {
$image = file_get_contents($image);
} elseif (preg_match('#^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$#', $file)) {
$image = base64_decode($image);
} elseif (substr($image, 0, 4) == 'http') {
if (list(, , $type) = getimagesize($image)) {
if ($type) {
$upload = new SplFileObject($image, 'rb');
$image = '';
while (!$upload->eof()) {
$image .= $upload->fgets();
}
} else {
throw new Exception('Unsupported image type');
return false;
}
}
}
if ($this->image->readImageBlob($image)) {
if ($this->image->getImageAlphaChannel() !== Imagick::ALPHACHANNEL_ACTIVATE) {
$this->image->setImageAlphaChannel(Imagick::ALPHACHANNEL_SET); // 8
#$this->image->setImageAlphaChannel(Imagick::ALPHACHANNEL_OPAQUE); // 6
}
}
$background = new Imagick;
$background->newImage($this->width, $this->height, new ImagickPixel('transparent'));
$background->setImageBackgroundColor(new ImagickPixel('transparent'));
$this->image->compositeImage($background, imagick::COMPOSITE_OVER, 0, 0); //Imagick::COMPOSITE_DISSOLVE
$this->image->setFormat('png');
$this->setSizes();
}
public function watermark($file, $corner = self::UL, $opacity = 0.5) {
$watermark = new Imagick(realpath($file));
$watermark->evaluateImage(Imagick::EVALUATE_MULTIPLY, $opacity, Imagick::CHANNEL_ALPHA);
$x = $this->widht();
$y = $this->height();
#$watermark->scaleImage($x / 8, 0);
$watermark->scaleImage(0, $y / 6);
$wx = $watermark->getImageWidth();
$wy = $watermark->getImageHeight();
$offsetw = 10;
$offseth = 10;
$resx = $offsetw;
$resy = $offseth;
switch($corner) {
case self::UL: // 2 верхний левый
default:
break;
case self::UR: // 1 верхний правый
$resx = $x - $wx - $offsetw;
break;
case self:
L: // 3 нижний левый
$resy = $y - $wy - $offseth;
break;
case self:
R: // 4 нижний правый
$resx = $x - $wx - $offsetw;
$resy = $y - $wy - $offseth;
break;
}
$this->image->compositeImage($watermark, Imagick::COMPOSITE_DISSOLVE, $resx, $resy);
return $this;
}
/*
public function copy_watermark(self $watermark, $offset_x, $offset_y, $opacity = 0.5) {
$watermark->image()->evaluateImage(Imagick::EVALUATE_MULTIPLY, $opacity, Imagick::CHANNEL_ALPHA);
return $this->image->compositeImage($watermark, Imagick::COMPOSITE_DISSOLVE, $offset_x, $offset_y);
}
*/
/**
* Изменение размера в процентном соотношении
*
* @param integer $scale
* @return Kimagick
*/
public function scale($scale) {
$width = $this->widht() * ($scale / 100);
$height = $this->height() * ($scale / 100);
$this->resize($width, $height);
}
public function resize($width, $height) {
$this->image->scaleImage($width, $height, 1);
$this->setSizes();
return $this;
}
public function crop($width, $height, $x, $y) {
$this->image->cropImage($width, $height, $x, $y);
$this->setSizes();
return $this;
}
public function reHeight($height) {
$this->resize($this->widht() * ($height / $this->height()), $height);
}
public function reWidth($width) {
$this->resize($width, $this->height() * ($width / $this->widht()));
}
public function rotate($angle = 90) {
$this->image->rotateImage(new ImagickPixel('#00000000'), $angle);
#$this->image->rotateImage(new ImagickPixel('transparent'), $angle);
$this->setSizes();
return $this;
}
public function roundCorners() {
$this->image->roundCorners(20, 20);
$this->setSizes();
return $this;
}
/**
* Создает вертикальное зеркало изображения
*
*/
public function flip() {
$this->image->flipImage();
return $this;
}
/**
* Создает горизонтальное зеркало изображения
*
*/
public function flop() {
$this->image->flopImage();
return $this;
}
public function widht() {
return $this->width;
}
public function height() {
return $this->height;
}
public function setSizes() {
$args = $this->image->getImageGeometry();
$this->width = $args['width'];
$this->height = $args['height'];
return $this;
}
public function image() {
return $this->image;
}
public function base64() {
return 'data:image/png;base64,' . base64_encode($this->image->getImageBlob());
}
// echo '<img src="' . $this->base64 . '" alt="base64_image" style="background-color: rgb(90, 187, 200);" />';
}$obj = new Kimagick('fazliddin.jpg');
#$obj->crop(300, 300, 100, 100);
#$obj->rotate(90);
#$obj->resize(800, 400);
$obj->watermark('wat2.png', 4, 1);
$obj->roundCorners();
#$obj->rotate(45);
$obj->scale(150);
#$obj->scale(50);
echo '<img src="' . $obj->base64() . '" alt="base64_image" style="background-color: rgb(150, 50, 50);" />'; // задний фон для теста прозрачности, красненький)
$obj->save(dirname(__FILE__) . '/test.png');
exit;# Koenig (17.08.2016 / 08:55)Как бы круче он не был, по-умолчанию с РНР он не идет.
на самом деле потестил и убедился что Imagick в разы круче
то что уже работает
остальное очень через жопу, закругление углов сделал, но мне не нравится, и проблема красного квадрата напрягает, так как прозрачность созданных через гд изображений косячит
class Kimage {
private $image, $width, $height = false;
public function __construct($file) {
$this->tmp($file);
}
public function save($filename, $quality = 9) {
imagepng($this->image, $filename, $quality, PNG_ALL_FILTERS);
}
public function tmp($image) {
if (file_exists($image)) {
$image = file_get_contents($image);
} elseif (preg_match('#[^-A-Za-z0-9+/=]|=[^=]|={3,}$#', $image)) {
$image = base64_decode($image);
} elseif (substr($image, 0, 4) == 'http') {
if (list(, , $type) = getimagesize($image)) {
if ($type) {
$upload = new SplFileObject($image, 'rb');
$image = '';
while (!$upload->eof()) {
$image .= $upload->fgets();
}
} else {
throw new Exception('Unsupported image type');
return false;
}
} else {
throw new Exception('Unsupported image type');
return false;
}
} else {
throw new Exception('Bad path to image');
return false;
}
$image = imagecreatefromstring($image);
if (!is_resource($image)) {
throw new Exception('Image create failed');
return false;
}
$this->width = imagesx($image);
$this->height = imagesy($image);
$this->image = $image;
imagesavealpha($this->image, true);
imagealphablending($this->image, false);
return $this;
}
public function mirroring() {
$newimage = $this->newImage($this->width, $this->height);
foreach(range($this->width, 0) as $range) {
imagecopy($newimage, $this->image, $this->width - $range - 1, 0, $range, 0, 1, $this->height);
}
$this->image = $newimage;
return $this;
}
public function newImage($width, $height) {
$newimg = imagecreatetruecolor($width, $height);
$transparent = imagecolorallocatealpha($newimg, 255, 255, 255, 127);
imagefill($newimg, 0, 0, $transparent);
imagealphablending($newimg, true);
imagesavealpha($newimg, true);
return $newimg;
}
public function rotate($angle = 90) {
$transparent = imagecolorallocatealpha($this->image, 0, 0, 0, 127);
$rotate = imagerotate($this->image, $angle, $transparent);
imagealphablending($rotate, true);
imagesavealpha($rotate, true);
$this->image = $rotate;
$this->setSizes();
return $this;
}
public function reHeight($height) {
$width = $this->width * ($height / $this->height);
$this->resize($width, $height);
return $this;
}
public function reWidth($width) {
$height = $this->height * ($width / $this->width);
$this->resize($width, $height);
return $this;
}
public function scale($scale) {
$width = $this->width * ($scale / 100);
$height = $this->height * ($scale / 100);
$this->resize($width, $height);
return $this;
}
public function resize($width, $height) {
$newimage = $this->newImage($width, $height);
imagecopyresampled($newimage, $this->image, 0, 0, 0, 0, $width, $height, $this->width , $this->height);
$this->image = $newimage;
$this->setSizes();
return $this;
}
public function crop($width, $height, $x, $y) {
$width = $width - $x;
$height = $height - $y;
$newimage = $this->newImage($width, $height);
imagecopyresampled($newimage, $this->image, 0, 0, $x, $y, $width, $height, $width, $height);
$this->image = $newimage;
$this->setSizes();
return $this;
}
public function setSizes() {
$this->width = imagesx($this->image);
$this->height = imagesy($this->image);
return $this;
}
public function grayscale() {
imagefilter($this->image, IMG_FILTER_GRAYSCALE);
return $this;
}
}