Yii2 imagecreatefrompng не работают с прозрачным

Я использую пользовательскую модель, чтобы обрезать и сжать файл изображения при загрузке. Проблема в том, что при загрузке изображения / png … он обрезает и сжимает изображение, но прозрачный фон заменяет черный фон … я не вижу своей ошибки …
вот функция загрузки в контроллер

$model->imageFiles = UploadedFile::getInstances($model, 'imageFiles[' . $imgKey . ']'); // get the imageFiles
$pic = Yii::getAlias('@frontend/web') . '/product_photos/thumb-270/' . $model->getImageFolderName() . '/' . $fileName;  // set the thumb path
$pic2 = Yii::getAlias('@frontend/web') . '/product_photos/' . $model->getImageFolderName() . '/' . $fileName; // set reale image path
$file->saveAs(Yii::getAlias('@frontend/web') . '/product_photos/' . $model->getImageFolderName() . '/' . $fileName);
$image = file_get_contents(Yii::getAlias('@frontend/web') . '/product_photos/' . $model->getImageFolderName() . '/' . $fileName);
file_put_contents($pic, $image);
$model->resizeImg($pic);
if($file->type!='image/png') {
$settings->compress($pic, $pic, 90);
$settings->compress($pic2, $pic2, 90);
}

вот модельная функция resizeimg:

 public function resizeImg($img) {
$sz = getimagesize($img);
$ratio = $sz[0] / $sz[1]; // w/h

$w2 = Yii::$app->params['thumbswidth']; // thumb 1 width

$image = new SimpleImage();

$image->load($img);

$image->resize($w2, round($w2 / $ratio));
$image->save($img);
}

а вот и вся модель SimpleImage:

 var $image;
var $image_type;

function load($DATA, $FORMNAME = NULL) {

$image_info = getimagesize($DATA);
$this->image_type = $image_info[2];

if( $this->image_type == IMAGETYPE_JPEG ) {

$this->image = imagecreatefromjpeg($DATA);
} elseif( $this->image_type == IMAGETYPE_GIF ) {

$this->image = imagecreatefromgif($DATA);
} elseif( $this->image_type == IMAGETYPE_PNG ) {

$this->image = imagecreatefrompng($DATA);

}
}

function save($filename, $image_type=IMAGETYPE_JPEG, $compression=100, $permissions=null) {

if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {

imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {

imagepng($this->image,$filename);
}
if( $permissions != null) {

chmod($filename,$permissions);
}
}

function output($image_type=IMAGETYPE_JPEG) {

if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {

imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {

imagepng($this->image);
}
}

function getWidth() {

return imagesx($this->image);
}

function getHeight() {

return imagesy($this->image);
}

function resizeToHeight($height) {

$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}

function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}

function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}

function resize($width,$height) {

$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
imagesavealpha($new_image, true);
$this->image = $new_image;
}

1

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector