Yii2 imagecreatefrompng不适用于透明

时间:2018-04-05 06:25:26

标签: php image yii yii2 crop

我在上传时使用自定义模型裁剪和压缩我的图像文件。问题是当上传图像/ 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;
   }

0 个答案:

没有答案